HealthSystemTest has been created. This runs a single test simple test with HealthEvents and HealthSystem. Also some minor changes

This commit is contained in:
verysecrethero
2016-01-07 14:29:04 +01:00
parent 638c236ceb
commit a1ed79dfbf
8 changed files with 173 additions and 33 deletions
+2
View File
@@ -11,6 +11,8 @@ struct PlayerDamage : Event
{
double DamageAmount;
EntityID PlayerDamagedID;
//optional TypeOfDamage
std::string TypeOfDamage;
};
}
+3 -1
View File
@@ -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;
};
}
+1 -2
View File
@@ -10,8 +10,7 @@ namespace Events
struct PlayerHealthPickup : Event
{
double HealthAmount;
EntityID HealthPickupID;
EntityID playerHealedID;
EntityID PlayerHealedID;
};
}
+2 -2
View File
@@ -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<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);
//create the vector which will keep track of health changes
//vector which will keep track of health changes
std::vector<std::tuple<EntityID, double>> m_DeltaHealthVector;
};
+1 -20
View File
@@ -60,26 +60,7 @@ Game::Game(int argc, char* argv[])
m_SystemPipeline->AddSystem<TriggerSystem>();
m_SystemPipeline->AddSystem<HealthSystem>();
//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<bool>("Networking.StartNetwork", false)) {
//boost::thread workerThread(&Game::networkFunction, this);
networkFunction();
+10 -8
View File
@@ -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;
}
+114
View File
@@ -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;
}
+40
View File
@@ -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