Added Healthrelated events and start of HealthSystem, also reverted the crash "fix"
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
#ifndef EPlayerDamage_h__
|
||||
#define EPlayerDamage_h__
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "../Core/Entity.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
struct PlayerDamage : Event
|
||||
{
|
||||
int DamageAmount;
|
||||
EntityID PlayerID;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef EPlayerDeath_h__
|
||||
#define EPlayerDeath_h__
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "../Core/Entity.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
struct PlayerDeath : Event
|
||||
{
|
||||
std::string KilledBy;
|
||||
EntityID PlayerID;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef EPlayerHealthPickup_h__
|
||||
#define EPlayerHealthPickup_h__
|
||||
|
||||
#include "EventBroker.h"
|
||||
#include "../Core/Entity.h"
|
||||
|
||||
namespace Events
|
||||
{
|
||||
|
||||
struct PlayerHealthPickup : Event
|
||||
{
|
||||
int HealthAmount;
|
||||
EntityID HealthPickupID;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef HealthSystem_h__
|
||||
#define HealthSystem_h__
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/common.hpp>
|
||||
|
||||
#include "Common.h"
|
||||
#include "Core/System.h"
|
||||
#include "Core\EPlayerDamage.h";
|
||||
#include "Core\EPlayerHealthPickup.h";
|
||||
#include "Core\EPlayerDeath.h";
|
||||
|
||||
class HealthSystem : public PureSystem
|
||||
{
|
||||
public:
|
||||
HealthSystem(EventBroker* eventBroker);
|
||||
|
||||
virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt) override;
|
||||
private:
|
||||
float m_Speed = 5;
|
||||
|
||||
//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;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,7 +3,7 @@
|
||||
BaseEventRelay::~BaseEventRelay()
|
||||
{
|
||||
if (m_Broker != nullptr) {
|
||||
m_Broker->Unsubscribe(*this);
|
||||
m_Broker->Unsubscribe(*this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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[])
|
||||
{
|
||||
@@ -57,6 +58,7 @@ Game::Game(int argc, char* argv[])
|
||||
m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer);
|
||||
m_SystemPipeline->AddSystem<CollisionSystem>();
|
||||
m_SystemPipeline->AddSystem<TriggerSystem>();
|
||||
m_SystemPipeline->AddSystem<HealthSystem>();
|
||||
|
||||
// Invoke network
|
||||
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "HealthSystem.h"
|
||||
|
||||
HealthSystem::HealthSystem(EventBroker* eventBroker)
|
||||
: PureSystem(eventBroker, "Health")
|
||||
{
|
||||
//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)
|
||||
{
|
||||
//Health is only affected by pickup/shoot events
|
||||
player["Health"] += playerDeltaHealth;
|
||||
playerDeltaHealth = 0;
|
||||
if (player["Health"] < 0) {
|
||||
//sendout/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
|
||||
return true;
|
||||
}
|
||||
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e)
|
||||
{
|
||||
//vem tog upp hp?
|
||||
playerDeltaHealth += e.HealthAmount;
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user