Add current progression
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
//std
|
//std
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -42,6 +41,10 @@ void ReadCin(std::atomic<bool> &run)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void parseArguments() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
//--- Setup CMD
|
//--- Setup CMD
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
//own
|
//own
|
||||||
#include "core/Components.h"
|
#include "core/Components.h"
|
||||||
#include "core/systems/PhysicSharer.h"
|
#include "core/systems/PhysicSharer.h"
|
||||||
|
#include "core/systems/InputProcessor.h"
|
||||||
|
|
||||||
using namespace yojimbo;
|
using namespace yojimbo;
|
||||||
|
|
||||||
@@ -34,21 +35,41 @@ GameServer::GameServer(const yojimbo::Address &address) : m_time(yojimbo_time())
|
|||||||
|
|
||||||
// ... load game ...
|
// ... load game ...
|
||||||
ex.systems.add<PhysicsShareSystem>(this);
|
ex.systems.add<PhysicsShareSystem>(this);
|
||||||
|
ex.systems.add<InputProcessor>(this);
|
||||||
ex.systems.configure();
|
ex.systems.configure();
|
||||||
|
|
||||||
// load an custom object
|
// load an custom object
|
||||||
entityx::Entity entity = ex.entities.create();
|
// entityx::Entity entity = ex.entities.create();
|
||||||
b2BodyDef groundBodyDef;
|
// b2BodyDef playerBodyDef;
|
||||||
groundBodyDef.position.Set(0.0f, -10.0f);
|
// playerBodyDef.type = b2_kinematicBody;
|
||||||
b2Body *bod = this->world.CreateBody(&groundBodyDef);
|
// playerBodyDef.position.Set(0.0f, 0.0f);
|
||||||
entity.assign<PhysicBody>(bod);
|
// b2Body *bod = this->world.CreateBody(&playerBodyDef);
|
||||||
|
|
||||||
|
// b2PolygonShape hitbox;
|
||||||
|
// hitbox.SetAsBox(5.0f, 5.0f);
|
||||||
|
|
||||||
|
// bod->CreateFixture(&hitbox, 0.0f);
|
||||||
|
// entity.assign<PhysicBody>(bod);
|
||||||
//TODO: -- add box2d components --
|
//TODO: -- add box2d components --
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameServer::ClientConnected(int clientIndex)
|
void GameServer::ClientConnected(int clientIndex)
|
||||||
{
|
{
|
||||||
entityx::Entity entity = ex.entities.create();
|
entityx::Entity entity = ex.entities.create();
|
||||||
|
//---Add network info
|
||||||
entity.assign<NetworkPlayerProperties>(clientIndex);
|
entity.assign<NetworkPlayerProperties>(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<PhysicBody>(bod);
|
||||||
|
entity.assign<InputVelocity>(0.0f,0.0f);
|
||||||
|
//--Usual continue
|
||||||
serverToEntityX.insert(std::pair<int, entityx::Entity>(clientIndex, entity));
|
serverToEntityX.insert(std::pair<int, entityx::Entity>(clientIndex, entity));
|
||||||
std::cout << "client " << clientIndex << " connected" << std::endl;
|
std::cout << "client " << clientIndex << " connected" << std::endl;
|
||||||
}
|
}
|
||||||
@@ -112,8 +133,8 @@ void GameServer::Update(float dt)
|
|||||||
this->world.Step(dt, velocityIterations, positionIterations);
|
this->world.Step(dt, velocityIterations, positionIterations);
|
||||||
|
|
||||||
// ... send game state to clients ...
|
// ... send game state to clients ...
|
||||||
ex.systems.update<PhysicsShareSystem>(dt);
|
ex.systems.update<InputProcessor>(dt);
|
||||||
|
// ex.systems.update<PhysicsShareSystem>(dt);
|
||||||
|
|
||||||
m_server.SendPackets();
|
m_server.SendPackets();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ public:
|
|||||||
|
|
||||||
std::map<int, entityx::Entity> serverToEntityX;
|
std::map<int, entityx::Entity> serverToEntityX;
|
||||||
|
|
||||||
|
|
||||||
GameConnectionConfig m_connectionConfig;
|
GameConnectionConfig m_connectionConfig;
|
||||||
ServerAdapter m_adapter;
|
ServerAdapter m_adapter;
|
||||||
yojimbo::Server m_server;
|
yojimbo::Server m_server;
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ Command *Command::create(int clientIndex, yojimbo::Message *message)
|
|||||||
return new TestCommand(clientIndex, ((TestMessage *)message)->m_data);
|
return new TestCommand(clientIndex, ((TestMessage *)message)->m_data);
|
||||||
case (int)GameMessageType::SETNAME:
|
case (int)GameMessageType::SETNAME:
|
||||||
return new SetNameCommand(clientIndex, std::string(((SetNameMessage *)message)->buffer));
|
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:
|
default:
|
||||||
std::cout << "Unknown message type received over network!" << std::endl;
|
std::cout << "Unknown message type received over network!" << std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -31,6 +33,9 @@ Command *Command::create(std::string command)
|
|||||||
else if (token == "test")
|
else if (token == "test")
|
||||||
{
|
{
|
||||||
return new TestCommand(-1, 100);
|
return new TestCommand(-1, 100);
|
||||||
|
} else if (token == "sendTest")
|
||||||
|
{
|
||||||
|
return new SendTestCommand(0, 42);
|
||||||
}
|
}
|
||||||
std::cout << "Local command don't exists: " << command << std::endl;
|
std::cout << "Local command don't exists: " << command << std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
@@ -26,8 +26,9 @@ class TestCommand : public Command
|
|||||||
public:
|
public:
|
||||||
TestCommand(int clientIndex, int data)
|
TestCommand(int clientIndex, int data)
|
||||||
: m_clientIndex(clientIndex),
|
: m_clientIndex(clientIndex),
|
||||||
m_data(data)
|
m_data(data)
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
virtual void execute(GameServer *server) override;
|
virtual void execute(GameServer *server) override;
|
||||||
|
|
||||||
@@ -41,8 +42,9 @@ class SetNameCommand : public Command
|
|||||||
public:
|
public:
|
||||||
SetNameCommand(int clientIndex, std::string name)
|
SetNameCommand(int clientIndex, std::string name)
|
||||||
: m_clientIndex(clientIndex),
|
: m_clientIndex(clientIndex),
|
||||||
m_name(name)
|
m_name(name)
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
virtual void execute(GameServer *server) override;
|
virtual void execute(GameServer *server) override;
|
||||||
|
|
||||||
@@ -58,3 +60,35 @@ class StopCommand : public Command
|
|||||||
public:
|
public:
|
||||||
virtual void execute(GameServer *server) override;
|
virtual void execute(GameServer *server) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
14
src/commands/PlayerInputCommand.cpp
Normal file
14
src/commands/PlayerInputCommand.cpp
Normal file
@@ -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<InputVelocity> iVel = entity.component<InputVelocity>();
|
||||||
|
if (iVel)
|
||||||
|
{
|
||||||
|
iVel->m_vel_x = this->m_vel_x;
|
||||||
|
iVel->m_vel_y = this->m_vel_y;
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/commands/SendTestCommand.cpp
Normal file
9
src/commands/SendTestCommand.cpp
Normal file
@@ -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);
|
||||||
|
}
|
||||||
@@ -21,3 +21,12 @@ struct PhysicBody
|
|||||||
|
|
||||||
b2Body* body;
|
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;
|
||||||
|
};
|
||||||
18
src/core/systems/DebugPositionPrinter.h
Normal file
18
src/core/systems/DebugPositionPrinter.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <entityx/entityx.h>
|
||||||
|
#include "../../GameServer.h"
|
||||||
|
#include "../Components.h"
|
||||||
|
|
||||||
|
struct DebugPositionPrinter : public entityx::System<DebugPositionPrinter>
|
||||||
|
{
|
||||||
|
DebugPositionPrinter(GameServer *server) : m_gserver(server) {}
|
||||||
|
GameServer *m_gserver;
|
||||||
|
|
||||||
|
void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override
|
||||||
|
{
|
||||||
|
es.each<NetworkPlayerProperties, PhysicBody>([](entityx::Entity entity, NetworkPlayerProperties &nProp, PhysicBody &bod) {
|
||||||
|
std::cout << "Client " << nProp.name << ":" << nProp.m_clientIndex << " Pos: " << bod.body->GetPosition << std::endl;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
19
src/core/systems/InputProcessor.h
Normal file
19
src/core/systems/InputProcessor.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <entityx/entityx.h>
|
||||||
|
#include "../../GameServer.h"
|
||||||
|
#include "../Components.h"
|
||||||
|
|
||||||
|
struct InputProcessor : public entityx::System<InputProcessor>
|
||||||
|
{
|
||||||
|
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<InputVelocity, PhysicBody>([](entityx::Entity entity, InputVelocity &iVel, PhysicBody &bod) {
|
||||||
|
// Do things with entity, position and direction.
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -11,7 +11,7 @@ struct PhysicsShareSystem : public entityx::System<PhysicsShareSystem>
|
|||||||
|
|
||||||
void update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt) override
|
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
|
es.each<PhysicBody>([&es, dt, this](entityx::Entity entity, PhysicBody &body) { //For every phyicbody filter networked kinematic bodys
|
||||||
//ONLY 10 TIMES PER SECOND
|
//ONLY 10 TIMES PER SECOND
|
||||||
//body.body->IsActive
|
//body.body->IsActive
|
||||||
es.each<NetworkPlayerProperties>([body, dt, this](entityx::Entity entity, NetworkPlayerProperties &net) { //For every networkplayer
|
es.each<NetworkPlayerProperties>([body, dt, this](entityx::Entity entity, NetworkPlayerProperties &net) { //For every networkplayer
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ enum class GameMessageType
|
|||||||
TEST,
|
TEST,
|
||||||
SETNAME,
|
SETNAME,
|
||||||
PHYSICSTATEMESSAGE,
|
PHYSICSTATEMESSAGE,
|
||||||
|
INPUTSTATEMESSAGE,
|
||||||
COUNT
|
COUNT
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -86,11 +87,31 @@ public:
|
|||||||
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
|
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 <typename Stream>
|
||||||
|
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
|
// the message factory
|
||||||
YOJIMBO_MESSAGE_FACTORY_START(GameMessageFactory, (int)GameMessageType::COUNT);
|
YOJIMBO_MESSAGE_FACTORY_START(GameMessageFactory, (int)GameMessageType::COUNT);
|
||||||
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::TEST, TestMessage);
|
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::TEST, TestMessage);
|
||||||
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::SETNAME, SetNameMessage);
|
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::SETNAME, SetNameMessage);
|
||||||
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::PHYSICSTATEMESSAGE, PhysicStateMessage);
|
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::PHYSICSTATEMESSAGE, PhysicStateMessage);
|
||||||
|
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::INPUTSTATEMESSAGE, InputStateMessage);
|
||||||
YOJIMBO_MESSAGE_FACTORY_FINISH();
|
YOJIMBO_MESSAGE_FACTORY_FINISH();
|
||||||
|
|
||||||
// the adapter
|
// the adapter
|
||||||
|
|||||||
Reference in New Issue
Block a user