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
+3 -1
View File
@@ -26,6 +26,7 @@
#include "Network/EInterpolate.h"
#include "Network/SnapshotFilter.h"
#include "Core/EPlayerSpawned.h"
#include "Core/EAmmoPickup.h"
#include "Network/ESearchForServers.h"
struct ServerInfo
@@ -106,7 +107,8 @@ private:
void parsePlayerDamage(Packet& packet);
void parseComponentDeletion(Packet& packet);
void parseDoubleJump(Packet& packet);
void InterpolateFields(Packet & packet, const ComponentInfo & componentInfo, const EntityID & entityID, const std::string & componentType);
void parseAmmoPickup(Packet& packet);
void InterpolateFields(Packet& packet, const ComponentInfo & componentInfo, const EntityID & entityID, const std::string & componentType);
void parseSnapshot(Packet& packet);
void identifyPacketLoss();
void hasServerTimedOut();
+1
View File
@@ -21,6 +21,7 @@ enum class MessageType
PlayerTransform,
OnDoubleJump,
ServerlistRequest,
AmmoPickup,
Invalid
};
+4 -1
View File
@@ -20,6 +20,7 @@
#include "../Game/Events/EDoubleJump.h"
#include "Core/EEntityDeleted.h"
#include "Core/EComponentDeleted.h"
#include "Core/EAmmoPickup.h"
class Server : public Network
{
@@ -88,7 +89,7 @@ private:
void parseServerlistRequest(boost::asio::ip::udp::endpoint endpoint);
bool shouldSendToClient(EntityWrapper childEntity);
// Debug event
// Events
EventRelay<Server, Events::InputCommand> m_EInputCommand;
bool OnInputCommand(const Events::InputCommand& e);
EventRelay<Server, Events::PlayerSpawned> m_EPlayerSpawned;
@@ -99,6 +100,8 @@ private:
bool OnComponentDeleted(const Events::ComponentDeleted& e);
EventRelay<Server, Events::PlayerDamage> m_EPlayerDamage;
bool OnPlayerDamage(const Events::PlayerDamage& e);
EventRelay<Server, Events::AmmoPickup> m_EAmmoPickup;
bool OnAmmoPickup(const Events::AmmoPickup& e);
};
#endif
+2
View File
@@ -20,6 +20,8 @@ public:
private:
EventRelay<AmmoPickupSystem, Events::TriggerTouch> m_ETriggerTouch;
bool OnTriggerTouch(Events::TriggerTouch& e);
EventRelay<AmmoPickupSystem, Events::AmmoPickup> m_EAmmoPickup;
bool OnAmmoPickup(Events::AmmoPickup& e);
struct NewAmmoPickup {
glm::vec3 Pos;
+11
View File
@@ -143,6 +143,9 @@ void Client::parseMessageType(Packet& packet)
case MessageType::OnDoubleJump:
parseDoubleJump(packet);
break;
case MessageType::AmmoPickup:
parseAmmoPickup(packet);
break;
default:
break;
}
@@ -294,6 +297,14 @@ void Client::parseDoubleJump(Packet & packet)
}
}
void Client::parseAmmoPickup(Packet & packet)
{
Events::AmmoPickup e;
e.AmmoGain = packet.ReadPrimitive<int>();
e.Player = m_LocalPlayer;
m_EventBroker->Publish(e);
}
void Client::updateFields(Packet& packet, const ComponentInfo& componentInfo, const EntityID& entityID)
{
for (auto field : componentInfo.FieldsInOrder) {
+18 -3
View File
@@ -13,7 +13,7 @@ Server::Server(World* world, EventBroker* eventBroker, int port)
EVENT_SUBSCRIBE_MEMBER(m_EEntityDeleted, &Server::OnEntityDeleted);
EVENT_SUBSCRIBE_MEMBER(m_EComponentDeleted, &Server::OnComponentDeleted);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &Server::OnPlayerDamage);
EVENT_SUBSCRIBE_MEMBER(m_EAmmoPickup, &Server::OnAmmoPickup);
// BindWW
if (port == 0) {
port = config->Get<float>("Networking.Port", 27666);
@@ -510,6 +510,19 @@ bool Server::OnPlayerDamage(const Events::PlayerDamage& e)
return true;
}
bool Server::OnAmmoPickup(const Events::AmmoPickup & e)
{
for (auto& kv : m_ConnectedPlayers) {
if (e.Player.ID == kv.second.EntityID) {
Packet packet(MessageType::AmmoPickup);
// We dont send playerID as it will be set at client to local
packet.WritePrimitive(e.AmmoGain);
m_Reliable.Send(packet, kv.second);
}
}
return true;
}
void Server::parseClientPing()
{
LOG_INFO("%i: Parsing ping", m_PacketID);
@@ -604,12 +617,14 @@ bool Server::shouldSendToClient(EntityWrapper childEntity)
auto children = m_World->GetDirectChildren(childEntity.ID);
for (auto it = children.first; it != children.second; it++) {
EntityWrapper child(m_World, it->second);
if(child.HasComponent("CapturePoint")) {
if (child.HasComponent("CapturePoint") || child.HasComponent("HealthPickup")
|| child.HasComponent("AmmoPickup")) {
return true;
}
}
return childEntity.HasComponent("Player") || childEntity.FirstParentWithComponent("Player").Valid()
|| childEntity.HasComponent("CapturePoint");
|| childEntity.HasComponent("CapturePoint") || childEntity.HasComponent("HealthPickup")
|| childEntity.HasComponent("AmmoPickup");
}
PlayerID Server::GetPlayerIDFromEndpoint()
+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;
}
+29 -26
View File
@@ -3,37 +3,40 @@
PickupSpawnSystem::PickupSpawnSystem(SystemParams params)
: System(params)
{
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &PickupSpawnSystem::OnTriggerTouch);
if (IsServer) {
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &PickupSpawnSystem::OnTriggerTouch);
}
}
void PickupSpawnSystem::Update(double dt)
{
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 entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/HealthPickup.xml");
EntityFileParser parser(entityFile);
EntityID healthPickupID = parser.MergeEntities(m_World);
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 entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/HealthPickup.xml");
EntityFileParser parser(entityFile);
EntityID healthPickupID = parser.MergeEntities(m_World);
//let the world know a pickup has spawned (graphics effects, etc)
Events::PickupSpawned ePickupSpawned;
ePickupSpawned.Pickup = EntityWrapper(m_World, healthPickupID);
m_EventBroker->Publish(ePickupSpawned);
//let the world know a pickup has spawned (graphics effects, etc)
Events::PickupSpawned ePickupSpawned;
ePickupSpawned.Pickup = EntityWrapper(m_World, healthPickupID);
m_EventBroker->Publish(ePickupSpawned);
//set 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);
//set 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);
//erase the current element (healthPickupPosition)
m_ETriggerTouchVector.erase(it);
break;
//erase the current element (healthPickupPosition)
m_ETriggerTouchVector.erase(it);
break;
}
}
}
}
@@ -58,8 +61,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)
//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)e.Trigger["Transform"]["Position"], e.Trigger["HealthPickup"]["HealthGain"],
e.Trigger["HealthPickup"]["RespawnTimer"], e.Trigger["HealthPickup"]["RespawnTimer"], m_World->GetParent(e.Trigger.ID) });
//delete the healthpickup
m_World->DeleteEntity(e.Trigger.ID);