networking addPlayer

This commit is contained in:
j3d1 2016-09-28 01:48:34 +02:00
parent 0e8e41e4fb
commit ef38da703e
8 changed files with 50 additions and 37 deletions

View file

@ -15,11 +15,7 @@ Game::Game()
m_state = new game::State(); m_state = new game::State();
m_state->init(); m_state->init();
m_state->addPlayer(0); m_state->addPlayer();
m_state->addPlayer(1);
m_state->addPlayer(2);
m_state->addPlayer(3);
//m_state->addPlayer(2);
} }
bool Game::cycle(float dt) bool Game::cycle(float dt)

View file

@ -11,7 +11,7 @@ public:
bool cycle(float dt); bool cycle(float dt);
// for rendering // for rendering
const game::State *state() const { return m_state; } game::State *state() const { return m_state; }
private: private:
game::State *m_state; game::State *m_state;

View file

@ -27,30 +27,30 @@ protected:
//if (!once) { //if (!once) {
// once = true; // once = true;
// for (int i=0; i<1000; i++) { // for (int i=0; i<1000; i++) {
// m_game.cycle(time); // m_game->cycle(time);
// } // }
//} //}
if (!m_game.cycle(static_cast<float>(time/1000000000.0))) { if (!m_game->cycle(static_cast<float>(time/1000000000.0))) {
std::cout<<"stopping the game..." << std::endl; std::cout<<"stopping the game..." << std::endl;
stop(); stop();
} }
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (const game::Planet *planet : m_game.state()->planets) { for (const game::Planet *planet : m_game->state()->planets) {
drawPlanet(planet->position, planet->radius); drawPlanet(planet->position, planet->radius);
} }
for (const game::Trace *trace : m_game.state()->traces) { for (const game::Trace *trace : m_game->state()->traces) {
drawTrace(trace); drawTrace(trace);
} }
for (const game::Ship *ship : m_game.state()->ships) { for (const game::Ship *ship : m_game->state()->ships) {
drawShip(ship->position); drawShip(ship->position);
} }
for (const game::Player *player : m_game.state()->players) { for (const game::Player *player : m_game->state()->players) {
for (const game::Missile *missile : player->missiles) { for (const game::Missile *missile : player->missiles) {
drawMissile(missile->position); drawMissile(missile->position);
} }
@ -64,7 +64,7 @@ protected:
//std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl; //std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl;
glm::vec3 color = glm::vec3(0.2, 1.0, 0.3); glm::vec3 color = glm::vec3(0.2, 1.0, 0.3);
float radius = m_game.state()->shipRadius(); float radius = m_game->state()->shipRadius();
m_renderer.drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z, 12); m_renderer.drawCircle(pos.x, pos.y, radius, color.x, color.y, color.z, 12);
} }
@ -93,11 +93,11 @@ protected:
} }
public: public:
GameWindow(unsigned int width, unsigned int height) GameWindow(unsigned int width, unsigned int height, Game* game)
: endofthejedi::GLWindow(width, height) {} : endofthejedi::GLWindow(width, height) { m_game = game; }
private: private:
Game m_game; Game *m_game;
endofthejedi::Renderer m_renderer; endofthejedi::Renderer m_renderer;
}; };

View file

