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. --- game.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'game.cpp') 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; -- cgit v1.2.3