HealthSystem tested and working. Also the 3 new PlayerEvents seems to work. TODO: a proper test in the testclass

This commit is contained in:
verysecrethero
2016-01-06 16:38:50 +01:00
parent f67f22c82d
commit 638c236ceb
9 changed files with 85 additions and 25 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ namespace Events
struct PlayerDamage : Event
{
int DamageAmount;
EntityID PlayerID;
double DamageAmount;
EntityID PlayerDamagedID;
};
}
+2 -1
View File
@@ -9,8 +9,9 @@ namespace Events
struct PlayerHealthPickup : Event
{
int HealthAmount;
double HealthAmount;
EntityID HealthPickupID;
EntityID playerHealedID;
};
}
+9 -5
View File
@@ -10,23 +10,27 @@
#include "Core\EPlayerHealthPickup.h";
#include "Core\EPlayerDeath.h";
#include <tuple>
#include <vector>
class HealthSystem : public PureSystem
{
public:
HealthSystem(EventBroker* eventBroker);
virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt) override;
private:
float m_Speed = 5;
//updatecomponent
virtual void UpdateComponent(World* world, ComponentWrapper& health, double dt) override;
private:
//create the 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);
int playerDeltaHealth;
//create the vector which will keep track of health changes
std::vector<std::tuple<EntityID, double>> m_DeltaHealthVector;
};
#endif
+1
View File
@@ -8,4 +8,5 @@
<xs:include schemaLocation="Components/Player.xsd"/>
<xs:include schemaLocation="Components/AABB.xsd"/>
<xs:include schemaLocation="Components/Trigger.xsd"/>
<xs:include schemaLocation="Components/Health.xsd"/>
</xs:schema>
+4
View File
@@ -0,0 +1,4 @@
<c:Health>
<Health>100</Health>
<MaxHealth>100</MaxHealth>
</c:Health>
+14
View File
@@ -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>
+1
View File
@@ -15,6 +15,7 @@
<xs:element ref="c:Test" minOccurs="0"/>
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
<xs:element ref="c:Player" minOccurs="0"/>
<xs:element ref="c:Health" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
+21 -2
View File
@@ -28,7 +28,7 @@ Game::Game(int argc, char* argv[])
0,
m_Config->Get<int>("Video.Width", 1280),
m_Config->Get<int>("Video.Height", 720)
));
));
m_Renderer->Initialize();
m_Renderer->Camera()->SetFOV(glm::radians(m_Config->Get<float>("Video.FOV", 90.f)));
@@ -60,7 +60,26 @@ Game::Game(int argc, char* argv[])
m_SystemPipeline->AddSystem<TriggerSystem>();
m_SystemPipeline->AddSystem<HealthSystem>();
// Invoke network
//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
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
//boost::thread workerThread(&Game::networkFunction, this);
networkFunction();
+31 -15
View File
@@ -1,4 +1,5 @@
#include "HealthSystem.h"
#include <algorithm>
HealthSystem::HealthSystem(EventBroker* eventBroker)
: PureSystem(eventBroker, "Health")
@@ -6,33 +7,48 @@ HealthSystem::HealthSystem(EventBroker* eventBroker)
//subscribe/listenTo playerdamage,healthpickup events with the eventbroker
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &HealthSystem::OnPlayerDamaged);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerHealthPickup, &HealthSystem::OnPlayerHealthPickup);
playerDeltaHealth = 0;
}
void HealthSystem::UpdateComponent(World * world, ComponentWrapper & player, double dt)
void HealthSystem::UpdateComponent(World * world, ComponentWrapper & health, double dt)
{
//Health is only affected by pickup/shoot events
player["Health"] += playerDeltaHealth;
playerDeltaHealth = 0;
if (player["Health"] < 0) {
//sendout/publish death event
//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 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--)
{
auto deltaHP = m_DeltaHealthVector[i-1];
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);
}
}
currentHealth = (double)world->GetComponent(health.EntityID, "Health")["Health"];
if (currentHealth < 0.0f) {
//publish death event
Events::PlayerDeath e;
e.PlayerID = player.EntityID;
m_EventBroker->Publish(e);
}
}
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e)
{
//vem skadades?
//antagligen spelaren själv
//såna här events skickas av network te spelare som kan lyssna på / kolla på de och tar hand om sina egna +-hp endast
//just add that damage to a variable, which will later be taken care of by UpdateComponent
playerDeltaHealth -= e.DamageAmount;
//ev skicka ut playerdeath event
//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)
{
//vem tog upp hp?
playerDeltaHealth += e.HealthAmount;
//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;
}