@ -31,7 +31,7 @@ int main(int argc, char *argv[]) {
// {"add", no_argument, 0, 'a'}, // {"add", no_argument, 0, 'a'},
// {"append", no_argument, 0, 'b'}, // {"append", no_argument, 0, 'b'},
// {"delete", required_argument, 0, 'd'}, // {"delete", required_argument, 0, 'd'},
// {"create", required_argument, 0, 'c'}, {"port", required_argument, 0, 'p'},
{"fps", no_argument, 0, 'f'}, {"fps", no_argument, 0, 'f'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
@ -39,7 +39,7 @@ int main(int argc, char *argv[]) {
int option_index = 0; int option_index = 0;
while(1){ while(1){
c = getopt_long (argc, argv, "abc:d:f", c = getopt_long (argc, argv, "p:f",
long_options, &option_index); long_options, &option_index);
if (c == -1) if (c == -1)
break; break;
@ -49,10 +49,10 @@ int main(int argc, char *argv[]) {
case 'f': case 'f':
SET_FLAG(SHOW_FPS,true); SET_FLAG(SHOW_FPS,true);
break; break;
/*case 'c': case 'p':
cvalue = optarg; strcpy(port,optarg);
break; break;
*/
case '?': case '?':
/* getopt_long already printed an error message. */ /* getopt_long already printed an error message. */
@ -63,10 +63,12 @@ int main(int argc, char *argv[]) {
} }
} }
asio::io_service io_service; Game game;
Server s(io_service, atoi(port) );
GameWindow window(500, 500); asio::io_service io_service;
Server s(io_service, game.state(), atoi(port) );
GameWindow window(500, 500, &game);
window.set_maxfps(60.0); window.set_maxfps(60.0);
while(window.running()){ while(window.running()){

View file

@ -12,10 +12,11 @@ using asio::ip::tcp;
class Server class Server
{ {
public: public:
Server(asio::io_service& io_service, short port) Server(asio::io_service& io_service,game::State* s, short port)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
socket_(io_service) socket_(io_service)
{ {
state=s;
do_accept(); do_accept();
} }
@ -27,7 +28,7 @@ private:
{ {
if (!ec) if (!ec)
{ {
std::make_shared<Session>(std::move(socket_))->start(); std::make_shared<Session>(std::move(socket_),state)->start();
} }
do_accept(); do_accept();
@ -36,4 +37,5 @@ private:
tcp::acceptor acceptor_; tcp::acceptor acceptor_;
tcp::socket socket_; tcp::socket socket_;
game::State* state;
}; };

View file

@ -11,16 +11,22 @@ class Session
: public std::enable_shared_from_this<Session> : public std::enable_shared_from_this<Session>
{ {
public: public:
Session(tcp::socket socket) Session(tcp::socket socket, game::State* state)
: socket_(std::move(socket)) : m_socket(std::move(socket))
{ {
m_state = state;
char c[]="> ";
strcpy(m_snd_data, c);
} }
void start() void start()
{ {
if(m_started) return;
m_started=true;
m_id = m_state->addPlayer();
auto self(shared_from_this()); auto self(shared_from_this());
char hello[] = "\nUse \"n name\" to change name, \"v velocity\" to change velocity, \"c\" to clear past shots or \"q\" to close the connection.\nEverything else is interpreted as a shooting angle.\n\n>"; char hello[] = "\nUse \"n name\" to change name, \"v velocity\" to change velocity, \"c\" to clear past shots or \"q\" to close the connection.\nEverything else is interpreted as a shooting angle.\n\n> ";
asio::async_write(socket_, asio::buffer(hello, strlen(hello)), asio::async_write(m_socket, asio::buffer(hello, strlen(hello)),
[this, self](std::error_code ec, std::size_t /*length*/) [this, self](std::error_code ec, std::size_t /*length*/)
{ {
if (!ec) if (!ec)
@ -34,7 +40,7 @@ private:
void do_read() void do_read()
{ {
auto self(shared_from_this()); auto self(shared_from_this());
socket_.async_read_some(asio::buffer(data_, max_length), m_socket.async_read_some(asio::buffer(m_rcv_data, max_length),
[this, self](std::error_code ec, std::size_t length) [this, self](std::error_code ec, std::size_t length)
{ {
if (!ec) if (!ec)
@ -47,7 +53,7 @@ private:
void do_write(std::size_t length) void do_write(std::size_t length)
{ {
auto self(shared_from_this()); auto self(shared_from_this());
asio::async_write(socket_, asio::buffer(data_, length), asio::async_write(m_socket, asio::buffer(m_snd_data, length),
[this, self](std::error_code ec, std::size_t /*length*/) [this, self](std::error_code ec, std::size_t /*length*/)
{ {
if (!ec) if (!ec)
@ -57,7 +63,11 @@ private:
}); });
} }
tcp::socket socket_; tcp::socket m_socket;
enum { max_length = 1024 }; enum { max_length = 1024 };
char data_[max_length]; char m_snd_data[max_length];
char m_rcv_data[max_length];
game::State* m_state;
bool m_started = false;
int m_id;
}; };

View file

@ -62,10 +62,12 @@ namespace game {
return true; return true;
} }
void State::addPlayer(int playerId) int State::addPlayer()
{ {
int playerId = m_nextId++;
Player *player = new Player(playerId); Player *player = new Player(playerId);
players.push_back(player); players.push_back(player);
return playerId;
} }
void State::playerLeft(int playerId) void State::playerLeft(int playerId)

View file

@ -36,7 +36,7 @@ namespace game {
// The upper layer (network/renderer) calling these three functions // The upper layer (network/renderer) calling these three functions
// should keep id's unique and give one (network) input an id. // should keep id's unique and give one (network) input an id.
void addPlayer(int playerId); int addPlayer();
void playerLeft(int playerId); void playerLeft(int playerId);
void commandForPlayer(int playerId, Command *cmd); void commandForPlayer(int playerId, Command *cmd);
@ -89,5 +89,6 @@ namespace game {
float m_playerRespawnTime; float m_playerRespawnTime;
float m_shipRadius; float m_shipRadius;
float m_defaultEnergy; float m_defaultEnergy;
int m_nextId=0;
}; };
}; };