HealthSystem tested and working. Also the 3 new PlayerEvents seems to work. TODO: a proper test in the testclass
This commit is contained in:
@@ -9,8 +9,8 @@ namespace Events
|
|||||||
|
|
||||||
struct PlayerDamage : Event
|
struct PlayerDamage : Event
|
||||||
{
|
{
|
||||||
int DamageAmount;
|
double DamageAmount;
|
||||||
EntityID PlayerID;
|
EntityID PlayerDamagedID;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ namespace Events
|
|||||||
|
|
||||||
struct PlayerHealthPickup : Event
|
struct PlayerHealthPickup : Event
|
||||||
{
|
{
|
||||||
int HealthAmount;
|
double HealthAmount;
|
||||||
EntityID HealthPickupID;
|
EntityID HealthPickupID;
|
||||||
|
EntityID playerHealedID;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,23 +10,27 @@
|
|||||||
#include "Core\EPlayerHealthPickup.h";
|
#include "Core\EPlayerHealthPickup.h";
|
||||||
#include "Core\EPlayerDeath.h";
|
#include "Core\EPlayerDeath.h";
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class HealthSystem : public PureSystem
|
class HealthSystem : public PureSystem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HealthSystem(EventBroker* eventBroker);
|
HealthSystem(EventBroker* eventBroker);
|
||||||
|
|
||||||
virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt) override;
|
//updatecomponent
|
||||||
private:
|
virtual void UpdateComponent(World* world, ComponentWrapper& health, double dt) override;
|
||||||
float m_Speed = 5;
|
|
||||||
|
|
||||||
|
private:
|
||||||
//create the methods which will take care of specific events
|
//create the methods which will take care of specific events
|
||||||
EventRelay<HealthSystem, Events::PlayerDamage> m_EPlayerDamage;
|
EventRelay<HealthSystem, Events::PlayerDamage> m_EPlayerDamage;
|
||||||
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e);
|
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e);
|
||||||
EventRelay<HealthSystem, Events::PlayerHealthPickup> m_EPlayerHealthPickup;
|
EventRelay<HealthSystem, Events::PlayerHealthPickup> m_EPlayerHealthPickup;
|
||||||
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e);
|
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
|
#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>
|
||||||
|
|||||||
+21
-2
@@ -28,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)));
|
||||||
|
|
||||||
@@ -60,7 +60,26 @@ Game::Game(int argc, char* argv[])
|
|||||||
m_SystemPipeline->AddSystem<TriggerSystem>();
|
m_SystemPipeline->AddSystem<TriggerSystem>();
|
||||||
m_SystemPipeline->AddSystem<HealthSystem>();
|
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)) {
|
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
|
||||||
//boost::thread workerThread(&Game::networkFunction, this);
|
//boost::thread workerThread(&Game::networkFunction, this);
|
||||||
networkFunction();
|
networkFunction();
|
||||||
|
|||||||
+31
-15
@@ -1,4 +1,5 @@
|
|||||||
#include "HealthSystem.h"
|
#include "HealthSystem.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
HealthSystem::HealthSystem(EventBroker* eventBroker)
|
HealthSystem::HealthSystem(EventBroker* eventBroker)
|
||||||
: PureSystem(eventBroker, "Health")
|
: PureSystem(eventBroker, "Health")
|
||||||
@@ -6,33 +7,48 @@ HealthSystem::HealthSystem(EventBroker* eventBroker)
|
|||||||
//subscribe/listenTo playerdamage,healthpickup events with the eventbroker
|
//subscribe/listenTo playerdamage,healthpickup events with the eventbroker
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &HealthSystem::OnPlayerDamaged);
|
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &HealthSystem::OnPlayerDamaged);
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EPlayerHealthPickup, &HealthSystem::OnPlayerHealthPickup);
|
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
|
//if entityID of health is 9 then the players ID is also 9 (player,health are connected to the same entity)
|
||||||
player["Health"] += playerDeltaHealth;
|
ComponentWrapper player = world->GetComponent(health.EntityID, "Player");
|
||||||
playerDeltaHealth = 0;
|
double currentHealth = (double) world->GetComponent(health.EntityID, "Health")["Health"];
|
||||||
if (player["Health"] < 0) {
|
double maxHealth = (double)world->GetComponent(health.EntityID, "Health")["MaxHealth"];
|
||||||
//sendout/publish death event
|
|
||||||
|
//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;
|
Events::PlayerDeath e;
|
||||||
e.PlayerID = player.EntityID;
|
e.PlayerID = player.EntityID;
|
||||||
m_EventBroker->Publish(e);
|
m_EventBroker->Publish(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e)
|
bool HealthSystem::OnPlayerDamaged(const Events::PlayerDamage& e)
|
||||||
{
|
{
|
||||||
//vem skadades?
|
//save the changed HP to a vector. it will be taken care of in UpdateComponent
|
||||||
//antagligen spelaren själv
|
m_DeltaHealthVector.push_back(std::make_tuple(e.PlayerDamagedID, -e.DamageAmount));
|
||||||
//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
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e)
|
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e)
|
||||||
{
|
{
|
||||||
//vem tog upp hp?
|
//save the changed HP to a vector. it will be taken care of in UpdateComponent
|
||||||
playerDeltaHealth += e.HealthAmount;
|
m_DeltaHealthVector.push_back(std::make_tuple(e.playerHealedID, e.HealthAmount));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user