@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef EPlayerDamage_h__
|
||||||
|
#define EPlayerDamage_h__
|
||||||
|
|
||||||
|
#include "EventBroker.h"
|
||||||
|
#include "../Core/Entity.h"
|
||||||
|
|
||||||
|
namespace Events
|
||||||
|
{
|
||||||
|
|
||||||
|
struct PlayerDamage : Event
|
||||||
|
{
|
||||||
|
double DamageAmount;
|
||||||
|
EntityID PlayerDamagedID;
|
||||||
|
//optional TypeOfDamage
|
||||||
|
std::string TypeOfDamage;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef EPlayerDeath_h__
|
||||||
|
#define EPlayerDeath_h__
|
||||||
|
|
||||||
|
#include "EventBroker.h"
|
||||||
|
#include "../Core/Entity.h"
|
||||||
|
|
||||||
|
namespace Events
|
||||||
|
{
|
||||||
|
|
||||||
|
struct PlayerDeath : Event
|
||||||
|
{
|
||||||
|
//KilledBy,KilledByWhat is optional for now. It might be used later in the playerlog-system
|
||||||
|
EntityID KilledBy;
|
||||||
|
EntityID PlayerID;
|
||||||
|
std::string KilledByWhat;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef EPlayerHealthPickup_h__
|
||||||
|
#define EPlayerHealthPickup_h__
|
||||||
|
|
||||||
|
#include "EventBroker.h"
|
||||||
|
#include "../Core/Entity.h"
|
||||||
|
|
||||||
|
namespace Events
|
||||||
|
{
|
||||||
|
|
||||||
|
struct PlayerHealthPickup : Event
|
||||||
|
{
|
||||||
|
double HealthAmount;
|
||||||
|
EntityID PlayerHealedID;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef HealthSystem_h__
|
||||||
|
#define HealthSystem_h__
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glm/common.hpp>
|
||||||
|
|
||||||
|
#include "Common.h"
|
||||||
|
#include "Core/System.h"
|
||||||
|
#include "Core\EPlayerDamage.h";
|
||||||
|
#include "Core\EPlayerHealthPickup.h";
|
||||||
|
#include "Core\EPlayerDeath.h";
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class HealthSystem : public PureSystem
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
HealthSystem(EventBroker* eventBroker);
|
||||||
|
|
||||||
|
//updatecomponent
|
||||||
|
virtual void UpdateComponent(World* world, ComponentWrapper& health, double dt) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
//methods which will take care of specific events
|
||||||
|
EventRelay<HealthSystem, Events::PlayerDamage> m_EPlayerDamage;
|
||||||
|
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e);
|
||||||
|
EventRelay<HealthSystem, Events::PlayerHealthPickup> m_EPlayerHealthPickup;
|
||||||
|
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e);
|
||||||
|
|
||||||
|
//vector which will keep track of health changes
|
||||||
|
std::vector<std::tuple<EntityID, double>> m_DeltaHealthVector;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,4 +8,5 @@
|
|||||||
<xs:include schemaLocation="Components/Player.xsd"/>
|
<xs:include schemaLocation="Components/Player.xsd"/>
|
||||||
<xs:include schemaLocation="Components/AABB.xsd"/>
|
<xs:include schemaLocation="Components/AABB.xsd"/>
|
||||||
<xs:include schemaLocation="Components/Trigger.xsd"/>
|
<xs:include schemaLocation="Components/Trigger.xsd"/>
|
||||||
|
<xs:include schemaLocation="Components/Health.xsd"/>
|
||||||
</xs:schema>
|
</xs:schema>
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
<c:Health>
|
||||||
|
<Health>100</Health>
|
||||||
|
<MaxHealth>100</MaxHealth>
|
||||||
|
</c:Health>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
|
||||||
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
|
||||||
|
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
|
||||||
|
|
||||||
|
<xs:element name="Health">
|
||||||
|
<xs:complexType>
|
||||||
|
<xs:all>
|
||||||
|
<xs:element name="Health" type="t:double" minOccurs="0"/>
|
||||||
|
<xs:element name="MaxHealth" type="t:double" minOccurs="0"/>
|
||||||
|
</xs:all>
|
||||||
|
</xs:complexType>
|
||||||
|
</xs:element>
|
||||||
|
</xs:schema>
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
<xs:element ref="c:Test" minOccurs="0"/>
|
<xs:element ref="c:Test" minOccurs="0"/>
|
||||||
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
|
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
|
||||||
<xs:element ref="c:Player" minOccurs="0"/>
|
<xs:element ref="c:Player" minOccurs="0"/>
|
||||||
|
<xs:element ref="c:Health" minOccurs="0"/>
|
||||||
</xs:all>
|
</xs:all>
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
</xs:element>
|
</xs:element>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
BaseEventRelay::~BaseEventRelay()
|
BaseEventRelay::~BaseEventRelay()
|
||||||
{
|
{
|
||||||
if (m_Broker != nullptr) {
|
if (m_Broker != nullptr) {
|
||||||
m_Broker->Unsubscribe(*this);
|
m_Broker->Unsubscribe(*this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -1,6 +1,7 @@
|
|||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include "Collision/TriggerSystem.h"
|
#include "Collision/TriggerSystem.h"
|
||||||
#include "Collision/CollisionSystem.h"
|
#include "Collision/CollisionSystem.h"
|
||||||
|
#include "Game/HealthSystem.h"
|
||||||
|
|
||||||
Game::Game(int argc, char* argv[])
|
Game::Game(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
@@ -27,7 +28,7 @@ Game::Game(int argc, char* argv[])
|
|||||||
0,
|
0,
|
||||||
m_Config->Get<int>("Video.Width", 1280),
|
m_Config->Get<int>("Video.Width", 1280),
|
||||||
m_Config->Get<int>("Video.Height", 720)
|
m_Config->Get<int>("Video.Height", 720)
|
||||||
));
|
));
|
||||||
m_Renderer->Initialize();
|
m_Renderer->Initialize();
|
||||||
m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get<float>("Video.FOV", 90.f)));
|
m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get<float>("Video.FOV", 90.f)));
|
||||||
|
|
||||||
@@ -57,6 +58,7 @@ Game::Game(int argc, char* argv[])
|
|||||||
m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer);
|
m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer);
|
||||||
m_SystemPipeline->AddSystem<CollisionSystem>();
|
m_SystemPipeline->AddSystem<CollisionSystem>();
|
||||||
m_SystemPipeline->AddSystem<TriggerSystem>();
|
m_SystemPipeline->AddSystem<TriggerSystem>();
|
||||||
|
m_SystemPipeline->AddSystem<HealthSystem>();
|
||||||
|
|
||||||
// Invoke network
|
// Invoke network
|
||||||
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
|
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include "HealthSystem.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
HealthSystem::HealthSystem(EventBroker* eventBroker)
|
||||||
|
: PureSystem(eventBroker, "Health")
|
||||||
|
{
|
||||||
|
//subscribe/listenTo playerdamage,healthpickup events (using the eventBroker)
|
||||||
|
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &HealthSystem::OnPlayerDamaged);
|
||||||
|
EVENT_SUBSCRIBE_MEMBER(m_EPlayerHealthPickup, &HealthSystem::OnPlayerHealthPickup);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HealthSystem::UpdateComponent(World *world, ComponentWrapper &health, double dt)
|
||||||
|
{
|
||||||
|
//if entityID of health is 9 then the players ID is also 9 (player,health are connected to the same entity)
|
||||||
|
ComponentWrapper player = world->GetComponent(health.EntityID, "Player");
|
||||||
|
double maxHealth = (double)health["MaxHealth"];
|
||||||
|
|
||||||
|
//process the DeltaHealthVector and change the entitys health accordingly
|
||||||
|
for (size_t i = m_DeltaHealthVector.size(); i > 0; i--)
|
||||||
|
{
|
||||||
|
auto deltaHP = m_DeltaHealthVector[i - 1];
|
||||||
|
//if we have a healthchange for the current player, then apply it
|
||||||
|
if (std::get<0>(deltaHP) == player.EntityID) {
|
||||||
|
//get the deltaHP value from the tuple and make sure you dont get more than maxHealth
|
||||||
|
double newHealth = std::min((double)health["Health"] + (double)std::get<1>(deltaHP), maxHealth);
|
||||||
|
health["Health"] = newHealth;
|
||||||
|
m_DeltaHealthVector.erase(m_DeltaHealthVector.begin() + i - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//check if health is <= 0
|
||||||
|
if ((double)health["Health"] <= 0.0f) {
|
||||||
|
//publish death event
|
||||||
|
Events::PlayerDeath e;
|
||||||
|
e.PlayerID = player.EntityID;
|
||||||
|
m_EventBroker->Publish(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e)
|
||||||
|
{
|
||||||
|
//save the changed HP to a vector. it will be taken care of in UpdateComponent
|
||||||
|
m_DeltaHealthVector.push_back(std::make_tuple(e.PlayerDamagedID, -e.DamageAmount));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e)
|
||||||
|
{
|
||||||
|
//save the changed HP to a vector. it will be taken care of in UpdateComponent
|
||||||
|
m_DeltaHealthVector.push_back(std::make_tuple(e.PlayerHealedID, e.HealthAmount));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
using boost::unit_test_framework::test_suite;
|
||||||
|
using boost::unit_test_framework::test_case;
|
||||||
|
|
||||||
|
#include "HealthSystemTest.h"
|
||||||
|
#include "Game/HealthSystem.h"
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(HealthSystemSuite)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(HealthSystemTest)
|
||||||
|
{
|
||||||
|
//this tests 2 healthevents and the healthsystem
|
||||||
|
GameHealthSystemTest game;
|
||||||
|
//100 loops will be more than enough to do the test
|
||||||
|
int loops = 100;
|
||||||
|
bool success = false;
|
||||||
|
while (loops > 0) {
|
||||||
|
game.Tick();
|
||||||
|
if (game.TestSucceeded) {
|
||||||
|
success = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
loops--;
|
||||||
|
}
|
||||||
|
//The system will process the events, hence it will take a while before we can read anything
|
||||||
|
BOOST_TEST(success);
|
||||||
|
}
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
GameHealthSystemTest::GameHealthSystemTest()
|
||||||
|
{
|
||||||
|
ResourceManager::RegisterType<ConfigFile>("ConfigFile");
|
||||||
|
ResourceManager::RegisterType<EntityXMLFile>("EntityXMLFile");
|
||||||
|
|
||||||
|
m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
|
||||||
|
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
|
||||||
|
|
||||||
|
// Create the core event broker
|
||||||
|
m_EventBroker = new EventBroker();
|
||||||
|
|
||||||
|
// Create a world
|
||||||
|
m_World = new World();
|
||||||
|
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
|
||||||
|
if (!mapToLoad.empty()) {
|
||||||
|
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create system pipeline
|
||||||
|
m_SystemPipeline = new SystemPipeline(m_EventBroker);
|
||||||
|
m_SystemPipeline->AddSystem<PlayerSystem>();
|
||||||
|
m_SystemPipeline->AddSystem<HealthSystem>();
|
||||||
|
|
||||||
|
//The Test
|
||||||
|
//create entity which has transorm,player,model,health in it. i.e. is a player
|
||||||
|
EntityID playerID = m_World->CreateEntity();
|
||||||
|
ComponentWrapper transform = m_World->AttachComponent(playerID, "Transform");
|
||||||
|
ComponentWrapper model = m_World->AttachComponent(playerID, "Model");
|
||||||
|
model["Resource"] = "Models/Core/UnitSphere.obj";
|
||||||
|
ComponentWrapper player = m_World->AttachComponent(playerID, "Player");
|
||||||
|
ComponentWrapper health = m_World->AttachComponent(playerID, "Health");
|
||||||
|
healthsID = playerID;
|
||||||
|
double currentHealth = (double)m_World->GetComponent(healthsID, "Health")["Health"];
|
||||||
|
|
||||||
|
//heal player with 40
|
||||||
|
Events::PlayerHealthPickup e3;
|
||||||
|
e3.HealthAmount = 40.0f;
|
||||||
|
e3.PlayerHealedID = healthsID;
|
||||||
|
m_EventBroker->Publish(e3);
|
||||||
|
//damage player with 50
|
||||||
|
Events::PlayerDamage e;
|
||||||
|
e.DamageAmount = 50.0f;
|
||||||
|
e.PlayerDamagedID = healthsID;
|
||||||
|
m_EventBroker->Publish(e);
|
||||||
|
//heal some other player with 40
|
||||||
|
Events::PlayerHealthPickup e2;
|
||||||
|
e2.HealthAmount = 40.0f;
|
||||||
|
e2.PlayerHealedID = healthsID+1;
|
||||||
|
m_EventBroker->Publish(e2);
|
||||||
|
|
||||||
|
EntityID playerID2 = m_World->CreateEntity();
|
||||||
|
ComponentWrapper transform2 = m_World->AttachComponent(playerID2, "Transform");
|
||||||
|
ComponentWrapper model2 = m_World->AttachComponent(playerID2, "Model");
|
||||||
|
model2["Resource"] = "Models/Core/UnitSphere.obj";
|
||||||
|
ComponentWrapper player2 = m_World->AttachComponent(playerID2, "Player");
|
||||||
|
ComponentWrapper health2 = m_World->AttachComponent(playerID2, "Health");
|
||||||
|
//END TEST
|
||||||
|
}
|
||||||
|
|
||||||
|
GameHealthSystemTest::~GameHealthSystemTest()
|
||||||
|
{
|
||||||
|
delete m_SystemPipeline;
|
||||||
|
delete m_World;
|
||||||
|
delete m_EventBroker;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameHealthSystemTest::Tick()
|
||||||
|
{
|
||||||
|
glfwPollEvents();
|
||||||
|
|
||||||
|
double currentTime = glfwGetTime();
|
||||||
|
double dt = currentTime - m_LastTime;
|
||||||
|
m_LastTime = currentTime;
|
||||||
|
|
||||||
|
// Iterate through systems and update world!
|
||||||
|
m_SystemPipeline->Update(m_World, dt);
|
||||||
|
|
||||||
|
m_EventBroker->Swap();
|
||||||
|
m_EventBroker->Clear();
|
||||||
|
|
||||||
|
//if health reaches 90 then we know the test has succeeded (start with 100hp, remove 50hp, add 40hp)
|
||||||
|
double currentHealth = (double)m_World->GetComponent(healthsID, "Health")["Health"];
|
||||||
|
if (currentHealth==90)
|
||||||
|
TestSucceeded = true;
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#ifndef HealthTest_h__
|
||||||
|
#define HealthTest_h__
|
||||||
|
|
||||||
|
#include "Core/ResourceManager.h"
|
||||||
|
#include "Core/ConfigFile.h"
|
||||||
|
#include "Core/EventBroker.h"
|
||||||
|
#include "Rendering/Renderer.h"
|
||||||
|
#include "Core/InputManager.h"
|
||||||
|
#include "GUI/Frame.h"
|
||||||
|
#include "Core/World.h"
|
||||||
|
#include "Rendering/RenderQueueFactory.h"
|
||||||
|
#include "Input/InputProxy.h"
|
||||||
|
#include "Input/KeyboardInputHandler.h"
|
||||||
|
#include "Input/MouseInputHandler.h"
|
||||||
|
#include "Core/EKeyDown.h"
|
||||||
|
#include "Core/EntityXMLFile.h"
|
||||||
|
#include "Core/SystemPipeline.h"
|
||||||
|
#include "RaptorCopterSystem.h"
|
||||||
|
#include "PlayerSystem.h"
|
||||||
|
#include "Editor/EditorSystem.h"
|
||||||
|
|
||||||
|
class GameHealthSystemTest
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
GameHealthSystemTest();
|
||||||
|
~GameHealthSystemTest();
|
||||||
|
|
||||||
|
void Tick();
|
||||||
|
bool TestSucceeded = false;
|
||||||
|
|
||||||
|
private:
|
||||||
|
double m_LastTime;
|
||||||
|
ConfigFile* m_Config = nullptr;
|
||||||
|
EventBroker* m_EventBroker;
|
||||||
|
World* m_World;
|
||||||
|
SystemPipeline* m_SystemPipeline;
|
||||||
|
int healthsID;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user