Refactoring: Changed HealthPickupSystem to use a struct instead. Removed the Id in EPickupSpawned

This commit is contained in:
verysecrethero
2016-02-05 18:05:46 +01:00
parent be628d9406
commit 4103c878ec
5 changed files with 71 additions and 17 deletions
-1
View File
@@ -9,7 +9,6 @@ namespace Events
struct PickupSpawned : Event struct PickupSpawned : Event
{ {
EntityID PickupID;
EntityWrapper Pickup; EntityWrapper Pickup;
}; };
+8 -2
View File
@@ -6,7 +6,7 @@
#include "Core/ResourceManager.h" #include "Core/ResourceManager.h"
#include "Core/EntityFileParser.h" #include "Core/EntityFileParser.h"
#include "Core/EPickupSpawned.h" #include "Core/EPickupSpawned.h"
#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> #include <tuple>
@@ -22,6 +22,12 @@ private:
EventRelay<PickupSpawnSystem, Events::TriggerTouch> m_ETriggerTouch; EventRelay<PickupSpawnSystem, Events::TriggerTouch> m_ETriggerTouch;
bool OnTriggerTouch(Events::TriggerTouch& e); bool OnTriggerTouch(Events::TriggerTouch& e);
std::vector<std::tuple<glm::vec3,double,double,double>> m_ETriggerTouchVector; struct NewHealthPickup {
glm::vec3 Pos;
double HealthGain;
double RespawnTimer;
double DecreaseThisRespawnTimer;
};
std::vector<NewHealthPickup> m_ETriggerTouchVector;
}; };
#endif #endif
+1 -1
View File
@@ -8,7 +8,7 @@
<Resource>Models/Core/UnitSphere.mesh</Resource> <Resource>Models/Core/UnitSphere.mesh</Resource>
</c:Model> </c:Model>
<c:Transform> <c:Transform>
<Position X="60" Y="1" Z="80"/> <Position X="0" Y="1" Z="0"/>
</c:Transform> </c:Transform>
<c:Trigger/> <c:Trigger/>
</Components> </Components>
@@ -238,6 +238,57 @@
</Entity> </Entity>
</Children> </Children>
</Entity> </Entity>
<Entity>
<Components>
<c:AABB/>
<c:HealthPickup>
<RespawnTimer>1</RespawnTimer>
<HealthGain>22</HealthGain>
</c:HealthPickup>
<c:Model>
<Resource>Models/Core/UnitSphere.mesh</Resource>
</c:Model>
<c:Transform>
<Position X="60" Y="1" Z="80"/>
</c:Transform>
<c:Trigger/>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:AABB/>
<c:HealthPickup>
<RespawnTimer>1</RespawnTimer>
<HealthGain>22</HealthGain>
</c:HealthPickup>
<c:Model>
<Resource>Models/Core/UnitSphere.mesh</Resource>
</c:Model>
<c:Transform>
<Position X="60" Y="1" Z="80"/>
</c:Transform>
<c:Trigger/>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:AABB/>
<c:HealthPickup>
<RespawnTimer>4</RespawnTimer>
<HealthGain>44</HealthGain>
</c:HealthPickup>
<c:Model>
<Resource>Models/Core/UnitSphere.mesh</Resource>
</c:Model>
<c:Transform>
<Position X="60" Y="1" Z="80"/>
</c:Transform>
<c:Trigger/>
</Components>
<Children/>
</Entity>
</Children> </Children>
</Entity> </Entity>
+11 -13
View File
@@ -8,10 +8,12 @@ PickupSpawnSystem::PickupSpawnSystem(World* m_World, EventBroker* eventBroker)
void PickupSpawnSystem::Update(double dt) void PickupSpawnSystem::Update(double dt)
{ {
for (auto &healthPickupPosition : m_ETriggerTouchVector) { for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it)
{
auto& healthPickupPosition = *it;
//set the double timer value (value 3) //set the double timer value (value 3)
std::get<3>(healthPickupPosition) -= dt; healthPickupPosition.DecreaseThisRespawnTimer -= dt;
if (std::get<3>(healthPickupPosition) < 0) { if (healthPickupPosition.DecreaseThisRespawnTimer < 0) {
//spawn and delete the vector item //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);
@@ -19,18 +21,17 @@ void PickupSpawnSystem::Update(double dt)
//let the world know a pickup has spawned (graphics effects, etc) //let the world know a pickup has spawned (graphics effects, etc)
Events::PickupSpawned ePickupSpawned; Events::PickupSpawned ePickupSpawned;
ePickupSpawned.PickupID = healthPickupID;
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 //set values from the old entity to the new entity
auto& newHealthPickupEntity = EntityWrapper(m_World, healthPickupID); auto& newHealthPickupEntity = EntityWrapper(m_World, healthPickupID);
newHealthPickupEntity["Transform"]["Position"] = std::get<0>(healthPickupPosition); newHealthPickupEntity["Transform"]["Position"] = healthPickupPosition.Pos;
newHealthPickupEntity["HealthPickup"]["HealthGain"] = std::get<1>(healthPickupPosition); newHealthPickupEntity["HealthPickup"]["HealthGain"] = healthPickupPosition.HealthGain;
newHealthPickupEntity["HealthPickup"]["RespawnTimer"] = std::get<2>(healthPickupPosition); newHealthPickupEntity["HealthPickup"]["RespawnTimer"] = healthPickupPosition.RespawnTimer;
//erase the current element (healthPickupPosition) //erase the current element (healthPickupPosition)
m_ETriggerTouchVector.erase(std::remove(m_ETriggerTouchVector.begin(), m_ETriggerTouchVector.end(), healthPickupPosition), m_ETriggerTouchVector.end()); m_ETriggerTouchVector.erase(it);
break; break;
} }
} }
@@ -50,11 +51,8 @@ bool PickupSpawnSystem::OnTriggerTouch(Events::TriggerTouch& e)
//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(std::make_tuple( m_ETriggerTouchVector.push_back({ (glm::vec3)e.Trigger["Transform"]["Position"] ,e.Trigger["HealthPickup"]["HealthGain"],
(glm::vec3)e.Trigger["Transform"]["Position"], e.Trigger["HealthPickup"]["RespawnTimer"],e.Trigger["HealthPickup"]["RespawnTimer"] });
e.Trigger["HealthPickup"]["HealthGain"],
e.Trigger["HealthPickup"]["RespawnTimer"],
e.Trigger["HealthPickup"]["RespawnTimer"]));
//delete the healthpickup //delete the healthpickup
m_World->DeleteEntity(e.Trigger.ID); m_World->DeleteEntity(e.Trigger.ID);