diff --git a/include/Engine/Core/EPlayerDamage.h b/include/Engine/Core/EPlayerDamage.h new file mode 100644 index 00000000..87ad67aa --- /dev/null +++ b/include/Engine/Core/EPlayerDamage.h @@ -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 \ No newline at end of file diff --git a/include/Engine/Core/EPlayerDeath.h b/include/Engine/Core/EPlayerDeath.h new file mode 100644 index 00000000..00ede5ed --- /dev/null +++ b/include/Engine/Core/EPlayerDeath.h @@ -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 \ No newline at end of file diff --git a/include/Engine/Core/EPlayerHealthPickup.h b/include/Engine/Core/EPlayerHealthPickup.h new file mode 100644 index 00000000..f3158f92 --- /dev/null +++ b/include/Engine/Core/EPlayerHealthPickup.h @@ -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 \ No newline at end of file diff --git a/include/Game/HealthSystem.h b/include/Game/HealthSystem.h new file mode 100644 index 00000000..a836e797 --- /dev/null +++ b/include/Game/HealthSystem.h @@ -0,0 +1,36 @@ +#ifndef HealthSystem_h__ +#define HealthSystem_h__ + +#include +#include + +#include "Common.h" +#include "Core/System.h" +#include "Core\EPlayerDamage.h"; +#include "Core\EPlayerHealthPickup.h"; +#include "Core\EPlayerDeath.h"; + +#include +#include + +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 m_EPlayerDamage; + bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e); + EventRelay m_EPlayerHealthPickup; + bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e); + + //vector which will keep track of health changes + std::vector> m_DeltaHealthVector; + +}; + +#endif \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index fd04fd39..7fcdd565 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -8,4 +8,5 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/Health.xml b/resources/Schema/Components/Health.xml new file mode 100644 index 00000000..143a91d1 --- /dev/null +++ b/resources/Schema/Components/Health.xml @@ -0,0 +1,4 @@ + + 100 + 100 + \ No newline at end of file diff --git a/resources/Schema/Components/Health.xsd b/resources/Schema/Components/Health.xsd new file mode 100644 index 00000000..0da80c1f --- /dev/null +++ b/resources/Schema/Components/Health.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 92f7dc31..5178b525 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -15,6 +15,7 @@ + diff --git a/src/Engine/Core/EventBroker.cpp b/src/Engine/Core/EventBroker.cpp index 6767878f..d847e1a2 100644 --- a/src/Engine/Core/EventBroker.cpp +++ b/src/Engine/Core/EventBroker.cpp @@ -3,7 +3,7 @@ BaseEventRelay::~BaseEventRelay() { if (m_Broker != nullptr) { - m_Broker->Unsubscribe(*this); + m_Broker->Unsubscribe(*this); } } diff --git a/src/Game/CMakeLists.txt b/src/Game/CMakeLists.txt index 18b80f93..04146670 100644 --- a/src/Game/CMakeLists.txt +++ b/src/Game/CMakeLists.txt @@ -19,6 +19,7 @@ file(GLOB SOURCE_FILES set(SOURCE_FILES ${SOURCE_FILES} "Game.cpp" + "HealthSystem.cpp" "PlayerSystem.cpp" ) diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index f71fcdfe..7d294355 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -1,6 +1,7 @@ #include "Game.h" #include "Collision/TriggerSystem.h" #include "Collision/CollisionSystem.h" +#include "Game/HealthSystem.h" Game::Game(int argc, char* argv[]) { @@ -27,7 +28,7 @@ Game::Game(int argc, char* argv[]) 0, m_Config->Get("Video.Width", 1280), m_Config->Get("Video.Height", 720) - )); + )); m_Renderer->Initialize(); m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get("Video.FOV", 90.f))); @@ -58,6 +59,7 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel); m_SystemPipeline->AddSystem(updateOrderLevel, m_Renderer); + m_SystemPipeline->AddSystem(updateOrderLevel); //Collision and TriggerSystem should update after player. ++updateOrderLevel; diff --git a/src/Game/HealthSystem.cpp b/src/Game/HealthSystem.cpp new file mode 100644 index 00000000..41f60ce5 --- /dev/null +++ b/src/Game/HealthSystem.cpp @@ -0,0 +1,52 @@ +#include "HealthSystem.h" +#include + +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; +} diff --git a/src/Tests/HealthSystemTest.cpp b/src/Tests/HealthSystemTest.cpp new file mode 100644 index 00000000..7ba2b89d --- /dev/null +++ b/src/Tests/HealthSystemTest.cpp @@ -0,0 +1,114 @@ +#include +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"); + ResourceManager::RegisterType("EntityXMLFile"); + + m_Config = ResourceManager::Load("Config.ini"); + LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get("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("Debug.LoadMap", ""); + if (!mapToLoad.empty()) { + ResourceManager::Load(mapToLoad)->PopulateWorld(m_World); + } + + // Create system pipeline + m_SystemPipeline = new SystemPipeline(m_EventBroker); + m_SystemPipeline->AddSystem(); + m_SystemPipeline->AddSystem(); + + //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; +} diff --git a/src/Tests/HealthSystemTest.h b/src/Tests/HealthSystemTest.h new file mode 100644 index 00000000..664d2ef3 --- /dev/null +++ b/src/Tests/HealthSystemTest.h @@ -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