Added PushEntity() & PopEntity() to RoomManager

Calling them from server_character_methods.cpp.
This commit is contained in:
Kayne Ruse
2015-01-01 02:50:52 +11:00
parent 963aca218a
commit ebd8e54725
6 changed files with 57 additions and 2 deletions
+8
View File
@@ -36,4 +36,12 @@ function mapMaker.debugIsland(region)
end
end
function mapMaker.dirtLand(region)
for i = 1, mapSystem.Region.GetWidth(region) do
for j = 1, mapSystem.Region.GetHeight(region) do
mapSystem.Region.SetTile(region, i, j, 1, mapMaker.dirt)
end
end
end
return mapMaker
+4
View File
@@ -22,6 +22,10 @@ mapSystem.RegionPager.SetOnSave(roomSystem.Room.GetPager(overworld), mapSaver.Sa
mapSystem.RegionPager.SetOnCreate(roomSystem.Room.GetPager(overworld), mapMaker.debugIsland)
mapSystem.RegionPager.SetOnUnload(roomSystem.Room.GetPager(overworld), mapSaver.Save)
--Dirt Land
local dirtLand = roomSystem.RoomManager.CreateRoom("dirt land", "overworld.bmp")
roomSystem.Room.Initialize(dirtLand, mapSaver.Load, mapSaver.Save, mapMaker.dirtLand, mapSaver.Save)
print("Finished the lua script")
--[[
-2
View File
@@ -56,8 +56,6 @@ static int getPager(lua_State* L) {
static int initialize(lua_State* L) {
//set the members of the given room
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
room->SetName(lua_tostring(L, 2));
room->SetTileset(lua_tostring(L, 3));
//set the refs of these parameters (backwards, since it pops from the top of the stack)
room->GetPager()->SetUnloadReference(luaL_ref(L, LUA_REGISTRYINDEX));
+27
View File
@@ -24,6 +24,7 @@
#include "room_api.hpp"
#include <stdexcept>
#include <sstream>
//-------------------------
//public access methods
@@ -41,6 +42,32 @@ int RoomManager::Create(std::string roomName, std::string tileset) {
return counter++;
}
void RoomManager::PushEntity(Entity* entity) {
std::map<int, RoomData>::iterator it = elementMap.find(entity->GetRoomIndex());
if (it == elementMap.end()) {
std::ostringstream msg;
msg << "Failed to push entity; Room index not found: " << entity->GetRoomIndex() << std::endl;
throw(std::runtime_error(msg.str()));
}
it->second.entityList.push_back(entity);
}
void RoomManager::PopEntity(Entity* entity) {
std::map<int, RoomData>::iterator it = elementMap.find(entity->GetRoomIndex());
if (it == elementMap.end()) {
std::ostringstream msg;
msg << "Failed to pop entity; Room index not found: " << entity->GetRoomIndex() << std::endl;
throw(std::runtime_error(msg.str()));
}
it->second.entityList.remove_if([&](Entity* ptr) -> bool {
return ptr == entity;
});
}
void RoomManager::UnloadAll() {
elementMap.clear();
}
+4
View File
@@ -22,6 +22,7 @@
#ifndef ROOMMANAGER_HPP_
#define ROOMMANAGER_HPP_
#include "entity.hpp"
#include "room_data.hpp"
#include "singleton.hpp"
@@ -39,6 +40,9 @@ public:
//common public methods
int Create(std::string name, std::string tileset);
void PushEntity(Entity* entity);
void PopEntity(Entity* entity);
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn);
+14
View File
@@ -45,6 +45,9 @@ void ServerApplication::HandleCharacterCreate(CharacterPacket* const argPacket)
return;
}
//push this character to the rooms
roomMgr.PushEntity(characterMgr.Get(characterIndex));
//pump this character to all clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, characterIndex);
@@ -87,6 +90,7 @@ void ServerApplication::HandleCharacterDelete(CharacterPacket* const argPacket)
}
//delete the character
roomMgr.PopEntity(characterMgr.Get(characterIndex));
characterMgr.Delete(characterIndex);
//pump character delete
@@ -119,6 +123,9 @@ void ServerApplication::HandleCharacterLoad(CharacterPacket* const argPacket) {
return;
}
//push this character to the rooms
roomMgr.PushEntity(characterMgr.Get(characterIndex));
//pump this character to all clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, characterIndex);
@@ -150,6 +157,7 @@ void ServerApplication::HandleCharacterUnload(CharacterPacket* const argPacket)
}
//unload the character
roomMgr.PopEntity(characterData);
characterMgr.Unload(argPacket->characterIndex);
//pump character delete
@@ -190,11 +198,17 @@ void ServerApplication::HandleCharacterSetRoom(CharacterPacket* const argPacket)
return;
}
//pop from the rooms
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 rooms
roomMgr.PushEntity(characterData);
//update the clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, argPacket->characterIndex);