diff --git a/game/game.cpp b/game/game.cpp index 38fcc0c..2d07b9f 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -15,11 +15,7 @@ Game::Game() m_state = new game::State(); m_state->init(); - m_state->addPlayer(0); - m_state->addPlayer(1); - m_state->addPlayer(2); - m_state->addPlayer(3); - //m_state->addPlayer(2); + m_state->addPlayer(); } bool Game::cycle(float dt) diff --git a/game/game.hpp b/game/game.hpp index 9c9512a..36a1de6 100644 --- a/game/game.hpp +++ b/game/game.hpp @@ -11,7 +11,7 @@ public: bool cycle(float dt); // for rendering - const game::State *state() const { return m_state; } + game::State *state() const { return m_state; } private: game::State *m_state; diff --git a/game/game_window.hpp b/game/game_window.hpp index 8bd7c4f..02c1aa0 100644 --- a/game/game_window.hpp +++ b/game/game_window.hpp @@ -27,30 +27,30 @@ protected: //if (!once) { // once = true; // for (int i=0; i<1000; i++) { - // m_game.cycle(time); + // m_game->cycle(time); // } //} - if (!m_game.cycle(static_cast(time/1000000000.0))) { + if (!m_game->cycle(static_cast(time/1000000000.0))) { std::cout<<"stopping the game..." << std::endl; stop(); } 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); } - for (const game::Trace *trace : m_game.state()->traces) { + for (const game::Trace *trace : m_game->state()->traces) { drawTrace(trace); } - for (const game::Ship *ship : m_game.state()->ships) { + for (const game::Ship *ship : m_game->state()->ships) { 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) { drawMissile(missile->position); } @@ -64,7 +64,7 @@ protected: //std::cout<<"draw ship @ " << pos.x << ", " << pos.y << std::endl; 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); } @@ -93,11 +93,11 @@ protected: } public: - GameWindow(unsigned int width, unsigned int height) - : endofthejedi::GLWindow(width, height) {} + GameWindow(unsigned int width, unsigned int height, Game* game) + : endofthejedi::GLWindow(width, height) { m_game = game; } private: - Game m_game; + Game *m_game; endofthejedi::Renderer m_renderer; }; diff --git a/game/main.cpp b/game/main.cpp index 6be6ec0..f33a738 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) { // {"add", no_argument, 0, 'a'}, // {"append", no_argument, 0, 'b'}, // {"delete", required_argument, 0, 'd'}, -// {"create", required_argument, 0, 'c'}, + {"port", required_argument, 0, 'p'}, {"fps", no_argument, 0, 'f'}, {0, 0, 0, 0} }; @@ -39,7 +39,7 @@ int main(int argc, char *argv[]) { int option_index = 0; while(1){ - c = getopt_long (argc, argv, "abc:d:f", + c = getopt_long (argc, argv, "p:f", long_options, &option_index); if (c == -1) break; @@ -49,10 +49,10 @@ int main(int argc, char *argv[]) { case 'f': SET_FLAG(SHOW_FPS,true); break; - /*case 'c': - cvalue = optarg; - break; - */ + case 'p': + strcpy(port,optarg); + break; + case '?': /* getopt_long already printed an error message. */ @@ -63,10 +63,12 @@ int main(int argc, char *argv[]) { } } - asio::io_service io_service; - Server s(io_service, atoi(port) ); + Game game; - 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); while(window.running()){ diff --git a/game/server.hpp b/game/server.hpp index 7b6e59a..23bb4de 100644 --- a/game/server.hpp +++ b/game/server.hpp @@ -12,10 +12,11 @@ using asio::ip::tcp; class Server { 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)), socket_(io_service) { + state=s; do_accept(); } @@ -27,7 +28,7 @@ private: { if (!ec) { - std::make_shared(std::move(socket_))->start(); + std::make_shared(std::move(socket_),state)->start(); } do_accept(); @@ -36,4 +37,5 @@ private: tcp::acceptor acceptor_; tcp::socket socket_; + game::State* state; }; diff --git a/game/session.hpp b/game/session.hpp index 0c1a04e..cc7b3da 100644 --- a/game/session.hpp +++ b/game/session.hpp @@ -11,16 +11,22 @@ class Session : public std::enable_shared_from_this { public: - Session(tcp::socket socket) - : socket_(std::move(socket)) + Session(tcp::socket socket, game::State* state) + : m_socket(std::move(socket)) { + m_state = state; + char c[]="> "; + strcpy(m_snd_data, c); } void start() { + if(m_started) return; + m_started=true; + m_id = m_state->addPlayer(); 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>"; - asio::async_write(socket_, asio::buffer(hello, strlen(hello)), + 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(m_socket, asio::buffer(hello, strlen(hello)), [this, self](std::error_code ec, std::size_t /*length*/) { if (!ec) @@ -34,7 +40,7 @@ private: void do_read() { 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) { if (!ec) @@ -47,7 +53,7 @@ private: void do_write(std::size_t length) { 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*/) { if (!ec) @@ -57,7 +63,11 @@ private: }); } - tcp::socket socket_; + tcp::socket m_socket; 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; }; diff --git a/game/state/state.cpp b/game/state/state.cpp index bc39594..1eb66d3 100644 --- a/game/state/state.cpp +++ b/game/state/state.cpp @@ -62,10 +62,12 @@ namespace game { return true; } - void State::addPlayer(int playerId) + int State::addPlayer() { + int playerId = m_nextId++; Player *player = new Player(playerId); players.push_back(player); + return playerId; } void State::playerLeft(int playerId) diff --git a/game/state/state.hpp b/game/state/state.hpp index 0df4313..5058ef6 100644 --- a/game/state/state.hpp +++ b/game/state/state.hpp @@ -36,7 +36,7 @@ namespace game { // The upper layer (network/renderer) calling these three functions // should keep id's unique and give one (network) input an id. - void addPlayer(int playerId); + int addPlayer(); void playerLeft(int playerId); void commandForPlayer(int playerId, Command *cmd); @@ -89,5 +89,6 @@ namespace game { float m_playerRespawnTime; float m_shipRadius; float m_defaultEnergy; + int m_nextId=0; }; };