Commit 2acb8192 by Anselme

added some useful methods to reset the map or teleport players

parent ffd3df34
......@@ -84,6 +84,8 @@ void Graphics::addPlayer(const PlayerAdd& msg, bool isMainPlayer)
void Graphics::removePlayer(const sf::String& name)
{
PlayerEntity* player = m_players[name];
if(player == m_main_player)
m_main_player = nullptr;
if(player != nullptr)
delete player;
m_players.erase(name);
......
......@@ -35,7 +35,8 @@ int main(int argc, char ** argv)
// launching server
Server* server = new Server(port);
server->initMap();
server->resetMap();
while(true)
server->update();
......
......@@ -47,6 +47,12 @@ bool MapInstance::isAvailable(sf::String id)
return (m_characters.find(id) == m_characters.end());
}
void MapInstance::respawnPlayers()
{
for(auto it : m_characters)
teleportPlayer(it.first, getRandomSpawn());
}
void MapInstance::addCharacter(CharacterData *charac)
{
m_characters[charac->id]=charac;
......@@ -57,6 +63,29 @@ void MapInstance::removeCharacter(const sf::String& id)
m_characters.erase(id);
}
bool MapInstance::teleportPlayer(const sf::String &id, sf::Vector2i pos)
{
if(m_characters.count(id) > 0)
{
CharacterData* player = m_characters[id];
PlayerRemove player_remove;
player_remove.id = id;
PlayerAdd player_add;
player_add.id = id;
player_add.color = player->color;
player_add.pos = pos;
player_add.turn_id = m_map->getTurn();
sf::Packet packet;
packet << PLAYER_REMOVE;
packet << player_remove;
packet << PLAYER_ADD;
packet << player_add;
m_server->getSocketHandler()->broadcastMessage(packet);
return true;
}
return false;
}
void MapInstance::pushPlayerAction(PlayerDiff p_diff)
{
std::lock_guard<std::mutex> guard(m_mutex);
......
......@@ -30,6 +30,7 @@ class MapInstance
MapDiff m_diff;
std::unordered_map<sf::String, CharacterData*> m_characters;
unsigned int m_current_turn_id;
std::vector<sf::Vector2i> m_spawns;
std::unordered_map<int, MapDiff*> m_mapdiffs;
std::mutex m_mutex;
public:
......@@ -42,10 +43,16 @@ public:
int getWidth(){return m_map->getWidth();}
int getHeight(){return m_map->getHeight();}
void addSpawn(sf::Vector2i spawnPos) { m_spawns.push_back(spawnPos); }
void resetSpawns() { m_spawns.clear(); }
void respawnPlayers();
sf::Vector2i getRandomSpawn() { return m_spawns.size() ? m_spawns[rand()%m_spawns.size()] : sf::Vector2i(1, 1); }
TileType getTile(sf::Vector2i pos){return m_map->getTile(pos);}
void addCharacter(CharacterData* charac);
void removeCharacter(const sf::String &id);
bool teleportPlayer(const sf::String &id, sf::Vector2i pos);
CharacterData* getCharacter(const sf::String &id){ return m_characters[id]; }
sf::Packet& packCharacterData(sf::Packet &packet);
......
......@@ -12,6 +12,8 @@
Server::Server(unsigned short port)
{
m_map = new MapInstance(this);
resetMap();
m_socket_handler = new SocketHandler(this,port);
m_socketUpdaterRunning = new bool(true);
new std::thread(Server::updateSockets, m_socket_handler, m_socketUpdaterRunning);
......@@ -27,8 +29,14 @@ void Server::destroy()
*m_socketUpdaterRunning = false;
}
void Server::initMap(){
void Server::resetMap(){
m_map->resetSpawns();
m_map->setMapData(MapKoban::getTestMap());
m_map->addSpawn(sf::Vector2i(3, 3));
m_map->addSpawn(sf::Vector2i(4, 3));
m_map->addSpawn(sf::Vector2i(4, 4));
m_map->addSpawn(sf::Vector2i(3, 4));
m_map->respawnPlayers();
}
void Server::updateSockets(SocketHandler* socket_handler, bool* running){
......
......@@ -36,12 +36,7 @@ public:
SocketHandler* getSocketHandler(){return m_socket_handler;}
static void updateSockets(SocketHandler* socket_handler, bool* running);
// PlayerAction resolveMove(sf::Vector2i pos, Direction dir);
// void stackAction(PlayerDiff p);
void initMap();
// sf::String findPlayer(sf::Vector2i pos);
// void filterMapDiff();
// void removeDisconnectedPlayers();
void resetMap();
};
#endif // SERVER_H
......@@ -104,9 +104,7 @@ void SocketHandler::handleSocketMessage(sf::TcpSocket& socket)
auth_answer.isOk = true;
new_charac->id = auth.name;
new_charac->color = auth.color;
do{
new_charac->position = sf::Vector2i(std::rand()%map->getWidth(), std::rand()%map->getHeight());
}while(map->getTile(new_charac->position) != FLOOR);
new_charac->position = map->getRandomSpawn();
m_server->getMap()->addCharacter(new_charac);
std::cout << "Spawning new player \"" << std::string(new_charac->id) << "\" at position (" << new_charac->position.x << "," << new_charac->position.y << ")" << std::endl;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment