Changed m_ETriggerTouchVector for-loop so it wont break when an item triggers the condition. This is done to update all somePickup.DecreaseThisRespawnTimer's.

This commit is contained in:
Jocke
2016-03-02 15:31:42 +01:00
parent 4652cd7861
commit 9161c11764
2 changed files with 17 additions and 10 deletions
+6 -3
View File
@@ -15,7 +15,8 @@ AmmoPickupSystem::AmmoPickupSystem(SystemParams params)
void AmmoPickupSystem::Update(double dt) void AmmoPickupSystem::Update(double dt)
{ {
if (IsServer) { if (IsServer) {
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) { auto it = m_ETriggerTouchVector.begin();
while (it != m_ETriggerTouchVector.end()) {
auto& somePickup = *it; auto& somePickup = *it;
somePickup.DecreaseThisRespawnTimer -= dt; somePickup.DecreaseThisRespawnTimer -= dt;
if (somePickup.DecreaseThisRespawnTimer < 0.0) { if (somePickup.DecreaseThisRespawnTimer < 0.0) {
@@ -36,8 +37,10 @@ void AmmoPickupSystem::Update(double dt)
m_World->SetParent(newAmmoPickupEntity.ID, somePickup.parentID); m_World->SetParent(newAmmoPickupEntity.ID, somePickup.parentID);
//erase the current element (somePickup) //erase the current element (somePickup)
m_ETriggerTouchVector.erase(it); it = m_ETriggerTouchVector.erase(it);
break; }
else {
it++;
} }
} }
//still touching m_PickupAtMaximum? //still touching m_PickupAtMaximum?
+11 -7
View File
@@ -12,7 +12,8 @@ PickupSpawnSystem::PickupSpawnSystem(SystemParams params)
void PickupSpawnSystem::Update(double dt) void PickupSpawnSystem::Update(double dt)
{ {
if (IsServer) { if (IsServer) {
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) { auto it = m_ETriggerTouchVector.begin();
while (it != m_ETriggerTouchVector.end()) {
auto& somePickup = *it; auto& somePickup = *it;
somePickup.DecreaseThisRespawnTimer -= dt; somePickup.DecreaseThisRespawnTimer -= dt;
if (somePickup.DecreaseThisRespawnTimer < 0.0) { if (somePickup.DecreaseThisRespawnTimer < 0.0) {
@@ -34,8 +35,9 @@ void PickupSpawnSystem::Update(double dt)
m_World->SetParent(newHealthPickupEntity.ID, somePickup.parentID); m_World->SetParent(newHealthPickupEntity.ID, somePickup.parentID);
//erase the current element (somePickup) //erase the current element (somePickup)
m_ETriggerTouchVector.erase(it); it = m_ETriggerTouchVector.erase(it);
break; } else {
it++;
} }
} }
//still touching PickupAtMaximum? //still touching PickupAtMaximum?
@@ -66,7 +68,8 @@ bool PickupSpawnSystem::OnTriggerTouch(Events::TriggerTouch& e)
DoPickup(e.Entity, e.Trigger); DoPickup(e.Entity, e.Trigger);
return true; return true;
} }
bool PickupSpawnSystem::OnTriggerLeave(Events::TriggerLeave& e) { bool PickupSpawnSystem::OnTriggerLeave(Events::TriggerLeave& e)
{
if (!e.Trigger.HasComponent("HealthPickup")) { if (!e.Trigger.HasComponent("HealthPickup")) {
return false; return false;
} }
@@ -79,7 +82,8 @@ bool PickupSpawnSystem::OnTriggerLeave(Events::TriggerLeave& e) {
} }
return true; return true;
} }
void PickupSpawnSystem::DoPickup(EntityWrapper &player, EntityWrapper &trigger) { void PickupSpawnSystem::DoPickup(EntityWrapper &player, EntityWrapper &trigger)
{
double healthGiven = 0.01*(double)trigger["HealthPickup"]["HealthGain"] * (double)player["Health"]["MaxHealth"]; 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 //only the server will increase the players hp and set it in the next delta
@@ -90,8 +94,8 @@ void PickupSpawnSystem::DoPickup(EntityWrapper &player, EntityWrapper &trigger)
//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)trigger["Transform"]["Position"] ,trigger["HealthPickup"]["HealthGain"], m_ETriggerTouchVector.push_back({ (glm::vec3)trigger["Transform"]["Position"], trigger["HealthPickup"]["HealthGain"],
trigger["HealthPickup"]["RespawnTimer"],trigger["HealthPickup"]["RespawnTimer"], m_World->GetParent(trigger.ID) }); trigger["HealthPickup"]["RespawnTimer"], trigger["HealthPickup"]["RespawnTimer"], m_World->GetParent(trigger.ID) });
//delete the healthpickup //delete the healthpickup
m_World->DeleteEntity(trigger.ID); m_World->DeleteEntity(trigger.ID);