diff --git a/server/entities/entity.cpp b/server/entities/entity.cpp index fcfa8aa..5587e81 100644 --- a/server/entities/entity.cpp +++ b/server/entities/entity.cpp @@ -33,14 +33,14 @@ Vector2 Entity::SetMotion(Vector2 v) { return motion = v; } -int Entity::GetRoomIndex() { +int Entity::GetRoomIndex() const { return roomIndex; } -Vector2 Entity::GetOrigin() { +Vector2 Entity::GetOrigin() const { return origin; } -Vector2 Entity::GetMotion() { +Vector2 Entity::GetMotion() const { return motion; } \ No newline at end of file diff --git a/server/entities/entity.hpp b/server/entities/entity.hpp index f71fbf9..6874a7e 100644 --- a/server/entities/entity.hpp +++ b/server/entities/entity.hpp @@ -32,9 +32,9 @@ public: Vector2 SetOrigin(Vector2 v); Vector2 SetMotion(Vector2 v); - int GetRoomIndex(); - Vector2 GetOrigin(); - Vector2 GetMotion(); + int GetRoomIndex() const; + Vector2 GetOrigin() const; + Vector2 GetMotion() const; protected: Entity() = default; diff --git a/server/rooms/room_manager.cpp b/server/rooms/room_manager.cpp index 56a3f65..b173ed8 100644 --- a/server/rooms/room_manager.cpp +++ b/server/rooms/room_manager.cpp @@ -57,6 +57,37 @@ void RoomManager::UnloadIf(std::functionGetRoomIndex()); + + if (!room) { + throw(std::runtime_error("Failed to push an entity to a non-existant room")); + } + + room->entityList.push_back(entity); +} + +void RoomManager::PopEntity(Entity const* entity) { + //NOTE: to pop an entity from a room, the entity must first exist + if (!entity) { + throw(std::runtime_error("Failed to pop a null entity to a room")); + } + + RoomData* room = Get(entity->GetRoomIndex()); + + if (!room) { + throw(std::runtime_error("Failed to pop an entity to a non-existant room")); + } + + room->entityList.remove_if([entity](Entity* ptr) { + return entity == ptr; + }); +} + RoomData* RoomManager::Get(int uid) { std::map::iterator it = elementMap.find(uid); diff --git a/server/rooms/room_manager.hpp b/server/rooms/room_manager.hpp index 8bf1cc6..17d8e53 100644 --- a/server/rooms/room_manager.hpp +++ b/server/rooms/room_manager.hpp @@ -22,6 +22,7 @@ #ifndef ROOMMANAGER_HPP_ #define ROOMMANAGER_HPP_ +#include "entity.hpp" #include "room_data.hpp" #include "singleton.hpp" @@ -42,6 +43,9 @@ public: void UnloadAll(); void UnloadIf(std::function)> fn); + void PushEntity(Entity* entity); + void PopEntity(Entity const* entity); + //accessors and mutators RoomData* Get(int uid); RoomData* Get(std::string name); diff --git a/server/server_character_methods.cpp b/server/server_character_methods.cpp index d123d8e..610ec2d 100644 --- a/server/server_character_methods.cpp +++ b/server/server_character_methods.cpp @@ -45,6 +45,9 @@ void ServerApplication::HandleCharacterCreate(CharacterPacket* const argPacket) return; } + //push to the rooms + roomMgr.PushEntity(characterMgr.Get(characterIndex)); + //pump this character to all clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, characterIndex); @@ -86,6 +89,9 @@ void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket) return; } + //pop from the rooms + roomMgr.PopEntity(characterMgr.Get(characterIndex)); + //delete the character characterMgr.Delete(characterIndex); @@ -119,6 +125,9 @@ void ServerApplication::HandleCharacterLoad(CharacterPacket* const argPacket) { return; } + //push to the rooms + roomMgr.PushEntity(characterMgr.Get(characterIndex)); + //pump this character to all clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, characterIndex); @@ -149,6 +158,9 @@ void ServerApplication::HandleCharacterUnload(CharacterPacket* const argPacket) return; } + //pop from the rooms + roomMgr.PopEntity(characterData); + //unload the character characterMgr.Unload(argPacket->characterIndex); @@ -190,11 +202,17 @@ void ServerApplication::HandleCharacterSetRoom(CharacterPacket* const argPacket) return; } + //pop from the old room + roomMgr.PopEntity(characterData); + //set the character's room, zero it's origin, zero it's motion characterData->SetRoomIndex(argPacket->roomIndex); characterData->SetOrigin({0, 0}); characterData->SetMotion({0, 0}); + //push to the new room + roomMgr.PushEntity(characterData); + //update the clients CharacterPacket newPacket; CopyCharacterToPacket(&newPacket, argPacket->characterIndex); diff --git a/server/server_methods.cpp b/server/server_methods.cpp index e8f197f..7222ef0 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -149,6 +149,9 @@ void ServerApplication::FullCharacterUnload(int index) { return false; } + //pop from the rooms + roomMgr.PopEntity(&character.second); + //pump character unload CharacterPacket newPacket; newPacket.type = SerialPacketType::CHARACTER_DELETE;