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/EInterpolate.h"
#include "Network/SnapshotFilter.h" #include "Network/SnapshotFilter.h"
#include "Core/EPlayerSpawned.h" #include "Core/EPlayerSpawned.h"
#include "Core/EAmmoPickup.h"
#include "Network/ESearchForServers.h" #include "Network/ESearchForServers.h"
struct ServerInfo struct ServerInfo
@@ -106,7 +107,8 @@ private:
void parsePlayerDamage(Packet& packet); void parsePlayerDamage(Packet& packet);
void parseComponentDeletion(Packet& packet); void parseComponentDeletion(Packet& packet);
void parseDoubleJump(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 parseSnapshot(Packet& packet);
void identifyPacketLoss(); void identifyPacketLoss();
void hasServerTimedOut(); void hasServerTimedOut();
+1
View File
@@ -21,6 +21,7 @@ enum class MessageType
PlayerTransform, PlayerTransform,
OnDoubleJump, OnDoubleJump,
ServerlistRequest, ServerlistRequest,
AmmoPickup,
Invalid Invalid
}; };
+4 -1
View File
@@ -20,6 +20,7 @@
#include "../Game/Events/EDoubleJump.h" #include "../Game/Events/EDoubleJump.h"
#include "Core/EEntityDeleted.h" #include "Core/EEntityDeleted.h"
#include "Core/EComponentDeleted.h" #include "Core/EComponentDeleted.h"
#include "Core/EAmmoPickup.h"
class Server : public Network class Server : public Network
{ {
@@ -88,7 +89,7 @@ private:
void parseServerlistRequest(boost::asio::ip::udp::endpoint endpoint); void parseServerlistRequest(boost::asio::ip::udp::endpoint endpoint);
bool shouldSendToClient(EntityWrapper childEntity); bool shouldSendToClient(EntityWrapper childEntity);
// Debug event // Events
EventRelay<Server, Events::InputCommand> m_EInputCommand; EventRelay<Server, Events::InputCommand> m_EInputCommand;
bool OnInputCommand(const Events::InputCommand& e); bool OnInputCommand(const Events::InputCommand& e);
EventRelay<Server, Events::PlayerSpawned> m_EPlayerSpawned; EventRelay<Server, Events::PlayerSpawned> m_EPlayerSpawned;
@@ -99,6 +100,8 @@ private:
bool OnComponentDeleted(const Events::ComponentDeleted& e); bool OnComponentDeleted(const Events::ComponentDeleted& e);
EventRelay<Server, Events::PlayerDamage> m_EPlayerDamage; EventRelay<Server, Events::PlayerDamage> m_EPlayerDamage;
bool OnPlayerDamage(const Events::PlayerDamage& e); bool OnPlayerDamage(const Events::PlayerDamage& e);
EventRelay<Server, Events::AmmoPickup> m_EAmmoPickup;
bool OnAmmoPickup(const Events::AmmoPickup& e);
}; };
#endif #endif
+2
View File
@@ -20,6 +20,8 @@ public:
private: private:
EventRelay<AmmoPickupSystem, Events::TriggerTouch> m_ETriggerTouch; EventRelay<AmmoPickupSystem, Events::TriggerTouch> m_ETriggerTouch;
bool OnTriggerTouch(Events::TriggerTouch& e); bool OnTriggerTouch(Events::TriggerTouch& e);
EventRelay<AmmoPickupSystem, Events::AmmoPickup> m_EAmmoPickup;
bool OnAmmoPickup(Events::AmmoPickup& e);
struct NewAmmoPickup { struct NewAmmoPickup {
glm::vec3 Pos; glm::vec3 Pos;
+11
View File
@@ -143,6 +143,9 @@ void Client::parseMessageType(Packet& packet)
case MessageType::OnDoubleJump: case MessageType::OnDoubleJump:
parseDoubleJump(packet); parseDoubleJump(packet);
break; break;
case MessageType::AmmoPickup:
parseAmmoPickup(packet);
break;
default: default:
break; 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) void Client::updateFields(Packet& packet, const ComponentInfo& componentInfo, const EntityID& entityID)
{ {
for (auto field : componentInfo.FieldsInOrder) { 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_EEntityDeleted, &Server::OnEntityDeleted);
EVENT_SUBSCRIBE_MEMBER(m_EComponentDeleted, &Server::OnComponentDeleted); EVENT_SUBSCRIBE_MEMBER(m_EComponentDeleted, &Server::OnComponentDeleted);
EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &Server::OnPlayerDamage); EVENT_SUBSCRIBE_MEMBER(m_EPlayerDamage, &Server::OnPlayerDamage);
EVENT_SUBSCRIBE_MEMBER(m_EAmmoPickup, &Server::OnAmmoPickup);
// BindWW // BindWW
if (port == 0) { if (port == 0) {
port = config->Get<float>("Networking.Port", 27666); port = config->Get<float>("Networking.Port", 27666);
@@ -510,6 +510,19 @@ bool Server::OnPlayerDamage(const Events::PlayerDamage& e)
return true; 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() void Server::parseClientPing()
{ {
LOG_INFO("%i: Parsing ping", m_PacketID); LOG_INFO("%i: Parsing ping", m_PacketID);
@@ -604,12 +617,14 @@ bool Server::shouldSendToClient(EntityWrapper childEntity)
auto children = m_World->GetDirectChildren(childEntity.ID); auto children = m_World->GetDirectChildren(childEntity.ID);
for (auto it = children.first; it != children.second; it++) { for (auto it = children.first; it != children.second; it++) {
EntityWrapper child(m_World, it->second); EntityWrapper child(m_World, it->second);
if(child.HasComponent("CapturePoint")) { if (child.HasComponent("CapturePoint") || child.HasComponent("HealthPickup")
|| child.HasComponent("AmmoPickup")) {
return true; return true;
} }
} }
return childEntity.HasComponent("Player") || childEntity.FirstParentWithComponent("Player").Valid() return childEntity.HasComponent("Player") || childEntity.FirstParentWithComponent("Player").Valid()
|| childEntity.HasComponent("CapturePoint"); || childEntity.HasComponent("CapturePoint") || childEntity.HasComponent("HealthPickup")
|| childEntity.HasComponent("AmmoPickup");
} }
PlayerID Server::GetPlayerIDFromEndpoint() PlayerID Server::GetPlayerIDFromEndpoint()
+34 -4
View File
@@ -3,13 +3,18 @@
AmmoPickupSystem::AmmoPickupSystem(SystemParams params) AmmoPickupSystem::AmmoPickupSystem(SystemParams params)
: System(params) : System(params)
{ {
if (IsServer) {
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &AmmoPickupSystem::OnTriggerTouch); EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &AmmoPickupSystem::OnTriggerTouch);
}
if (IsClient) {
EVENT_SUBSCRIBE_MEMBER(m_EAmmoPickup, &AmmoPickupSystem::OnAmmoPickup);
}
} }
void AmmoPickupSystem::Update(double dt) void AmmoPickupSystem::Update(double dt)
{ {
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) if (IsServer) {
{ for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) {
auto& ammoPickupPosition = *it; auto& ammoPickupPosition = *it;
//set the double timer value (value 3) //set the double timer value (value 3)
ammoPickupPosition.DecreaseThisRespawnTimer -= dt; ammoPickupPosition.DecreaseThisRespawnTimer -= dt;
@@ -36,12 +41,17 @@ void AmmoPickupSystem::Update(double dt)
break; break;
} }
} }
}
} }
bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e) bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e)
{ {
if (e.Entity != LocalPlayer) { /*if (e.Entity != LocalPlayer) {
return false;
}*/
if (!e.Entity.Valid()) {
return false; return false;
} }
//TODO: add other weapontypes //TODO: add other weapontypes
@@ -66,7 +76,7 @@ bool AmmoPickupSystem::OnTriggerTouch(Events::TriggerTouch& e)
ePlayerAmmoPickup.Player = e.Entity; ePlayerAmmoPickup.Player = e.Entity;
m_EventBroker->Publish(ePlayerAmmoPickup); m_EventBroker->Publish(ePlayerAmmoPickup);
//immediately give the player the ammo //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) //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 //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); m_World->DeleteEntity(e.Trigger.ID);
return true; 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;
}
+7 -4
View File
@@ -3,13 +3,15 @@
PickupSpawnSystem::PickupSpawnSystem(SystemParams params) PickupSpawnSystem::PickupSpawnSystem(SystemParams params)
: System(params) : System(params)
{ {
if (IsServer) {
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &PickupSpawnSystem::OnTriggerTouch); EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &PickupSpawnSystem::OnTriggerTouch);
}
} }
void PickupSpawnSystem::Update(double dt) void PickupSpawnSystem::Update(double dt)
{ {
for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) if (IsServer) {
{ for (auto it = m_ETriggerTouchVector.begin(); it != m_ETriggerTouchVector.end(); ++it) {
auto& healthPickupPosition = *it; auto& healthPickupPosition = *it;
//set the double timer value (value 3) //set the double timer value (value 3)
healthPickupPosition.DecreaseThisRespawnTimer -= dt; healthPickupPosition.DecreaseThisRespawnTimer -= dt;
@@ -36,6 +38,7 @@ void PickupSpawnSystem::Update(double dt)
break; 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) //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)e.Trigger["Transform"]["Position"] ,e.Trigger["HealthPickup"]["HealthGain"], 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) }); e.Trigger["HealthPickup"]["RespawnTimer"], e.Trigger["HealthPickup"]["RespawnTimer"], m_World->GetParent(e.Trigger.ID) });
//delete the healthpickup //delete the healthpickup
m_World->DeleteEntity(e.Trigger.ID); m_World->DeleteEntity(e.Trigger.ID);