Reimplemented the push/pop entity methods in RoomManager
Some accessors in Entity had to be made const, as they were being called from lambdas with const parameters.
This commit is contained in:
@@ -57,6 +57,37 @@ void RoomManager::UnloadIf(std::function<bool(std::pair<const int, RoomData cons
|
||||
}
|
||||
}
|
||||
|
||||
void RoomManager::PushEntity(Entity* entity) {
|
||||
if (!entity) {
|
||||
throw(std::runtime_error("Failed to push a null entity to a room"));
|
||||
}
|
||||
|
||||
RoomData* room = Get(entity->GetRoomIndex());
|
||||
|
||||
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<int, RoomData>::iterator it = elementMap.find(uid);
|
||||
|
||||
|
||||
@@ -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<bool(std::pair<const int, RoomData const&>)> fn);
|
||||
|
||||
void PushEntity(Entity* entity);
|
||||
void PopEntity(Entity const* entity);
|
||||
|
||||
//accessors and mutators
|
||||
RoomData* Get(int uid);
|
||||
RoomData* Get(std::string name);
|
||||
|
||||
Reference in New Issue
Block a user