Added the already-at-maxammo/health-save-trigger for the PickupSystems

This commit is contained in:
verysecrethero
2016-03-02 11:53:04 +01:00
parent 03a35d0f5e
commit 4652cd7861
4 changed files with 129 additions and 60 deletions
+9
View File
@@ -20,6 +20,9 @@ public:
private: private:
EventRelay<AmmoPickupSystem, Events::TriggerTouch> m_ETriggerTouch; EventRelay<AmmoPickupSystem, Events::TriggerTouch> m_ETriggerTouch;
bool OnTriggerTouch(Events::TriggerTouch& e); bool OnTriggerTouch(Events::TriggerTouch& e);
EventRelay<AmmoPickupSystem, Events::TriggerLeave> m_ETriggerLeave;
bool OnTriggerLeave(Events::TriggerLeave& e);
EventRelay<AmmoPickupSystem, Events::AmmoPickup> m_EAmmoPickup; EventRelay<AmmoPickupSystem, Events::AmmoPickup> m_EAmmoPickup;
bool OnAmmoPickup(Events::AmmoPickup& e); bool OnAmmoPickup(Events::AmmoPickup& e);
@@ -31,5 +34,11 @@ private:
EntityID parentID; EntityID parentID;
}; };
std::vector<NewAmmoPickup> m_ETriggerTouchVector; std::vector<NewAmmoPickup> m_ETriggerTouchVector;
struct EntityAtMaxValuePickupStruct {
EntityWrapper player;
EntityWrapper trigger;
};
std::vector<EntityAtMaxValuePickupStruct> m_PickupAtMaximum;
void DoPickup(EntityWrapper &player, EntityWrapper &trigger);
}; };
#endif #endif
+8 -1
View File
@@ -9,7 +9,6 @@
#include "Core/EPlayerHealthPickup.h" #include "Core/EPlayerHealthPickup.h"
#include "Engine/Collision/ETrigger.h" #include "Engine/Collision/ETrigger.h"
#include "Common.h" #include "Common.h"
#include <tuple>
class PickupSpawnSystem : public ImpureSystem class PickupSpawnSystem : public ImpureSystem
{ {
@@ -21,6 +20,8 @@ public:
private: private:
EventRelay<PickupSpawnSystem, Events::TriggerTouch> m_ETriggerTouch; EventRelay<PickupSpawnSystem, Events::TriggerTouch> m_ETriggerTouch;
bool OnTriggerTouch(Events::TriggerTouch& e); bool OnTriggerTouch(Events::TriggerTouch& e);
EventRelay<PickupSpawnSystem, Events::TriggerLeave> m_ETriggerLeave;
bool OnTriggerLeave(Events::TriggerLeave& e);
struct NewHealthPickup { struct NewHealthPickup {
glm::vec3 Pos; glm::vec3 Pos;
@@ -30,5 +31,11 @@ private:
EntityID parentID; EntityID parentID;
}; };
std::vector<NewHealthPickup> m_ETriggerTouchVector; std::vector<NewHealthPickup> m_ETriggerTouchVector;
struct EntityAtMaxValuePickupStruct {
EntityWrapper player;
EntityWrapper trigger;
};
std::vector<EntityAtMaxValuePickupStruct> m_PickupAtMaximum;
void DoPickup(EntityWrapper &player, EntityWrapper &trigger);
}; };
#endif #endif
+63 -38
View File
@@ -5,6 +5,7 @@ AmmoPickupSystem::AmmoPickupSystem(SystemParams params)
{ {
if (IsServer) { if (IsServer) {
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &AmmoPickupSystem::OnTriggerTouch); EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &AmmoPickupSystem::OnTriggerTouch);
EVENT_SUBSCRIBE_MEMBER(m_ETriggerLeave, &AmmoPickupSystem::OnTriggerLeave);
} }
if (IsClient) { if (IsClient) {
EVENT_SUBSCRIBE_MEMBER(m_EAmmoPickup, &AmmoPickupSystem::OnAmmoPickup); EVENT_SUBSCRIBE_MEMBER(m_EAmmoPickup, &AmmoPickupSystem::OnAmmoPickup);
@@ -15,42 +16,47 @@ void AmmoPickupSystem::Update(double dt)
{ {
if (IsServer) { if (IsServer) {
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) { for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) {
auto& ammoPickupPosition = *it; auto& somePickup = *it;
//set the double timer value (value 3) somePickup.DecreaseThisRespawnTimer -= dt;
ammoPickupPosition.DecreaseThisRespawnTimer -= dt; if (somePickup.DecreaseThisRespawnTimer < 0.0) {
if (ammoPickupPosition.DecreaseThisRespawnTimer < 0.0) {
//spawn and delete the vector item
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/AmmoPickup.xml"); auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/AmmoPickup.xml");
EntityFileParser parser(entityFile); EntityFileParser parser(entityFile);
EntityID ammoPickupID = parser.MergeEntities(m_World); EntityID ammoPickupID = parser.MergeEntities(m_World);
//let the world know a pickup has spawned (graphics effects, etc) //let the world know a pickup has spawned
Events::PickupSpawned ePickupSpawned; Events::PickupSpawned ePickupSpawned;
ePickupSpawned.Pickup = EntityWrapper(m_World, ammoPickupID); ePickupSpawned.Pickup = EntityWrapper(m_World, ammoPickupID);
m_EventBroker->Publish(ePickupSpawned); m_EventBroker->Publish(ePickupSpawned);
//set values from the old entity to the new entity //copy values from the old entity to the new entity
auto& newAmmoPickupEntity = EntityWrapper(m_World, ammoPickupID); auto& newAmmoPickupEntity = EntityWrapper(m_World, ammoPickupID);
newAmmoPickupEntity["Transform"]["Position"] = ammoPickupPosition.Pos; newAmmoPickupEntity["Transform"]["Position"] = somePickup.Pos;
newAmmoPickupEntity["AmmoPickup"]["AmmoGain"] = ammoPickupPosition.AmmoGain; newAmmoPickupEntity["AmmoPickup"]["AmmoGain"] = somePickup.AmmoGain;
newAmmoPickupEntity["AmmoPickup"]["RespawnTimer"] = ammoPickupPosition.RespawnTimer; newAmmoPickupEntity["AmmoPickup"]["RespawnTimer"] = somePickup.RespawnTimer;
m_World->SetParent(newAmmoPickupEntity.ID, ammoPickupPosition.parentID); m_World->SetParent(newAmmoPickupEntity.ID, somePickup.parentID);
//erase the current element (AmmoPickupPosition) //erase the current element (somePickup)
m_ETriggerTouchVector.erase(it); m_ETriggerTouchVector.erase(it);
break; break;
} }
} }
//still touching m_PickupAtMaximum?
for (auto& it = m_PickupAtMaximum.begin(); it != m_PickupAtMaximum.end(); ++it) {
if (!it->player.Valid()) {
m_PickupAtMaximum.erase(it);
break;
}
if ((int)it->player["AssaultWeapon"]["Ammo"] < (int)it->player["AssaultWeapon"]["MaxAmmo"]) {
DoPickup(it->player, it->trigger);
m_PickupAtMaximum.erase(it);
break;
}
}
} }
} }
bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e) bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e)
{ {
/*if (e.Entity != LocalPlayer) {
return false;
}*/
if (!e.Entity.Valid()) { if (!e.Entity.Valid()) {
return false; return false;
} }
@@ -61,30 +67,13 @@ bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e)
if (!e.Trigger.HasComponent("AmmoPickup")) { if (!e.Trigger.HasComponent("AmmoPickup")) {
return false; return false;
} }
int maxWeaponAmmo = (int)e.Entity["AssaultWeapon"]["MaxAmmo"];
int& currentAmmo = (int)e.Entity["AssaultWeapon"]["Ammo"];
int ammoGiven = 0.01*(double)e.Trigger["AmmoPickup"]["AmmoGain"] * maxWeaponAmmo; //if at maxammo, save the trigger-touch to a vector since standing inside it will not re-trigger the trigger
//cant pick up ammopacks if you are already at MaxAmmo if ((int)e.Entity["AssaultWeapon"]["Ammo"] >= (int)e.Entity["AssaultWeapon"]["MaxAmmo"]) {
if (currentAmmo >= maxWeaponAmmo) { m_PickupAtMaximum.push_back({ e.Entity, e.Trigger });
return false; return false;
} }
DoPickup(e.Entity, e.Trigger);
//personEntered = e.Entity, thingEntered = e.Trigger
Events::AmmoPickup ePlayerAmmoPickup;
ePlayerAmmoPickup.AmmoGain = ammoGiven;
ePlayerAmmoPickup.Player = e.Entity;
m_EventBroker->Publish(ePlayerAmmoPickup);
//immediately give the player the ammo
//currentAmmo = std::min(currentAmmo + ammoGiven, maxWeaponAmmo);
//copy position, ammogain, respawntimer (twice since one of the values will be counted down to 0, the other will be set in the new object)
//we need to copy all values since each value can be different for each ammoPickup
m_ETriggerTouchVector.push_back({ e.Trigger["Transform"]["Position"], e.Trigger["AmmoPickup"]["AmmoGain"],
e.Trigger["AmmoPickup"]["RespawnTimer"], e.Trigger["AmmoPickup"]["RespawnTimer"], m_World->GetParent(e.Trigger.ID) });
//delete the ammopickup
m_World->DeleteEntity(e.Trigger.ID);
return true; return true;
} }
@@ -107,3 +96,39 @@ bool AmmoPickupSystem::OnAmmoPickup(Events::AmmoPickup & e)
currentAmmo = std::min(currentAmmo + e.AmmoGain, maxWeaponAmmo); currentAmmo = std::min(currentAmmo + e.AmmoGain, maxWeaponAmmo);
return false; return false;
} }
bool AmmoPickupSystem::OnTriggerLeave(Events::TriggerLeave& e) {
if (!e.Trigger.HasComponent("AmmoPickup")) {
return false;
}
//triggerleave erases possible m_PickupAtMaximum
for (auto& it = m_PickupAtMaximum.begin(); it != m_PickupAtMaximum.end(); ++it) {
if (it->trigger.ID == e.Trigger.ID && it->player.ID == e.Entity.ID) {
m_PickupAtMaximum.erase(it);
break;
}
}
return true;
}
void AmmoPickupSystem::DoPickup(EntityWrapper &player, EntityWrapper &trigger) {
int maxWeaponAmmo = (int)player["AssaultWeapon"]["MaxAmmo"];
int& currentAmmo = (int)player["AssaultWeapon"]["Ammo"];
int ammoGiven = 0.01*(double)trigger["AmmoPickup"]["AmmoGain"] * maxWeaponAmmo;
Events::AmmoPickup ePlayerAmmoPickup;
ePlayerAmmoPickup.AmmoGain = ammoGiven;
ePlayerAmmoPickup.Player = player;
m_EventBroker->Publish(ePlayerAmmoPickup);
//immediately give the player the ammo (on server)
currentAmmo = std::min(currentAmmo + ammoGiven, maxWeaponAmmo);
//copy position, ammogain, respawntimer (twice since one of the values will be counted down to 0, the other will be set in the new object)
//we need to copy all values since each value can be different for each ammoPickup
m_ETriggerTouchVector.push_back({ (glm::vec3)trigger["Transform"]["Position"], trigger["AmmoPickup"]["AmmoGain"],
trigger["AmmoPickup"]["RespawnTimer"], trigger["AmmoPickup"]["RespawnTimer"], m_World->GetParent(trigger.ID) });
//delete the ammopickup
m_World->DeleteEntity(trigger.ID);
}
+49 -21
View File
@@ -5,6 +5,7 @@ PickupSpawnSystem::PickupSpawnSystem(SystemParams params)
{ {
if (IsServer) { if (IsServer) {
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &PickupSpawnSystem::OnTriggerTouch); EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &PickupSpawnSystem::OnTriggerTouch);
EVENT_SUBSCRIBE_MEMBER(m_ETriggerLeave, &PickupSpawnSystem::OnTriggerLeave);
} }
} }
@@ -12,11 +13,10 @@ void PickupSpawnSystem::Update(double dt)
{ {
if (IsServer) { if (IsServer) {
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) { for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) {
auto& healthPickupPosition = *it; auto& somePickup = *it;
//set the double timer value (value 3) somePickup.DecreaseThisRespawnTimer -= dt;
healthPickupPosition.DecreaseThisRespawnTimer -= dt; if (somePickup.DecreaseThisRespawnTimer < 0.0) {
if (healthPickupPosition.DecreaseThisRespawnTimer < 0) { //spawn the new healthPickup
//spawn and delete the vector item
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/HealthPickup.xml"); auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/HealthPickup.xml");
EntityFileParser parser(entityFile); EntityFileParser parser(entityFile);
EntityID healthPickupID = parser.MergeEntities(m_World); EntityID healthPickupID = parser.MergeEntities(m_World);
@@ -26,45 +26,73 @@ void PickupSpawnSystem::Update(double dt)
ePickupSpawned.Pickup = EntityWrapper(m_World, healthPickupID); ePickupSpawned.Pickup = EntityWrapper(m_World, healthPickupID);
m_EventBroker->Publish(ePickupSpawned); m_EventBroker->Publish(ePickupSpawned);
//set values from the old entity to the new entity //copy values from the old entity to the new entity
auto& newHealthPickupEntity = EntityWrapper(m_World, healthPickupID); auto& newHealthPickupEntity = EntityWrapper(m_World, healthPickupID);
newHealthPickupEntity["Transform"]["Position"] = healthPickupPosition.Pos; newHealthPickupEntity["Transform"]["Position"] = somePickup.Pos;
newHealthPickupEntity["HealthPickup"]["HealthGain"] = healthPickupPosition.HealthGain; newHealthPickupEntity["HealthPickup"]["HealthGain"] = somePickup.HealthGain;
newHealthPickupEntity["HealthPickup"]["RespawnTimer"] = healthPickupPosition.RespawnTimer; newHealthPickupEntity["HealthPickup"]["RespawnTimer"] = somePickup.RespawnTimer;
m_World->SetParent(newHealthPickupEntity.ID, healthPickupPosition.parentID); m_World->SetParent(newHealthPickupEntity.ID, somePickup.parentID);
//erase the current element (healthPickupPosition) //erase the current element (somePickup)
m_ETriggerTouchVector.erase(it); m_ETriggerTouchVector.erase(it);
break; break;
} }
} }
//still touching PickupAtMaximum?
for (auto& it = m_PickupAtMaximum.begin(); it != m_PickupAtMaximum.end(); ++it) {
if (!it->player.Valid()) {
m_PickupAtMaximum.erase(it);
break;
}
if ((double)it->player["Health"]["Health"] < (double)it->player["Health"]["MaxHealth"]) {
DoPickup(it->player, it->trigger);
m_PickupAtMaximum.erase(it);
break;
}
}
} }
} }
bool PickupSpawnSystem::OnTriggerTouch(Events::TriggerTouch& e) bool PickupSpawnSystem::OnTriggerTouch(Events::TriggerTouch& e)
{ {
if (!e.Trigger.HasComponent("HealthPickup")) { if (!e.Trigger.HasComponent("HealthPickup")) {
return false; return false;
} }
double healthGiven = 0.01*(double)e.Trigger["HealthPickup"]["HealthGain"] * (double)e.Entity["Health"]["MaxHealth"]; //if at maxhealth, save the trigger-touch to a vector since standing inside it will not re-trigger the trigger
//cant pick up healthpacks if you are already at MaxHealth
if ((double)e.Entity["Health"]["Health"] >= (double)e.Entity["Health"]["MaxHealth"]) { if ((double)e.Entity["Health"]["Health"] >= (double)e.Entity["Health"]["MaxHealth"]) {
m_PickupAtMaximum.push_back({ e.Entity, e.Trigger });
return false; return false;
} }
//personEntered = e.Entity, thingEntered = e.Trigger DoPickup(e.Entity, e.Trigger);
return true;
}
bool PickupSpawnSystem::OnTriggerLeave(Events::TriggerLeave& e) {
if (!e.Trigger.HasComponent("HealthPickup")) {
return false;
}
//triggerleave erases possible m_PickupAtMaximum
for (auto& it = m_PickupAtMaximum.begin(); it != m_PickupAtMaximum.end(); ++it) {
if (it->trigger.ID == e.Trigger.ID && it->player.ID == e.Entity.ID) {
m_PickupAtMaximum.erase(it);
break;
}
}
return true;
}
void PickupSpawnSystem::DoPickup(EntityWrapper &player, EntityWrapper &trigger) {
double healthGiven = 0.01*(double)trigger["HealthPickup"]["HealthGain"] * (double)player["Health"]["MaxHealth"];
//only the server will increase the players hp and set it in the next delta
Events::PlayerHealthPickup ePlayerHealthPickup; Events::PlayerHealthPickup ePlayerHealthPickup;
ePlayerHealthPickup.HealthAmount = healthGiven; ePlayerHealthPickup.HealthAmount = healthGiven;
ePlayerHealthPickup.Player = e.Entity; ePlayerHealthPickup.Player = player;
m_EventBroker->Publish(ePlayerHealthPickup); m_EventBroker->Publish(ePlayerHealthPickup);
//copy position, healthgain, respawntimer (twice since one of the values will be counted down to 0, the other will be set in the new object) //copy position, healthgain, respawntimer (twice since one of the values will be counted down to 0, the other will be set in the new object)
//we need to copy all values since each value can be different for each healthPickup //we need to copy all values since each value can be different for each healthPickup
m_ETriggerTouchVector.push_back({ (glm::vec3)e.Trigger["Transform"]["Position"], e.Trigger["HealthPickup"]["HealthGain"], m_ETriggerTouchVector.push_back({ (glm::vec3)trigger["Transform"]["Position"] ,trigger["HealthPickup"]["HealthGain"],
e.Trigger["HealthPickup"]["RespawnTimer"], e.Trigger["HealthPickup"]["RespawnTimer"], m_World->GetParent(e.Trigger.ID) }); trigger["HealthPickup"]["RespawnTimer"],trigger["HealthPickup"]["RespawnTimer"], m_World->GetParent(trigger.ID) });
//delete the healthpickup //delete the healthpickup
m_World->DeleteEntity(e.Trigger.ID); m_World->DeleteEntity(trigger.ID);
return true;
} }