From 7add4dd07e17c5a6ba0a2a1d9700d72055b812ae Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 13 Jun 2011 13:19:01 +0200 Subject: Send players and updated positions to other players. --- common | 2 +- connection.cpp | 7 +++++++ connection.h | 3 +++ server.cpp | 18 ++++++++++++++---- 4 files changed, 25 insertions(+), 5 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/connection.cpp b/connection.cpp index 3b5076c..613eed0 100644 --- a/connection.cpp +++ b/connection.cpp @@ -4,7 +4,10 @@ using std::min; using std::max; +uint32_t Connection::next_id = 0; + Connection::Connection(boost::asio::io_service& io_service_) : io_service(io_service_), socket(io_service_) { + id = ++next_id; } float Connection::distance(float px, float pz) { @@ -57,3 +60,7 @@ std::set > Connection::check_chunks() { return new_chunks; } + +uint32_t Connection::get_id() { + return id; +} diff --git a/connection.h b/connection.h index 78603e4..cba5031 100644 --- a/connection.h +++ b/connection.h @@ -10,6 +10,8 @@ class Connection { private: + static uint32_t next_id; + uint32_t id; boost::asio::io_service& io_service; public: @@ -25,6 +27,7 @@ class Connection { /* data */ Vector3 pos; std::set > chunk_indices; + uint32_t get_id(); }; #endif diff --git a/server.cpp b/server.cpp index 77956fa..891708c 100644 --- a/server.cpp +++ b/server.cpp @@ -36,11 +36,14 @@ void Server::handle_connect(Connection::p connection) { % connection->socket.remote_endpoint().address().to_string()).str()); m.send(connection->socket); - message::Player p(0, Vector3(0, 52, 0), "foooooooooooooooooooooo"); - p.send(connection->socket); + message::Player player(connection->get_id(), connection->pos, connection->socket.local_endpoint().address().to_string()); - message::Player p2(1, Vector3(5, 50, 5), "b"); - p2.send(connection->socket); + for(std::list::iterator it = clients.begin(); it != clients.end(); it++) { + player.send((*it)->socket); + Connection::p c = *it; + message::Player other(c->get_id(), c->pos, c->socket.local_endpoint().address().to_string()); + other.send(connection->socket); + } clients.push_back(connection); @@ -111,6 +114,13 @@ void Server::handle_pos(Connection::p c) { chunk.set_data(obj->heights); chunk.send(c->socket); } + for(std::list::iterator it = clients.begin(); it != clients.end(); it++) { + if(*it == c) + continue; + + message::Pos p(c->get_id(), c->pos); + p.send((*it)->socket); + } } void Server::handle_chunk(Connection::p c) { -- cgit v1.2.3