diff --git a/CMakeLists.txt b/CMakeLists.txt index fd2287d..d56e7c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,6 @@ if(NOT CMAKE_BUILD_TYPE) add_definitions(-DNDEBUG) endif(NOT CMAKE_BUILD_TYPE) - if( CMAKE_SIZEOF_VOID_P EQUAL 8 ) MESSAGE( "64-Bit Compiler erkannt" ) SET( EX_PLATFORM 64 ) diff --git a/conanfile.txt b/conanfile.txt index c996fed..a31cfa2 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,10 +1,12 @@ [requires] box2d/2.3.1@conan/stable glm/0.9.9.1@g-truc/stable -EntityX/1.1.2@gulum/stable +EntityX/1.3.0@gulum/stable Yojimbo/1.2.1@gulum/stable vscodepropertiesgen/0.1@mkovalchik/stable +[options] +Entityx:shared=False [generators] cmake diff --git a/src/DarkRP2DDedicatedServer.cpp b/src/DarkRP2DDedicatedServer.cpp index 9641e1f..524e2fe 100644 --- a/src/DarkRP2DDedicatedServer.cpp +++ b/src/DarkRP2DDedicatedServer.cpp @@ -56,7 +56,6 @@ int main(int argc, char *argv[]) if (!InitializeYojimbo()) { std::cout << "error: failed to initialize Yojimbo!" << std::endl; - ; return 1; } yojimbo_log_level(YOJIMBO_LOG_LEVEL_INFO); diff --git a/src/GameServer.cpp b/src/GameServer.cpp index 54133d4..dba4538 100644 --- a/src/GameServer.cpp +++ b/src/GameServer.cpp @@ -6,6 +6,7 @@ //own #include "core/Components.h" +#include "core/systems/PhysicSharer.h" using namespace yojimbo; @@ -17,7 +18,8 @@ GameServer::GameServer(const yojimbo::Address &address) : m_time(yojimbo_time()) m_connectionConfig, m_adapter, m_time), - m_running(false) + m_running(false), + world(b2Vec2(0.0, 0.0)) //2D top down gravity { // start the server m_server.Start(MAX_PLAYERS); @@ -31,12 +33,22 @@ GameServer::GameServer(const yojimbo::Address &address) : m_time(yojimbo_time()) m_server.GetAddress().ToString(buffer, sizeof(buffer)); // ... load game ... + 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); + //TODO: -- add box2d components -- } void GameServer::ClientConnected(int clientIndex) { entityx::Entity entity = ex.entities.create(); - entity.assign(); + entity.assign(clientIndex); serverToEntityX.insert(std::pair(clientIndex, entity)); std::cout << "client " << clientIndex << " connected" << std::endl; } @@ -95,7 +107,13 @@ void GameServer::Update(float dt) this->m_commandQueueMutex.unlock(); // ... update game ... + int velocityIterations = 6; + int positionIterations = 2; + this->world.Step(dt, velocityIterations, positionIterations); + // ... send game state to clients ... + ex.systems.update(dt); + m_server.SendPackets(); } diff --git a/src/GameServer.h b/src/GameServer.h index f875279..f9e8b7e 100644 --- a/src/GameServer.h +++ b/src/GameServer.h @@ -10,11 +10,12 @@ //Vendor #include #include +#include class GameServer; //Own -#include "Shared.h" +#include "network/Shared.h" #include "commands/Command.h" static const int MAX_PLAYERS = 64; @@ -49,16 +50,20 @@ public: bool m_running; std::queue> commandQueue; + entityx::EntityX ex; + b2World world; + std::map serverToEntityX; -private: - double m_time; - std::mutex m_commandQueueMutex; GameConnectionConfig m_connectionConfig; ServerAdapter m_adapter; yojimbo::Server m_server; + +private: + double m_time; + std::mutex m_commandQueueMutex; }; #endif //GAMESERVER_H \ No newline at end of file diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index cf7ff86..52dfdb4 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -1,5 +1,5 @@ #include "Command.h" -#include "../Shared.h" +#include "../network/Shared.h" #include #include diff --git a/src/core/Components.h b/src/core/Components.h index 10ab372..db40ee5 100644 --- a/src/core/Components.h +++ b/src/core/Components.h @@ -4,8 +4,20 @@ struct NetworkPlayerProperties { - NetworkPlayerProperties(std::string name = "unknown") - : name(name) {} + NetworkPlayerProperties(int clientIndex,std::string name = "unknown") + : name(name), + m_clientIndex(clientIndex) + {} std::string name; + int m_clientIndex; +}; + +#include + +struct PhysicBody +{ + PhysicBody(b2Body* b) : body(b) {} + + b2Body* body; }; \ No newline at end of file diff --git a/src/core/systems/PhysicSharer.h b/src/core/systems/PhysicSharer.h new file mode 100644 index 0000000..eadfd11 --- /dev/null +++ b/src/core/systems/PhysicSharer.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include "../../GameServer.h" +#include "../Components.h" + +struct PhysicsShareSystem : public entityx::System +{ + PhysicsShareSystem(GameServer *server) : m_gserver(server) {} + GameServer *m_gserver; + + 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 + //ONLY 10 TIMES PER SECOND + //body.body->IsActive + es.each([body, dt, this](entityx::Entity entity, NetworkPlayerProperties &net) { //For every networkplayer + PhysicStateMessage *msg = (PhysicStateMessage *)this->m_gserver->m_server.CreateMessage(net.m_clientIndex, (int)GameMessageType::PHYSICSTATEMESSAGE); + msg->isActive = body.body->IsActive(); + msg->m_posX = body.body->GetPosition().x; + msg->m_posY = body.body->GetPosition().y; + this->m_gserver->m_server.SendMessage(net.m_clientIndex, (int)GameChannel::UNRELIABLE, msg); + }); + }); + }; +}; \ No newline at end of file diff --git a/src/ServerAdapter.cpp b/src/network/ServerAdapter.cpp similarity index 93% rename from src/ServerAdapter.cpp rename to src/network/ServerAdapter.cpp index 32bc84a..d3f0a14 100644 --- a/src/ServerAdapter.cpp +++ b/src/network/ServerAdapter.cpp @@ -1,4 +1,4 @@ -#include "GameServer.h" +#include "../GameServer.h" ServerAdapter::ServerAdapter(GameServer *server) : m_server(server) {} diff --git a/src/Shared.h b/src/network/Shared.h similarity index 67% rename from src/Shared.h rename to src/network/Shared.h index b6e2f95..242ec73 100644 --- a/src/Shared.h +++ b/src/network/Shared.h @@ -3,14 +3,6 @@ #include -// a simple test message -enum class GameMessageType -{ - TEST, - SETNAME, - COUNT -}; - // two channels, one for each type that Yojimbo supports enum class GameChannel { @@ -30,6 +22,15 @@ struct GameConnectionConfig : yojimbo::ClientServerConfig } }; +// a simple test message +enum class GameMessageType +{ + TEST, + SETNAME, + PHYSICSTATEMESSAGE, + COUNT +}; + class TestMessage : public yojimbo::Message { public: @@ -51,8 +52,7 @@ class SetNameMessage : public yojimbo::Message { public: char buffer[yojimbo::MaxAddressLength]; - SetNameMessage() { - } + SetNameMessage() {} template bool Serialize(Stream &stream) @@ -64,22 +64,43 @@ public: YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS(); }; +class PhysicStateMessage : public yojimbo::Message +{ +public: + int m_objectID; + float m_posX; + float m_posY; + bool isActive; + PhysicStateMessage() : m_objectID(0), m_posX(0), m_posY(0), isActive(false) {} + + template + bool Serialize(Stream &stream) + { + serialize_int(stream, m_objectID, 0, 512); + serialize_float(stream, m_posX); + serialize_float(stream, m_posY); + serialize_bool(stream, isActive); + 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::TEST, TestMessage); +YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::SETNAME, SetNameMessage); +YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::PHYSICSTATEMESSAGE, PhysicStateMessage); YOJIMBO_MESSAGE_FACTORY_FINISH(); // the adapter class SharedAdapter : public yojimbo::Adapter { public: - yojimbo::MessageFactory *CreateMessageFactory(yojimbo::Allocator &allocator) override { return YOJIMBO_NEW(allocator, GameMessageFactory, allocator); } - }; static const uint8_t DEFAULT_PRIVATE_KEY[yojimbo::KeyBytes] = {0};