Modified HealthSystem so it actually publishes the DeathEvent. Modified EPlayerHealthPickup. Fixed 2 tests. Started work on PlayerDeathSystem

This commit is contained in:
verysecrethero
2016-01-27 14:46:45 +01:00
parent eb42217885
commit 5cdf6db340
10 changed files with 176 additions and 43 deletions
+1
View File
@@ -9,6 +9,7 @@ namespace Events
struct PlayerDamage : Event
{
//NOTE: this struct is missing information on what the damageSource is
EntityWrapper Player;
double Damage;
};
+2 -2
View File
@@ -2,15 +2,15 @@
#define EPlayerHealthPickup_h__
#include "EventBroker.h"
#include "../Core/Entity.h"
#include "../Core/EntityWrapper.h"
namespace Events
{
struct PlayerHealthPickup : Event
{
EntityWrapper Player;
double HealthAmount;
EntityID PlayerHealedID;
};
}
+1 -1
View File
@@ -26,7 +26,7 @@ private:
EventRelay<HealthSystem, Events::PlayerDamage> m_EPlayerDamage;
bool HealthSystem::OnPlayerDamaged(Events::PlayerDamage& e);
EventRelay<HealthSystem, Events::PlayerHealthPickup> m_EPlayerHealthPickup;
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e);
bool HealthSystem::OnPlayerHealthPickup(Events::PlayerHealthPickup& e);
//vector which will keep track of health changes
std::vector<std::tuple<EntityID, double>> m_DeltaHealthVector;
+39
View File
@@ -0,0 +1,39 @@
#ifndef PlayerDeathSystem_h__
#define PlayerDeathSystem_h__
#include "Core/System.h"
#include "Input/EInputCommand.h"
#include "Systems/SpawnerSystem.h"
#include "Events/ESpawnerSpawn.h"
#include "Core/EPlayerSpawned.h"
#include "Rendering/ESetCamera.h"
#include "Core/ConfigFile.h"
#include "Core/EPlayerDeath.h"
//tests
#include "Core/EPlayerDamage.h"
class PlayerDeathSystem : public ImpureSystem
{
public:
PlayerDeathSystem(World* world, EventBroker* eventBroker);
virtual void Update(double dt) override;
private:
struct SpawnRequest
{
int PlayerID;
ComponentInfo::EnumType Team;
};
bool m_NetworkEnabled = false;
std::vector<SpawnRequest> m_SpawnRequests;
std::map<int, EntityWrapper> m_PlayerEntities;
EventRelay<PlayerDeathSystem, Events::InputCommand> m_OnInputCommand;
bool OnInputCommand(const Events::InputCommand& e);
EventRelay<PlayerDeathSystem, Events::PlayerDeath> m_OnPlayerDeath;
bool OnPlayerDeath(Events::PlayerDeath& e);
};
#endif
+2 -2
View File
@@ -139,7 +139,7 @@
</Team>
</c:Team>
<c:Transform>
<Position X="57.7363472" Y="1.18853188" Z="79.5"/>
<Position X="57.7363472" Y="2.38900018" Z="79.5"/>
</c:Transform>
</Components>
<Children>
@@ -201,7 +201,7 @@
</Team>
</c:Team>
<c:Transform>
<Position X="-60.2567062" Y="1.60000002" Z="-78.1000061"/>
<Position X="-60.2567062" Y="3.4000001" Z="-78.1000061"/>
</c:Transform>
</Components>
<Children>
+2
View File
@@ -7,6 +7,7 @@
#include "Systems/PlayerMovementSystem.h"
#include "Systems/SpawnerSystem.h"
#include "Systems/PlayerSpawnSystem.h"
#include "Systems/PlayerDeathSystem.h"
#include "Core/EntityFileWriter.h"
#include "Game/Systems/CapturePointSystem.h"
#include "Game/Systems/WeaponSystem.h"
@@ -86,6 +87,7 @@ Game::Game(int argc, char* argv[])
m_SystemPipeline->AddSystem<InterpolationSystem>(updateOrderLevel);
m_SystemPipeline->AddSystem<SpawnerSystem>(updateOrderLevel);
m_SystemPipeline->AddSystem<PlayerSpawnSystem>(updateOrderLevel);
m_SystemPipeline->AddSystem<PlayerDeathSystem>(updateOrderLevel);
m_SystemPipeline->AddSystem<WeaponSystem>(updateOrderLevel, m_Renderer);
m_SystemPipeline->AddSystem<LifetimeSystem>(updateOrderLevel);
// Populate Octree with collidables
+10 -35
View File
@@ -11,38 +11,6 @@ HealthSystem::HealthSystem(World* m_World, EventBroker* eventBroker)
void HealthSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& component, double dt)
{
//if entityID of health is 9 then the players ID is also 9 (player,health are connected to the same entity)
double maxHealth = (double)component["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 we have a healthchange for the current player and health is greater than 0, then apply it
if (std::get<0>(deltaHP) == component.EntityID && (double)component["Health"] > 0.0f) {
//get the deltaHP value from the tuple and make sure you dont get more than maxHealth
double newHealth = std::min((double)component["Health"] + (double)std::get<1>(deltaHP), maxHealth);
component["Health"] = newHealth;
m_DeltaHealthVector.erase(m_DeltaHealthVector.begin() + i - 1);
//check if health is <= 0
if ((double)component["Health"] <= 0.0f) {
component["Health"] = 0.0;
//publish death event
Events::PlayerDeath e;
e.PlayerID = component.EntityID;
m_EventBroker->Publish(e);
//clear the remaining hpDeltas for the dead player
for (size_t j = m_DeltaHealthVector.size(); j > 0; j--)
{
if (std::get<0>(m_DeltaHealthVector[j - 1]) == component.EntityID)
m_DeltaHealthVector.erase(m_DeltaHealthVector.begin() + j - 1);
}
//delete the player and break the loop
m_World->DeleteEntity(entity.ID);
break;
}
}
}
}
bool HealthSystem::OnPlayerDamaged(Events::PlayerDamage& e)
@@ -52,16 +20,23 @@ bool HealthSystem::OnPlayerDamaged(Events::PlayerDamage& e)
health -= e.Damage;
if (health <= 0.0) {
Events::PlayerDeath ePlayerDeath;
ePlayerDeath.PlayerID = e.Player.ID;
m_EventBroker->Publish(ePlayerDeath);
m_World->DeleteEntity(e.Player.ID);
}
return true;
}
bool HealthSystem::OnPlayerHealthPickup(const Events::PlayerHealthPickup& e)
bool HealthSystem::OnPlayerHealthPickup(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));
ComponentWrapper cHealth = e.Player["Health"];
double& health = cHealth["Health"];
//NOTE: its possible to get more than MaxHealth health
health += e.HealthAmount;
return true;
}
+116
View File
@@ -0,0 +1,116 @@
#include "Systems/PlayerDeathSystem.h"
PlayerDeathSystem::PlayerDeathSystem(World* m_World, EventBroker* eventBroker)
: System(m_World, eventBroker)
{
EVENT_SUBSCRIBE_MEMBER(m_OnInputCommand, &PlayerDeathSystem::OnInputCommand);
EVENT_SUBSCRIBE_MEMBER(m_OnPlayerDeath, &PlayerDeathSystem::OnPlayerDeath);
m_NetworkEnabled = ResourceManager::Load<ConfigFile>("Config.ini")->Get("Networking.StartNetwork", false);
}
void PlayerDeathSystem::Update(double dt)
{
auto playerSpawns = m_World->GetComponents("PlayerSpawn");
if (playerSpawns == nullptr) {
return;
}
for (auto& req : m_SpawnRequests) {
for (auto& cPlayerSpawn : *playerSpawns) {
EntityWrapper spawner(m_World, cPlayerSpawn.EntityID);
if (!spawner.HasComponent("Spawner")) {
continue;
}
// If the spawner has a team affiliation, check it
if (spawner.HasComponent("Team")) {
if ((int)spawner["Team"]["Team"] != req.Team) {
continue;
}
}
// Spawn the player!
EntityWrapper player = SpawnerSystem::Spawn(spawner);
// Set the player team affiliation
player["Team"]["Team"] = req.Team;
// Publish a PlayerSpawned event
Events::PlayerSpawned e;
e.PlayerID = req.PlayerID;
e.Player = player;
e.Spawner = spawner;
m_EventBroker->Publish(e);
}
}
m_SpawnRequests.clear();
}
bool PlayerDeathSystem::OnInputCommand(const Events::InputCommand& e)
{
//testing: Jump -> playerdamage
if (e.Command != "Jump") {
return false;
}
// 0 = released
if (e.Value != 0) {
return false;
}
auto players = m_World->GetComponents("Player");
for (auto& cPlayer : *players) {
EntityWrapper player(m_World, cPlayer.EntityID);
Events::PlayerDamage e;
e.Player = player;
e.Damage = 50;
m_EventBroker->Publish(e);
}
return true;
}
bool PlayerDeathSystem::OnPlayerDeath(Events::PlayerDeath& e)
{
//// When a player is actually spawned (since the actual spawning is handled on the server)
//// Check if a player already exists
//if (m_PlayerEntities.count(e.PlayerID) != 0) {
// // TODO: Disallow infinite respawning here
// m_World->DeleteEntity(m_PlayerEntities[e.PlayerID].ID);
//}
//// Store the player for future reference
//m_PlayerEntities[e.PlayerID] = e.Player;
//// Set the camera to the correct entity
//EntityWrapper cameraEntity = e.Player.FirstChildByName("Camera");
//if (cameraEntity.Valid()) {
// Events::SetCamera e;
// e.CameraEntity = cameraEntity;
// m_EventBroker->Publish(e);
//}
//// HACK: Set the player model color to team color
//EntityWrapper playerModel = e.Player.FirstChildByName("PlayerModel");
//if (playerModel.Valid() && e.Player.HasComponent("Team")) {
// ComponentWrapper cTeam = e.Player["Team"];
// ComponentWrapper cModel = playerModel["Model"];
// if ((ComponentInfo::EnumType)cTeam["Team"] == cTeam["Team"].Enum("Red")) {
// cModel["Color"] = glm::vec3(1.f, 0.f, 0.f);
// } else if ((ComponentInfo::EnumType)cTeam["Team"] == cTeam["Team"].Enum("Blue")) {
// cModel["Color"] = glm::vec3(0.f, 0.25f, 1.f);
// }
//}
//// TODO: Set the player name to whatever
//EntityWrapper playerName = e.Player.FirstChildByName("PlayerName");
//if (playerName.Valid()) {
// playerName["Text"]["Content"] = e.PlayerName;
//}
return true;
}
+1 -1
View File
@@ -29,7 +29,7 @@ void RayTest(std::string fileName) {
Ray ray(glm::vec3(-50, 0, 0), glm::vec3(1, 0, 0));
//using a
here, else we have to init the renderingsystem
//here, else we have to init the renderingsystem
ResourceManager::RegisterType<RawModel>("RawModel");
auto unitBox = ResourceManager::Load<RawModel>(fileName);
BOOST_REQUIRE(unitBox != nullptr);
+2 -2
View File
@@ -69,8 +69,8 @@ GameHealthSystemTest::GameHealthSystemTest()
m_EventBroker->Publish(e3);
//damage player with 50
Events::PlayerDamage e;
e.DamageAmount = 50.0f;
e.PlayerDamagedID = healthsID;
e.Damage = 50.0f;
// e.PlayerDamagedID = healthsID;
m_EventBroker->Publish(e);
//heal some other player with 40
Events::PlayerHealthPickup e2;