From a1ed79dfbf0282dbc890c8609df96a0505b58573 Mon Sep 17 00:00:00 2001 From: verysecrethero Date: Thu, 7 Jan 2016 14:29:04 +0100 Subject: [PATCH] HealthSystemTest has been created. This runs a single test simple test with HealthEvents and HealthSystem. Also some minor changes --- include/Engine/Core/EPlayerDamage.h | 2 + include/Engine/Core/EPlayerDeath.h | 4 +- include/Engine/Core/EPlayerHealthPickup.h | 3 +- include/Game/HealthSystem.h | 4 +- src/Game/Game.cpp | 21 +--- src/Game/HealthSystem.cpp | 18 ++-- src/Tests/HealthSystemTest.cpp | 114 ++++++++++++++++++++++ src/Tests/HealthSystemTest.h | 40 ++++++++ 8 files changed, 173 insertions(+), 33 deletions(-) create mode 100644 src/Tests/HealthSystemTest.cpp create mode 100644 src/Tests/HealthSystemTest.h diff --git a/include/Engine/Core/EPlayerDamage.h b/include/Engine/Core/EPlayerDamage.h index e0f2acd7..87ad67aa 100644 --- a/include/Engine/Core/EPlayerDamage.h +++ b/include/Engine/Core/EPlayerDamage.h @@ -11,6 +11,8 @@ struct PlayerDamage : Event { double DamageAmount; EntityID PlayerDamagedID; + //optional TypeOfDamage + std::string TypeOfDamage; }; } diff --git a/include/Engine/Core/EPlayerDeath.h b/include/Engine/Core/EPlayerDeath.h index 278b11e6..00ede5ed 100644 --- a/include/Engine/Core/EPlayerDeath.h +++ b/include/Engine/Core/EPlayerDeath.h @@ -9,8 +9,10 @@ namespace Events struct PlayerDeath : Event { - std::string KilledBy; + //KilledBy,KilledByWhat is optional for now. It might be used later in the playerlog-system + EntityID KilledBy; EntityID PlayerID; + std::string KilledByWhat; }; } diff --git a/include/Engine/Core/EPlayerHealthPickup.h b/include/Engine/Core/EPlayerHealthPickup.h index e7d01a4e..f3158f92 100644 --- a/include/Engine/Core/EPlayerHealthPickup.h +++ b/include/Engine/Core/EPlayerHealthPickup.h @@ -10,8 +10,7 @@ namespace Events struct PlayerHealthPickup : Event { double HealthAmount; - EntityID HealthPickupID; - EntityID playerHealedID; + EntityID PlayerHealedID; }; } diff --git a/include/Game/HealthSystem.h b/include/Game/HealthSystem.h index c4ea4695..a836e797 100644 --- a/include/Game/HealthSystem.h +++ b/include/Game/HealthSystem.h @@ -22,13 +22,13 @@ public: virtual void UpdateComponent(World* world, ComponentWrapper& health, double dt) override; private: - //create the methods which will take care of specific events + //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); - //create the vector which will keep track of health changes + //vector which will keep track of health changes std::vector> m_DeltaHealthVector; }; diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 8f479e03..18a533d0 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -60,26 +60,7 @@ Game::Game(int argc, char* argv[]) m_SystemPipeline->AddSystem(); m_SystemPipeline->AddSystem(); - //TEMP TEST DEL LATER - //skapar entityn som har komponenterna transf,model,player,health i sig. dvs är en spelare - 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"); - Events::PlayerDamage e; - e.DamageAmount = 50.0f; - e.PlayerDamagedID = 9; - m_EventBroker->Publish(e); - Events::PlayerHealthPickup e2; - e2.HealthAmount = 40.0f; - e2.playerHealedID = 9; - m_EventBroker->Publish(e2); - - //END TEST - - // Invoke network + // Invoke network if (m_Config->Get("Networking.StartNetwork", false)) { //boost::thread workerThread(&Game::networkFunction, this); networkFunction(); diff --git a/src/Game/HealthSystem.cpp b/src/Game/HealthSystem.cpp index 9ab8f13a..188bfb5a 100644 --- a/src/Game/HealthSystem.cpp +++ b/src/Game/HealthSystem.cpp @@ -4,34 +4,36 @@ HealthSystem::HealthSystem(EventBroker* eventBroker) : PureSystem(eventBroker, "Health") { - //subscribe/listenTo playerdamage,healthpickup events with the eventbroker + //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) +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 currentHealth = (double) world->GetComponent(health.EntityID, "Health")["Health"]; + double currentHealth; double maxHealth = (double)world->GetComponent(health.EntityID, "Health")["MaxHealth"]; //process the DeltaHealthVector and change the entitys health accordingly - for (size_t i = m_DeltaHealthVector.size(); i >0; i--) + for (size_t i = m_DeltaHealthVector.size(); i > 0; i--) { - auto deltaHP = m_DeltaHealthVector[i-1]; + 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) { //re-read currentHealth for each iteration currentHealth = (double)world->GetComponent(health.EntityID, "Health")["Health"]; //get the deltaHP value from the tuple and make sure you dont get more than maxHealth double newHealth = std::min(currentHealth + (double)std::get<1>(deltaHP), maxHealth); health.SetProperty("Health", newHealth); - m_DeltaHealthVector.erase(m_DeltaHealthVector.begin()+i-1); + m_DeltaHealthVector.erase(m_DeltaHealthVector.begin() + i - 1); } } currentHealth = (double)world->GetComponent(health.EntityID, "Health")["Health"]; - if (currentHealth < 0.0f) { + //check if health is <= 0 + if (currentHealth <= 0.0f) { //publish death event Events::PlayerDeath e; e.PlayerID = player.EntityID; @@ -49,6 +51,6 @@ bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e) 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)); + 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