Add Physicsharer

This commit is contained in:
Julian Nießner
2019-06-19 11:44:52 +02:00
parent e6d262cfc0
commit 342162ba80
10 changed files with 109 additions and 27 deletions

View File

@@ -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 )

View File

@@ -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

View File

@@ -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);

View File

@@ -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<PhysicsShareSystem>(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<PhysicBody>(bod);
//TODO: -- add box2d components --
}
void GameServer::ClientConnected(int clientIndex)
{
entityx::Entity entity = ex.entities.create();
entity.assign<NetworkPlayerProperties>();
entity.assign<NetworkPlayerProperties>(clientIndex);
serverToEntityX.insert(std::pair<int, entityx::Entity>(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<PhysicsShareSystem>(dt);
m_server.SendPackets();
}

View File

@@ -10,11 +10,12 @@
//Vendor
#include <yojimbo.h>
#include <entityx/entityx.h>
#include <Box2D/Box2D.h>
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<std::unique_ptr<Command>> commandQueue;
entityx::EntityX ex;
b2World world;
std::map<int, entityx::Entity> 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

View File

@@ -1,5 +1,5 @@
#include "Command.h"
#include "../Shared.h"
#include "../network/Shared.h"
#include <iostream>
#include <sstream>

View File

@@ -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 <Box2D/Box2D.h>
struct PhysicBody
{
PhysicBody(b2Body* b) : body(b) {}
b2Body* body;
};

View File

@@ -0,0 +1,26 @@
#pragma once
#include <entityx/entityx.h>
#include "../../GameServer.h"
#include "../Components.h"
struct PhysicsShareSystem : public entityx::System<PhysicsShareSystem>
{
PhysicsShareSystem(GameServer *server) : m_gserver(server) {}
GameServer *m_gserver;
void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override
{
es.each<PhysicBody>([&es, dt, this](entityx::Entity entity, PhysicBody &body) { //For every phyicbody
//ONLY 10 TIMES PER SECOND
//body.body->IsActive
es.each<NetworkPlayerProperties>([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);
});
});
};
};

View File

@@ -1,4 +1,4 @@
#include "GameServer.h"
#include "../GameServer.h"
ServerAdapter::ServerAdapter(GameServer *server) : m_server(server) {}

View File

@@ -3,14 +3,6 @@
#include <yojimbo.h>
// 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 <typename Stream>
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 <typename Stream>
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};