From 45673816bf69e2563a79ebb542e25dc39fccc66a Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Mon, 4 May 2020 06:38:58 -0400 Subject: [PATCH] Non-grid UDP movement --- server/gameentity.hpp | 46 ++++++++++++++++++++++++++---------- server/gamemap.hpp | 54 +++++++++++++++++++++++++++++++++++++++---- server/gameserver.hpp | 35 +++++++++++++++++++++++----- server/main.cpp | 3 +++ server/tests.cpp | 4 ++-- 5 files changed, 117 insertions(+), 25 deletions(-) diff --git a/server/gameentity.hpp b/server/gameentity.hpp index dd6fb44..78be9fb 100644 --- a/server/gameentity.hpp +++ b/server/gameentity.hpp @@ -7,38 +7,60 @@ class GameEntity { public: - GameEntity(std::string,std::string,int,int); + GameEntity(std::string,std::string,float,float); std::string get_id(void); std::string get_type(void); - int get_x(void); - int get_y(void); - std::string set_x(int); - std::string set_y(int); + float get_x(void); + float get_y(void); + std::string set_x(float); + std::string set_y(float); + void set_velocity(float, float); std::string get_dump(void); + bool movement_tick(void); private: - int pos_x; - int pos_y; + float pos_x; + float pos_y; + float velocity_x; + float velocity_y; std::string id; std::string type; }; -GameEntity::GameEntity(std::string entity_id, std::string entity_type, int x = 0, int y = 0) +GameEntity::GameEntity(std::string entity_id, std::string entity_type, float x = 0, float y = 0) { id = entity_id; type = entity_type; + velocity_x = 0; + velocity_y = 0; this->set_x(x); this->set_y(y); std::cout << "Entity created with id: '" << id << "' at " << x << "," << y << std::endl; } +bool GameEntity::movement_tick() +{ + if(velocity_x != 0 || velocity_y != 0) + { + set_x(pos_x + velocity_x); + set_y(pos_y + velocity_y); + return true; + } + return false; +} -int GameEntity::get_x(void) +void GameEntity::set_velocity(float x, float y) +{ + velocity_x = x; + velocity_y = y; +} + +float GameEntity::get_x(void) { return pos_x; } -int GameEntity::get_y(void) +float GameEntity::get_y(void) { return pos_y; } @@ -48,12 +70,12 @@ std::string GameEntity::get_dump(void) return std::to_string(pos_x) + "," + std::to_string(pos_y) + "," + type + ":" + id + '\n'; } -std::string GameEntity::set_x(int x) +std::string GameEntity::set_x(float x) { pos_x = x; return this -> get_dump(); } -std::string GameEntity::set_y(int y) +std::string GameEntity::set_y(float y) { pos_y = y; return this -> get_dump(); diff --git a/server/gamemap.hpp b/server/gamemap.hpp index d152ae8..f5dda0a 100644 --- a/server/gamemap.hpp +++ b/server/gamemap.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "gameentity.hpp" @@ -15,14 +16,17 @@ class GameMap void set_tile(int,int,int); std::string get_tile_dump(void); std::string get_entity_dump(void); - std::string spawn_entity(std::string,std::string,int,int); - std::string move_entity(std::string,std::string,int,int); + std::string spawn_entity(std::string,std::string,float,float); + std::string move_entity(std::string,std::string,float,float); + void set_entity_velocity(std::string,std::string,float,float); + std::string move_enitity_relatively(std::string,std::string,float,float); int get_entity_pos_x(std::string,std::string); int get_entity_pos_y(std::string,std::string); int get_size_x(void); int get_size_y(void); bool entity_exists(std::string, std::string); bool remove_entity(std::string, std::string); + std::string world_tick(void); private: int ** map_data; @@ -58,14 +62,26 @@ GameMap::GameMap(int x, int y) } -std::string GameMap::spawn_entity(std::string entity_id, std::string entity_type, int x = 0, int y = 0) +std::string GameMap::spawn_entity(std::string entity_id, std::string entity_type, float x = 0, float y = 0) { GameEntity entity = GameEntity(entity_id, entity_type, x, y); entities.push_back(entity); return entity.get_dump(); } -std::string GameMap::move_entity(std::string entity_id, std::string entity_type, int x = 0, int y = 0) +std::string GameMap::move_enitity_relatively(std::string entity_id, std::string entity_type, float x = 0, float y = 0) +{ + for(int i = 0; i < entities.size(); i++) + { + if(entities[i].get_id() == entity_id && entities[i].get_type() == entity_type) + { + return move_entity(entity_id, entity_type, x - entities[i].get_x(), y - entities[i].get_y()); + } + } + return ""; +} + +std::string GameMap::move_entity(std::string entity_id, std::string entity_type, float x = 0, float y = 0) { for(int i = 0; i < entities.size(); i++) { @@ -106,6 +122,18 @@ std::string GameMap::move_entity(std::string entity_id, std::string entity_type, return ""; } +void GameMap::set_entity_velocity(std::string entity_id, std::string entity_type, float x = 0, float y = 0) +{ + for(int i = 0; i < entities.size(); i++) + { + if(entities[i].get_id() == entity_id && entities[i].get_type() == entity_type) + { + GameEntity* entity = &entities[i]; + entity->set_velocity(x,y); + } + } +} + bool GameMap::valid_x_y(int x, int y) { if(x < size_x && x >= 0) @@ -118,6 +146,23 @@ bool GameMap::valid_x_y(int x, int y) return false; } +std::string GameMap::world_tick(void) +{ + std::string dump = ""; + for(int i = 0; i < entities.size(); i++) + { + if(entities[i].movement_tick()) + { + dump += entities[i].get_dump(); + } + } + if(dump != "") + { + std::cout << dump << std::endl; + } + return dump; +} + int GameMap::get_tile(int x, int y) { if(this->valid_x_y(x,y)) @@ -214,7 +259,6 @@ bool GameMap::remove_entity(std::string entity_id, std::string entity_type) return false; } - int GameMap::get_entity_pos_x(std::string entity_id, std::string entity_type) { for(int i = 0; i < entities.size(); i++) diff --git a/server/gameserver.hpp b/server/gameserver.hpp index a9abfa3..178673e 100644 --- a/server/gameserver.hpp +++ b/server/gameserver.hpp @@ -7,6 +7,9 @@ #include #include +#include +#include + #include "gamemap.hpp" using namespace std; @@ -42,8 +45,8 @@ namespace gameserver { int peer_id = event->peer -> incomingPeerID; - int move_x = 0; - int move_y = 0; + float move_x = 0; + float move_y = 0; //Remove id marker from packet data std::string data_input((char*)event->packet->data); @@ -53,14 +56,14 @@ namespace gameserver std::stringstream ss(data_input); std::string tempString; std::getline(ss, tempString, ','); - move_x = std::stoi(tempString); + move_x = std::stof(tempString); std::getline(ss, tempString, '\n'); - move_y = std::stoi(tempString); + move_y = std::stof(tempString); //Update player position - std::string resp = "2|" + gamemap.move_entity(usernames[peer_id], "player", move_x, move_y); + std::string resp = "2|" + gamemap.move_enitity_relatively(usernames[peer_id], "player", move_x, move_y); const char* data = resp.c_str(); - ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, ENET_PACKET_FLAG_RELIABLE); + ENetPacket* packet = enet_packet_create(data, strlen(data) + 1, 0); enet_host_broadcast(server, 0, packet); } @@ -174,6 +177,26 @@ namespace gameserver break; } } + + pthread_t ticker_thread; + + void * GameTicker(void *) + { + while(game_is_running) + { + usleep(50000); + std::string data_string = "2|"+gamemap.world_tick(); + const char* data = data_string.c_str(); + ENetPacket* packet = enet_packet_create(data, strlen(data), ENET_PACKET_FLAG_UNSEQUENCED); + enet_host_broadcast(server, 0, packet); + } + return 0; + } + + void StartTicker() + { + pthread_create(&ticker_thread, NULL, GameTicker, NULL); + } } #endif \ No newline at end of file diff --git a/server/main.cpp b/server/main.cpp index 148b1f2..f06effd 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -42,6 +42,9 @@ int main (int argc, char ** argv) return 1; } + //Start ticker + gameserver::StartTicker(); + //Run main game server loop std::cout << "Awaiting connections..." << std::endl; while(game_is_running) diff --git a/server/tests.cpp b/server/tests.cpp index b2a594a..e160ec8 100644 --- a/server/tests.cpp +++ b/server/tests.cpp @@ -39,12 +39,12 @@ TEST(EntityTest, GetBasicDump) { GameEntity entity = CreateTestEntity(); //Test intial location (0,0) - EXPECT_EQ(entity.get_dump(), std::string("0,0," + TEST_ENTITY_TYPE + ":" + TEST_ENTITY_ID + "\n")); + EXPECT_EQ(entity.get_dump(), std::string("0.000000,0.000000," + TEST_ENTITY_TYPE + ":" + TEST_ENTITY_ID + "\n")); //Test non-intial location (1,2) entity.set_x(1); entity.set_y(2); - EXPECT_EQ(entity.get_dump(), std::string("1,2," + TEST_ENTITY_TYPE + ":" + TEST_ENTITY_ID + "\n")); + EXPECT_EQ(entity.get_dump(), std::string("1.000000,2.000000," + TEST_ENTITY_TYPE + ":" + TEST_ENTITY_ID + "\n")); } //Test GameMap Object