AmmoPickups and HealthPickups will only spawn on the server. When a ammo pickup is taken the Server will send an Event to that

Client and it will update its ammo.
This commit is contained in:
Jocke
2016-03-01 17:55:00 +01:00
parent 6d68486c05
commit aadfdfdd90
8 changed files with 124 additions and 57 deletions
+56 -26
View File
@@ -3,37 +3,43 @@
AmmoPickupSystem::AmmoPickupSystem(SystemParams params)
: System(params)
{
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &AmmoPickupSystem::OnTriggerTouch);
if (IsServer) {
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &AmmoPickupSystem::OnTriggerTouch);
}
if (IsClient) {
EVENT_SUBSCRIBE_MEMBER(m_EAmmoPickup, &AmmoPickupSystem::OnAmmoPickup);
}
}
void AmmoPickupSystem::Update(double dt)
{
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it)
{
auto& ammoPickupPosition = *it;
//set the double timer value (value 3)
ammoPickupPosition.DecreaseThisRespawnTimer -= dt;
if (ammoPickupPosition.DecreaseThisRespawnTimer < 0.0) {
//spawn and delete the vector item
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/AmmoPickup.xml");
EntityFileParser parser(entityFile);
EntityID ammoPickupID = parser.MergeEntities(m_World);
if (IsServer) {
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) {
auto& ammoPickupPosition = *it;
//set the double timer value (value 3)
ammoPickupPosition.DecreaseThisRespawnTimer -= dt;
if (ammoPickupPosition.DecreaseThisRespawnTimer < 0.0) {
//spawn and delete the vector item
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/AmmoPickup.xml");
EntityFileParser parser(entityFile);
EntityID ammoPickupID = parser.MergeEntities(m_World);
//let the world know a pickup has spawned (graphics effects, etc)
Events::PickupSpawned ePickupSpawned;
ePickupSpawned.Pickup = EntityWrapper(m_World, ammoPickupID);
m_EventBroker->Publish(ePickupSpawned);
//let the world know a pickup has spawned (graphics effects, etc)
Events::PickupSpawned ePickupSpawned;
ePickupSpawned.Pickup = EntityWrapper(m_World, ammoPickupID);
m_EventBroker->Publish(ePickupSpawned);
//set values from the old entity to the new entity
auto& newAmmoPickupEntity = EntityWrapper(m_World, ammoPickupID);
newAmmoPickupEntity["Transform"]["Position"] = ammoPickupPosition.Pos;
newAmmoPickupEntity["AmmoPickup"]["AmmoGain"] = ammoPickupPosition.AmmoGain;
newAmmoPickupEntity["AmmoPickup"]["RespawnTimer"] = ammoPickupPosition.RespawnTimer;
m_World->SetParent(newAmmoPickupEntity.ID, ammoPickupPosition.parentID);
//set values from the old entity to the new entity
auto& newAmmoPickupEntity = EntityWrapper(m_World, ammoPickupID);
newAmmoPickupEntity["Transform"]["Position"] = ammoPickupPosition.Pos;
newAmmoPickupEntity["AmmoPickup"]["AmmoGain"] = ammoPickupPosition.AmmoGain;
newAmmoPickupEntity["AmmoPickup"]["RespawnTimer"] = ammoPickupPosition.RespawnTimer;
m_World->SetParent(newAmmoPickupEntity.ID, ammoPickupPosition.parentID);
//erase the current element (AmmoPickupPosition)
m_ETriggerTouchVector.erase(it);
break;
//erase the current element (AmmoPickupPosition)
m_ETriggerTouchVector.erase(it);
break;
}
}
}
}
@@ -41,7 +47,11 @@ void AmmoPickupSystem::Update(double dt)
bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e)
{
if (e.Entity != LocalPlayer) {
/*if (e.Entity != LocalPlayer) {
return false;
}*/
if (!e.Entity.Valid()) {
return false;
}
//TODO: add other weapontypes
@@ -66,7 +76,7 @@ bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e)
ePlayerAmmoPickup.Player = e.Entity;
m_EventBroker->Publish(ePlayerAmmoPickup);
//immediately give the player the ammo
currentAmmo = std::min(currentAmmo + ammoGiven, maxWeaponAmmo);
//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
@@ -77,3 +87,23 @@ bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e)
m_World->DeleteEntity(e.Trigger.ID);
return true;
}
bool AmmoPickupSystem::OnAmmoPickup(Events::AmmoPickup & e)
{
if (!e.Player.Valid()) {
return false;
}
//TODO: add other weapontypes
if (!e.Player.HasComponent("AssaultWeapon")) {
return false;
}
int maxWeaponAmmo = (int)e.Player["AssaultWeapon"]["MaxAmmo"];
int& currentAmmo = (int)e.Player["AssaultWeapon"]["Ammo"];
//cant pick up ammopacks if you are already at MaxAmmo
if (currentAmmo >= maxWeaponAmmo) {
return false;
}
currentAmmo = std::min(currentAmmo + e.AmmoGain, maxWeaponAmmo);
return false;
}