diff --git a/src/DarkRP2DDedicatedServer.cpp b/src/DarkRP2DDedicatedServer.cpp index 524e2fe..9466f21 100644 --- a/src/DarkRP2DDedicatedServer.cpp +++ b/src/DarkRP2DDedicatedServer.cpp @@ -1,4 +1,3 @@ - //std #include #include @@ -42,6 +41,10 @@ void ReadCin(std::atomic &run) } } +void parseArguments() { + //TODO +} + int main(int argc, char *argv[]) { //--- Setup CMD diff --git a/src/GameServer.cpp b/src/GameServer.cpp index dba4538..48b6235 100644 --- a/src/GameServer.cpp +++ b/src/GameServer.cpp @@ -7,6 +7,7 @@ //own #include "core/Components.h" #include "core/systems/PhysicSharer.h" +#include "core/systems/InputProcessor.h" using namespace yojimbo; @@ -34,21 +35,41 @@ GameServer::GameServer(const yojimbo::Address &address) : m_time(yojimbo_time()) // ... load game ... ex.systems.add(this); + ex.systems.add(this); ex.systems.configure(); // load an custom object - entityx::Entity entity = ex.entities.create(); - b2BodyDef groundBodyDef; - groundBodyDef.position.Set(0.0f, -10.0f); - b2Body *bod = this->world.CreateBody(&groundBodyDef); - entity.assign(bod); + // entityx::Entity entity = ex.entities.create(); + // b2BodyDef playerBodyDef; + // playerBodyDef.type = b2_kinematicBody; + // playerBodyDef.position.Set(0.0f, 0.0f); + // b2Body *bod = this->world.CreateBody(&playerBodyDef); + + // b2PolygonShape hitbox; + // hitbox.SetAsBox(5.0f, 5.0f); + + // bod->CreateFixture(&hitbox, 0.0f); + // entity.assign(bod); //TODO: -- add box2d components -- } void GameServer::ClientConnected(int clientIndex) { entityx::Entity entity = ex.entities.create(); + //---Add network info entity.assign(clientIndex); + //---Add networked physics + b2BodyDef playerBodyDef; + playerBodyDef.type = b2_kinematicBody; + playerBodyDef.position.Set(0.0f, 0.0f); + b2Body *bod = this->world.CreateBody(&playerBodyDef); + + b2PolygonShape hitbox; + hitbox.SetAsBox(5.0f, 5.0f); + bod->CreateFixture(&hitbox, 0.0f); + entity.assign(bod); + entity.assign(0.0f,0.0f); + //--Usual continue serverToEntityX.insert(std::pair(clientIndex, entity)); std::cout << "client " << clientIndex << " connected" << std::endl; } @@ -112,8 +133,8 @@ void GameServer::Update(float dt) this->world.Step(dt, velocityIterations, positionIterations); // ... send game state to clients ... - ex.systems.update(dt); - + ex.systems.update(dt); + // ex.systems.update(dt); m_server.SendPackets(); } diff --git a/src/GameServer.h b/src/GameServer.h index f9e8b7e..4a9ea03 100644 --- a/src/GameServer.h +++ b/src/GameServer.h @@ -56,7 +56,6 @@ public: std::map serverToEntityX; - GameConnectionConfig m_connectionConfig; ServerAdapter m_adapter; yojimbo::Server m_server; diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index 52dfdb4..7f325bc 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -12,6 +12,8 @@ Command *Command::create(int clientIndex, yojimbo::Message *message) return new TestCommand(clientIndex, ((TestMessage *)message)->m_data); case (int)GameMessageType::SETNAME: return new SetNameCommand(clientIndex, std::string(((SetNameMessage *)message)->buffer)); + case (int)GameMessageType::INPUTSTATEMESSAGE: + return new PlayerInputCommand(clientIndex, ((InputStateMessage *) message)->m_vel_x, ((InputStateMessage *) message)->m_vel_y); default: std::cout << "Unknown message type received over network!" << std::endl; return nullptr; @@ -31,6 +33,9 @@ Command *Command::create(std::string command) else if (token == "test") { return new TestCommand(-1, 100); + } else if (token == "sendTest") + { + return new SendTestCommand(0, 42); } std::cout << "Local command don't exists: " << command << std::endl; return nullptr; diff --git a/src/commands/Command.h b/src/commands/Command.h index 84c62cd..1491485 100644 --- a/src/commands/Command.h +++ b/src/commands/Command.h @@ -25,9 +25,10 @@ class TestCommand : public Command { public: TestCommand(int clientIndex, int data) - : m_clientIndex(clientIndex), - m_data(data) - {} + : m_clientIndex(clientIndex), + m_data(data) + { + } virtual void execute(GameServer *server) override; @@ -40,9 +41,10 @@ class SetNameCommand : public Command { public: SetNameCommand(int clientIndex, std::string name) - : m_clientIndex(clientIndex), - m_name(name) - {} + : m_clientIndex(clientIndex), + m_name(name) + { + } virtual void execute(GameServer *server) override; @@ -57,4 +59,36 @@ class StopCommand : public Command { public: virtual void execute(GameServer *server) override; -}; \ No newline at end of file +}; + +class SendTestCommand : public Command +{ +public: + SendTestCommand(int clientIndex, int data) + : m_clientIndex(clientIndex), + m_data(data) + { + } + virtual void execute(GameServer *server) override; + +private: + int m_clientIndex; + int m_data; +}; + +class PlayerInputCommand : public Command +{ +public: + PlayerInputCommand(int clientIndex, float vel_x, float vel_y) + : m_clientIndex(clientIndex), + m_vel_x(vel_x), + m_vel_y(vel_y) + { + } + virtual void execute(GameServer *server) override; + +private: + int m_clientIndex; + float m_vel_x, m_vel_y; +}; + diff --git a/src/commands/PlayerInputCommand.cpp b/src/commands/PlayerInputCommand.cpp new file mode 100644 index 0000000..8767c0d --- /dev/null +++ b/src/commands/PlayerInputCommand.cpp @@ -0,0 +1,14 @@ +#include "Command.h" + +#include "../core/Components.h" + +void PlayerInputCommand::execute(GameServer *server) +{ + entityx::Entity entity = server->serverToEntityX.at(m_clientIndex); + entityx::ComponentHandle iVel = entity.component(); + if (iVel) + { + iVel->m_vel_x = this->m_vel_x; + iVel->m_vel_y = this->m_vel_y; + } +} \ No newline at end of file diff --git a/src/commands/SendTestCommand.cpp b/src/commands/SendTestCommand.cpp new file mode 100644 index 0000000..9ef4502 --- /dev/null +++ b/src/commands/SendTestCommand.cpp @@ -0,0 +1,9 @@ +#include "Command.h" + +void SendTestCommand::execute(GameServer *server) +{ + std::cout << "Sending Test command to ClientID: " << this->m_clientIndex << " with data: " << this->m_data << std::endl; + TestMessage *msg = (TestMessage *)server->m_server.CreateMessage(this->m_clientIndex, (int)GameMessageType::TEST); + msg->m_data = this->m_data; + server->m_server.SendMessage(this->m_clientIndex, (int)GameChannel::RELIABLE, msg); +} \ No newline at end of file diff --git a/src/core/Components.h b/src/core/Components.h index db40ee5..1c4978a 100644 --- a/src/core/Components.h +++ b/src/core/Components.h @@ -20,4 +20,13 @@ struct PhysicBody PhysicBody(b2Body* b) : body(b) {} b2Body* body; +}; + +struct InputVelocity +{ + InputVelocity(float vel_x, float vel_y) + : m_vel_x(vel_x), m_vel_y(vel_y) + {} + + float m_vel_x, m_vel_y; }; \ No newline at end of file diff --git a/src/core/systems/DebugPositionPrinter.h b/src/core/systems/DebugPositionPrinter.h new file mode 100644 index 0000000..1d906d9 --- /dev/null +++ b/src/core/systems/DebugPositionPrinter.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include "../../GameServer.h" +#include "../Components.h" + +struct DebugPositionPrinter : public entityx::System +{ + DebugPositionPrinter(GameServer *server) : m_gserver(server) {} + GameServer *m_gserver; + + void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override + { + es.each([](entityx::Entity entity, NetworkPlayerProperties &nProp, PhysicBody &bod) { + std::cout << "Client " << nProp.name << ":" << nProp.m_clientIndex << " Pos: " << bod.body->GetPosition << std::endl; + }); + }; +}; \ No newline at end of file diff --git a/src/core/systems/InputProcessor.h b/src/core/systems/InputProcessor.h new file mode 100644 index 0000000..4c5e0d1 --- /dev/null +++ b/src/core/systems/InputProcessor.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include "../../GameServer.h" +#include "../Components.h" + +struct InputProcessor : public entityx::System +{ + InputProcessor(GameServer *server) : m_gserver(server) {} + GameServer *m_gserver; + + void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override + { + //For each with input component look for id + es.each([](entityx::Entity entity, InputVelocity &iVel, PhysicBody &bod) { + // Do things with entity, position and direction. + }); + }; +}; \ No newline at end of file diff --git a/src/core/systems/PhysicSharer.h b/src/core/systems/PhysicSharer.h index eadfd11..26fb9cd 100644 --- a/src/core/systems/PhysicSharer.h +++ b/src/core/systems/PhysicSharer.h @@ -11,7 +11,7 @@ struct PhysicsShareSystem : public entityx::System void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override { - es.each([&es, dt, this](entityx::Entity entity, PhysicBody &body) { //For every phyicbody + es.each([&es, dt, this](entityx::Entity entity, PhysicBody &body) { //For every phyicbody filter networked kinematic bodys //ONLY 10 TIMES PER SECOND //body.body->IsActive es.each([body, dt, this](entityx::Entity entity, NetworkPlayerProperties &net) { //For every networkplayer diff --git a/src/network/Shared.h b/src/network/Shared.h index 242ec73..5110d59 100644 --- a/src/network/Shared.h +++ b/src/network/Shared.h @@ -28,6 +28,7 @@ enum class GameMessageType TEST, SETNAME, PHYSICSTATEMESSAGE, + INPUTSTATEMESSAGE, COUNT }; @@ -86,11 +87,31 @@ public: YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS(); }; +class InputStateMessage : public yojimbo::Message +{ +public: + float m_vel_x, m_vel_y; + + InputStateMessage() : m_vel_x(0), m_vel_y(0) {} + + template + bool Serialize(Stream &stream) + { + serialize_float(stream, m_vel_x); + serialize_float(stream, m_vel_y); + return true; + } + + YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS(); +}; + + // the message factory YOJIMBO_MESSAGE_FACTORY_START(GameMessageFactory, (int)GameMessageType::COUNT); YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::TEST, TestMessage); YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::SETNAME, SetNameMessage); YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::PHYSICSTATEMESSAGE, PhysicStateMessage); +YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::INPUTSTATEMESSAGE, InputStateMessage); YOJIMBO_MESSAGE_FACTORY_FINISH(); // the adapter