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
+49 -21
View File
@@ -5,6 +5,7 @@ PickupSpawnSystem::PickupSpawnSystem(SystemParams params)
{
if (IsServer) {
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) {
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) {
auto& healthPickupPosition = *it;
//set the double timer value (value 3)
healthPickupPosition.DecreaseThisRespawnTimer -= dt;
if (healthPickupPosition.DecreaseThisRespawnTimer < 0) {
//spawn and delete the vector item
auto& somePickup = *it;
somePickup.DecreaseThisRespawnTimer -= dt;
if (somePickup.DecreaseThisRespawnTimer < 0.0) {
//spawn the new healthPickup
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/HealthPickup.xml");
EntityFileParser parser(entityFile);
EntityID healthPickupID = parser.MergeEntities(m_World);
@@ -26,45 +26,73 @@ void PickupSpawnSystem::Update(double dt)
ePickupSpawned.Pickup = EntityWrapper(m_World, healthPickupID);
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);
newHealthPickupEntity["Transform"]["Position"] = healthPickupPosition.Pos;
newHealthPickupEntity["HealthPickup"]["HealthGain"] = healthPickupPosition.HealthGain;
newHealthPickupEntity["HealthPickup"]["RespawnTimer"] = healthPickupPosition.RespawnTimer;
m_World->SetParent(newHealthPickupEntity.ID, healthPickupPosition.parentID);
newHealthPickupEntity["Transform"]["Position"] = somePickup.Pos;
newHealthPickupEntity["HealthPickup"]["HealthGain"] = somePickup.HealthGain;
newHealthPickupEntity["HealthPickup"]["RespawnTimer"] = somePickup.RespawnTimer;
m_World->SetParent(newHealthPickupEntity.ID, somePickup.parentID);
//erase the current element (healthPickupPosition)
//erase the current element (somePickup)
m_ETriggerTouchVector.erase(it);
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)
{
if (!e.Trigger.HasComponent("HealthPickup")) {
return false;
}
double healthGiven = 0.01*(double)e.Trigger["HealthPickup"]["HealthGain"] * (double)e.Entity["Health"]["MaxHealth"];
//cant pick up healthpacks if you are already at MaxHealth
//if at maxhealth, save the trigger-touch to a vector since standing inside it will not re-trigger the trigger
if ((double)e.Entity["Health"]["Health"] >= (double)e.Entity["Health"]["MaxHealth"]) {
m_PickupAtMaximum.push_back({ e.Entity, e.Trigger });
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;
ePlayerHealthPickup.HealthAmount = healthGiven;
ePlayerHealthPickup.Player = e.Entity;
ePlayerHealthPickup.Player = player;
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)
//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"],
e.Trigger["HealthPickup"]["RespawnTimer"], e.Trigger["HealthPickup"]["RespawnTimer"], m_World->GetParent(e.Trigger.ID) });
m_ETriggerTouchVector.push_back({ (glm::vec3)trigger["Transform"]["Position"] ,trigger["HealthPickup"]["HealthGain"],
trigger["HealthPickup"]["RespawnTimer"],trigger["HealthPickup"]["RespawnTimer"], m_World->GetParent(trigger.ID) });
//delete the healthpickup
m_World->DeleteEntity(e.Trigger.ID);
return true;
m_World->DeleteEntity(trigger.ID);
}