Server now broadcasts certain input commands to all other clients (for stuff like shooting)

This commit is contained in:
2016-02-09 19:37:42 +01:00
parent d5647ce89c
commit d83190290f
6 changed files with 48 additions and 22 deletions
+2
View File
@@ -48,6 +48,7 @@ private:
float snapshotInterval;
int checkTimeOutInterval = 100;
int m_NextPlayerID = 0;
std::vector<Events::InputCommand> m_InputCommandsToBroadcast;
//Timers
std::clock_t m_StartPingTime;
@@ -64,6 +65,7 @@ private:
void broadcast(Packet& packet);
void sendSnapshot();
void addChildrenToPacket(Packet& packet, EntityID entityID);
void addInputCommandsToPacket(Packet& packet);
void sendPing();
void checkForTimeOuts();
void disconnect(PlayerID playerID);
+1 -7
View File
@@ -9,7 +9,6 @@
#include "Core/System.h"
#include "Core/EPlayerDamage.h"
#include "Core/EShoot.h"
#include "Core/EPlayerSpawned.h"
#include "Input/EInputCommand.h"
#include "Core/EntityFile.h"
#include "Core/EntityFileParser.h"
@@ -28,16 +27,11 @@ public:
private:
IRenderer* m_Renderer;
// State
EntityWrapper m_LocalPlayer = EntityWrapper::Invalid;
// Events
EventRelay<WeaponSystem, Events::PlayerSpawned> m_EPlayerSpawned;
bool WeaponSystem::OnPlayerSpawned(const Events::PlayerSpawned& e);
EventRelay<WeaponSystem, Events::Shoot> m_EShoot;
bool WeaponSystem::OnShoot(Events::Shoot& e);
EventRelay<WeaponSystem, Events::InputCommand> m_EInputCommand;
bool WeaponSystem::OnInputCommand(const Events::InputCommand& e);
bool WeaponSystem::OnInputCommand(Events::InputCommand& e);
};
#endif
+16
View File
@@ -230,6 +230,18 @@ void Client::ignoreFields(Packet& packet, const ComponentInfo& componentInfo)
void Client::parseSnapshot(Packet& packet)
{
// Read input commands
std::size_t numInputCommands = packet.ReadPrimitive<std::size_t>();
for (std::size_t i = 0; i < numInputCommands; ++i) {
Events::InputCommand e;
e.PlayerID = packet.ReadPrimitive<EntityID>();
e.Player = EntityWrapper(m_World, m_ServerIDToClientID.at(packet.ReadPrimitive<EntityID>()));
e.Command = packet.ReadString();
e.Value = packet.ReadPrimitive<float>();
m_EventBroker->Publish(e);
}
// Read world state
while (packet.DataReadSize() < packet.Size()) {
EntityID serverEntityID = packet.ReadPrimitive<EntityID>();
EntityID serverParentID = packet.ReadPrimitive<EntityID>();
@@ -339,6 +351,10 @@ void Client::disconnect()
bool Client::OnInputCommand(const Events::InputCommand & e)
{
if (e.PlayerID != -1) {
return false;
}
if (e.Command == "ConnectToServer") { // Connect for now
if (e.Value > 0) {
connect();
+18
View File
@@ -165,10 +165,24 @@ void Server::broadcast(Packet& packet)
void Server::sendSnapshot()
{
Packet packet(MessageType::Snapshot);
addInputCommandsToPacket(packet);
addChildrenToPacket(packet, EntityID_Invalid);
broadcast(packet);
}
void Server::addInputCommandsToPacket(Packet& packet)
{
// Number of input commands
packet.WritePrimitive(m_InputCommandsToBroadcast.size());
for (auto& command : m_InputCommandsToBroadcast) {
packet.WritePrimitive(command.PlayerID);
packet.WritePrimitive(m_ConnectedPlayers.at(command.PlayerID).EntityID);
packet.WriteString(command.Command);
packet.WritePrimitive(command.Value);
}
m_InputCommandsToBroadcast.clear();
}
void Server::addChildrenToPacket(Packet & packet, EntityID entityID)
{
auto itPair = m_World->GetChildren(entityID);
@@ -273,6 +287,10 @@ void Server::parseOnInputCommand(Packet& packet)
e.Player = EntityWrapper(m_World, m_ConnectedPlayers.at(player).EntityID);
e.Value = packet.ReadPrimitive<float>();
m_EventBroker->Publish(e);
if (e.Command == "PrimaryFire") {
m_InputCommandsToBroadcast.push_back(e);
}
//LOG_INFO("Server::parseOnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID);
}
}
+5
View File
@@ -218,5 +218,10 @@ int Game::parseArgs(int argc, char* argv[])
m_IsClient = true;
}
// HACK: Right now, client and server are mutually exclusive
if (m_IsServer) {
m_IsClient = false;
}
return 0;
}
+6 -15
View File
@@ -5,7 +5,6 @@ WeaponSystem::WeaponSystem(SystemParams params, IRenderer* renderer)
, ImpureSystem()
, m_Renderer(renderer)
{
EVENT_SUBSCRIBE_MEMBER(m_EPlayerSpawned, &WeaponSystem::OnPlayerSpawned);
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &WeaponSystem::OnInputCommand);
EVENT_SUBSCRIBE_MEMBER(m_EShoot, &WeaponSystem::OnShoot);
}
@@ -15,30 +14,22 @@ void WeaponSystem::Update(double dt)
}
bool WeaponSystem::OnPlayerSpawned(const Events::PlayerSpawned& e)
{
if (e.PlayerID == -1) {
m_LocalPlayer = e.Player;
}
return true;
}
bool WeaponSystem::OnInputCommand(const Events::InputCommand& e)
bool WeaponSystem::OnInputCommand(Events::InputCommand& e)
{
// Only shoot client-side!
if (e.PlayerID != -1) {
if (!IsClient) {
return false;
}
// Only shoot if the player is alive
if (!m_LocalPlayer.Valid()) {
if (!e.Player.Valid()) {
return false;
}
if (e.Command == "PrimaryFire" && e.Value > 0) {
Events::Shoot eShoot;
if (e.PlayerID == -1) {
eShoot.Player = m_LocalPlayer;
eShoot.Player = LocalPlayer;
} else {
eShoot.Player = e.Player;
}
@@ -97,8 +88,8 @@ bool WeaponSystem::OnShoot(Events::Shoot& eShoot)
//(glm::vec3&)ray["Transform"]["Orientation"] = Transform::AbsoluteOrientationEuler(weapon);
}
// Only run further picking code client-side!
if (eShoot.Player != m_LocalPlayer) {
// Only run further picking code for the local player!
if (eShoot.Player != LocalPlayer) {
return false;
}