Non-grid UDP movement

This commit is contained in:
Layla 2020-05-04 06:38:58 -04:00
parent 3ee6551013
commit 45673816bf
No known key found for this signature in database
GPG Key ID: A494D9357BA1BE31
5 changed files with 117 additions and 25 deletions

View File

@ -7,38 +7,60 @@
class GameEntity class GameEntity
{ {
public: public:
GameEntity(std::string,std::string,int,int); GameEntity(std::string,std::string,float,float);
std::string get_id(void); std::string get_id(void);
std::string get_type(void); std::string get_type(void);
int get_x(void); float get_x(void);
int get_y(void); float get_y(void);
std::string set_x(int); std::string set_x(float);
std::string set_y(int); std::string set_y(float);
void set_velocity(float, float);
std::string get_dump(void); std::string get_dump(void);
bool movement_tick(void);
private: private:
int pos_x; float pos_x;
int pos_y; float pos_y;
float velocity_x;
float velocity_y;
std::string id; std::string id;
std::string type; 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; id = entity_id;
type = entity_type; type = entity_type;
velocity_x = 0;
velocity_y = 0;
this->set_x(x); this->set_x(x);
this->set_y(y); this->set_y(y);
std::cout << "Entity created with id: '" << id << "' at " << x << "," << y << std::endl; 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; return pos_x;
} }
int GameEntity::get_y(void) float GameEntity::get_y(void)
{ {
return pos_y; 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'; 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; pos_x = x;
return this -> get_dump(); return this -> get_dump();
} }
std::string GameEntity::set_y(int y) std::string GameEntity::set_y(float y)
{ {
pos_y = y; pos_y = y;
return this -> get_dump(); return this -> get_dump();

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <sstream>
#include "gameentity.hpp" #include "gameentity.hpp"
@ -15,14 +16,17 @@ class GameMap
void set_tile(int,int,int); void set_tile(int,int,int);
std::string get_tile_dump(void); std::string get_tile_dump(void);
std::string get_entity_dump(void); std::string get_entity_dump(void);
std::string spawn_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,int,int); 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_x(std::string,std::string);
int get_entity_pos_y(std::string,std::string); int get_entity_pos_y(std::string,std::string);
int get_size_x(void); int get_size_x(void);
int get_size_y(void); int get_size_y(void);
bool entity_exists(std::string, std::string); bool entity_exists(std::string, std::string);
bool remove_entity(std::string, std::string); bool remove_entity(std::string, std::string);
std::string world_tick(void);
private: private:
int ** map_data; 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); GameEntity entity = GameEntity(entity_id, entity_type, x, y);
entities.push_back(entity); entities.push_back(entity);
return entity.get_dump(); 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++) 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 ""; 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) bool GameMap::valid_x_y(int x, int y)
{ {
if(x < size_x && x >= 0) if(x < size_x && x >= 0)
@ -118,6 +146,23 @@ bool GameMap::valid_x_y(int x, int y)
return false; 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) int GameMap::get_tile(int x, int y)
{ {
if(this->valid_x_y(x,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; return false;
} }
int GameMap::get_entity_pos_x(std::string entity_id, std::string entity_type) int GameMap::get_entity_pos_x(std::string entity_id, std::string entity_type)
{ {
for(int i = 0; i < entities.size(); i++) for(int i = 0; i < entities.size(); i++)

View File

@ -7,6 +7,9 @@
#include <sstream> #include <sstream>
#include <cstring> #include <cstring>
#include <unistd.h>
#include <pthread.h>
#include "gamemap.hpp" #include "gamemap.hpp"
using namespace std; using namespace std;
@ -42,8 +45,8 @@ namespace gameserver
{ {
int peer_id = event->peer -> incomingPeerID; int peer_id = event->peer -> incomingPeerID;
int move_x = 0; float move_x = 0;
int move_y = 0; float move_y = 0;
//Remove id marker from packet data //Remove id marker from packet data
std::string data_input((char*)event->packet->data); std::string data_input((char*)event->packet->data);
@ -53,14 +56,14 @@ namespace gameserver
std::stringstream ss(data_input); std::stringstream ss(data_input);
std::string tempString; std::string tempString;
std::getline(ss, tempString, ','); std::getline(ss, tempString, ',');
move_x = std::stoi(tempString); move_x = std::stof(tempString);
std::getline(ss, tempString, '\n'); std::getline(ss, tempString, '\n');
move_y = std::stoi(tempString); move_y = std::stof(tempString);
//Update player position //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(); 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); enet_host_broadcast(server, 0, packet);
} }
@ -174,6 +177,26 @@ namespace gameserver
break; 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 #endif

View File

@ -42,6 +42,9 @@ int main (int argc, char ** argv)
return 1; return 1;
} }
//Start ticker
gameserver::StartTicker();
//Run main game server loop //Run main game server loop
std::cout << "Awaiting connections..." << std::endl; std::cout << "Awaiting connections..." << std::endl;
while(game_is_running) while(game_is_running)

View File

@ -39,12 +39,12 @@ TEST(EntityTest, GetBasicDump)
{ {
GameEntity entity = CreateTestEntity(); GameEntity entity = CreateTestEntity();
//Test intial location (0,0) //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) //Test non-intial location (1,2)
entity.set_x(1); entity.set_x(1);
entity.set_y(2); 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 //Test GameMap Object