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:
@@ -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();
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ enum class MessageType
|
|||||||
PlayerTransform,
|
PlayerTransform,
|
||||||
OnDoubleJump,
|
OnDoubleJump,
|
||||||
ServerlistRequest,
|
ServerlistRequest,
|
||||||
|
AmmoPickup,
|
||||||
Invalid
|
Invalid
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -3,37 +3,43 @@
|
|||||||
AmmoPickupSystem::AmmoPickupSystem(SystemParams params)
|
AmmoPickupSystem::AmmoPickupSystem(SystemParams params)
|
||||||
: System(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)
|
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;
|
||||||
if (ammoPickupPosition.DecreaseThisRespawnTimer < 0.0) {
|
if (ammoPickupPosition.DecreaseThisRespawnTimer < 0.0) {
|
||||||
//spawn and delete the vector item
|
//spawn and delete the vector item
|
||||||
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/AmmoPickup.xml");
|
auto entityFile = ResourceManager::Load<EntityFile>("Schema/Entities/AmmoPickup.xml");
|
||||||
EntityFileParser parser(entityFile);
|
EntityFileParser parser(entityFile);
|
||||||
EntityID ammoPickupID = parser.MergeEntities(m_World);
|
EntityID ammoPickupID = parser.MergeEntities(m_World);
|
||||||
|
|
||||||
//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.Pickup = EntityWrapper(m_World, ammoPickupID);
|
ePickupSpawned.Pickup = EntityWrapper(m_World, ammoPickupID);
|
||||||
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& newAmmoPickupEntity = EntityWrapper(m_World, ammoPickupID);
|
auto& newAmmoPickupEntity = EntityWrapper(m_World, ammoPickupID);
|
||||||
newAmmoPickupEntity["Transform"]["Position"] = ammoPickupPosition.Pos;
|
newAmmoPickupEntity["Transform"]["Position"] = ammoPickupPosition.Pos;
|
||||||
newAmmoPickupEntity["AmmoPickup"]["AmmoGain"] = ammoPickupPosition.AmmoGain;
|
newAmmoPickupEntity["AmmoPickup"]["AmmoGain"] = ammoPickupPosition.AmmoGain;
|
||||||
newAmmoPickupEntity["AmmoPickup"]["RespawnTimer"] = ammoPickupPosition.RespawnTimer;
|
newAmmoPickupEntity["AmmoPickup"]["RespawnTimer"] = ammoPickupPosition.RespawnTimer;
|
||||||
m_World->SetParent(newAmmoPickupEntity.ID, ammoPickupPosition.parentID);
|
m_World->SetParent(newAmmoPickupEntity.ID, ammoPickupPosition.parentID);
|
||||||
|
|
||||||
//erase the current element (AmmoPickupPosition)
|
//erase the current element (AmmoPickupPosition)
|
||||||
m_ETriggerTouchVector.erase(it);
|
m_ETriggerTouchVector.erase(it);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,7 +47,11 @@ void AmmoPickupSystem::Update(double dt)
|
|||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,37 +3,40 @@
|
|||||||
PickupSpawnSystem::PickupSpawnSystem(SystemParams params)
|
PickupSpawnSystem::PickupSpawnSystem(SystemParams params)
|
||||||
: System(params)
|
: System(params)
|
||||||
{
|
{
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_ETriggerTouch, &PickupSpawnSystem::OnTriggerTouch);
|
if (IsServer) {
|
||||||
|
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;
|
||||||
if (healthPickupPosition.DecreaseThisRespawnTimer < 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);
|
||||||
EntityID healthPickupID = parser.MergeEntities(m_World);
|
EntityID healthPickupID = parser.MergeEntities(m_World);
|
||||||
|
|
||||||
//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.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"] = healthPickupPosition.Pos;
|
newHealthPickupEntity["Transform"]["Position"] = healthPickupPosition.Pos;
|
||||||
newHealthPickupEntity["HealthPickup"]["HealthGain"] = healthPickupPosition.HealthGain;
|
newHealthPickupEntity["HealthPickup"]["HealthGain"] = healthPickupPosition.HealthGain;
|
||||||
newHealthPickupEntity["HealthPickup"]["RespawnTimer"] = healthPickupPosition.RespawnTimer;
|
newHealthPickupEntity["HealthPickup"]["RespawnTimer"] = healthPickupPosition.RespawnTimer;
|
||||||
m_World->SetParent(newHealthPickupEntity.ID, healthPickupPosition.parentID);
|
m_World->SetParent(newHealthPickupEntity.ID, healthPickupPosition.parentID);
|
||||||
|
|
||||||
//erase the current element (healthPickupPosition)
|
//erase the current element (healthPickupPosition)
|
||||||
m_ETriggerTouchVector.erase(it);
|
m_ETriggerTouchVector.erase(it);
|
||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user