From be84d27784f0d9c3c814c023e2e8d1b8e64a53ab Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 13 Jun 2011 13:20:38 +0200 Subject: Handle player position updates. --- common | 2 +- game.cpp | 24 +++++++++++++++++++++++- game.h | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/common b/common index e483f46..ff7f9de 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit e483f468085379a986cbc48bb13a7983315475cf +Subproject commit ff7f9de199213ea6d4832c0b91f2a96f5edc6bb2 diff --git a/game.cpp b/game.cpp index 42e8b53..e24bacf 100644 --- a/game.cpp +++ b/game.cpp @@ -45,8 +45,9 @@ void Game::run(const std::string host, const std::string port) { async_read(); unsigned int last = SDL_GetTicks(); + Vector3 last_pos = scene->pos; while(scene->running) { - if(SDL_GetTicks() - last >= 1000) { + if(SDL_GetTicks() - last >= 100 && scene->pos != last_pos) { message::Pos pos(scene->pos); pos.send(socket); last = SDL_GetTicks(); @@ -75,6 +76,9 @@ void Game::handle_type(const boost::system::error_code& error, std::size_t bytes } switch(*type) { + case message::MSG_TYPE_POS: + handle_pos(); + break; case message::MSG_TYPE_CHUNK: handle_chunk(); break; @@ -92,6 +96,24 @@ void Game::handle_type(const boost::system::error_code& error, std::size_t bytes async_read(); } +void Game::handle_pos() { + message::Pos m; + + m.recv(socket); + uint32_t id = m.get_id(); + Vector3 pos = m.get_pos(); + if(id == 0) { // my pos + scene->pos = pos; + } else { // other player's pos + for(PlayerList::iterator it = scene->players.begin(); it != scene->players.end(); it++) { + if((*it)->get_id() == id) { + (*it)->set_pos(pos); + break; + } + } + } +} + void Game::handle_chunk() { message::Chunk m; diff --git a/game.h b/game.h index 44222c2..6c58565 100644 --- a/game.h +++ b/game.h @@ -26,6 +26,7 @@ class Game { void async_read(); void handle_type(const boost::system::error_code& error, std::size_t bytes_transferred, uint8_t *type); + void handle_pos(); void handle_chunk(); void handle_message(); void handle_player(); -- cgit v1.2.3