Updated RoomManager and the API

This commit is contained in:
Kayne Ruse
2014-06-21 19:02:43 +10:00
parent a64411a567
commit e19b6fbc23
3 changed files with 66 additions and 27 deletions
+45 -22
View File
@@ -21,37 +21,64 @@
*/
#include "room_manager.hpp"
//the generator types
#include "overworld_generator.hpp"
#include "ruins_generator.hpp"
#include "towers_generator.hpp"
#include "forests_generator.hpp"
#include "caves_generator.hpp"
#include <stdexcept>
//-------------------------
//public access methods
//-------------------------
RoomData* RoomManager::CreateRoom(int uid) {
//don't overwrite existing rooms
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
if (it != roomMap.end()) {
throw(std::runtime_error("Cannot overwrite an existing room"));
}
roomMap[uid] = new RoomData();
//TODO: create room in the API
int RoomManager::CreateRoom(MapType mapType) {
//create the room
RoomData* newRoom = new RoomData();
//set the state
if (luaState) {
roomMap[uid]->pager.SetLuaState(luaState);
newRoom->pager.SetLuaState(luaState);
}
return roomMap[uid];
//create the generator
newRoom->generator = [mapType]() -> BaseGenerator* {
switch(mapType) {
case MapType::NONE: //use overworld as a default
case MapType::OVERWORLD: return new OverworldGenerator();
case MapType::RUINS: return new RuinsGenerator();
case MapType::TOWERS: return new TowersGenerator();
case MapType::FORESTS: return new ForestsGenerator();
case MapType::CAVES: return new CavesGenerator();
}
throw(std::runtime_error("Failed to set the room's generator"));
}();
//finish the routine
roomMap[counter] = newRoom;
return counter++;
}
RoomData* RoomManager::UnloadRoom(int uid) {
//TODO: unload room in the API
delete roomMap[uid];
int RoomManager::UnloadRoom(int uid) {
RoomData* room = FindRoom(uid);
if (!room) {
return -1;
}
delete room->generator;
delete room;
roomMap.erase(uid);
return 0;
}
RoomData* RoomManager::GetRoom(int uid) {
RoomData* ptr = FindRoom(uid);
if (ptr) return ptr;
ptr = CreateRoom(uid);
return ptr;
int newIndex = CreateRoom(MapType::NONE);
return FindRoom(newIndex);
}
RoomData* RoomManager::FindRoom(int uid) {
@@ -62,11 +89,7 @@ RoomData* RoomManager::FindRoom(int uid) {
return it->second;
}
RoomData* RoomManager::PushRoom(int uid, RoomData* room) {
//unload existing rooms with this index
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
if (it != roomMap.end()) {
UnloadRoom(uid);
}
roomMap[uid] = room;
int RoomManager::PushRoom(RoomData* room) {
roomMap[counter] = room;
return counter++;
}
+4 -3
View File
@@ -36,12 +36,12 @@ public:
~RoomManager() = default;
//public access methods
RoomData* CreateRoom(int uid);
RoomData* UnloadRoom(int uid);
int CreateRoom(MapType);
int UnloadRoom(int uid);
RoomData* GetRoom(int uid);
RoomData* FindRoom(int uid);
RoomData* PushRoom(int uid, RoomData*);
int PushRoom(RoomData*);
//accessors and mutators
std::map<int, RoomData*>* GetContainer() { return &roomMap; }
@@ -52,6 +52,7 @@ public:
private:
std::map<int, RoomData*> roomMap;
lua_State* luaState = nullptr;
int counter = 0;
};
#endif
+17 -2
View File
@@ -24,6 +24,8 @@
#include "room_manager.hpp"
#include "room_data.hpp"
#include <string>
static int getRoom(lua_State* L) {
//get the room manager
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX);
@@ -41,9 +43,22 @@ static int createRoom(lua_State* L) {
lua_gettable(L, LUA_REGISTRYINDEX);
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
//TODO: create room
//determine the specified room type
MapType mapType = [L]() -> MapType {
if (std::string("overworld") == lua_tostring(L, -2)) return MapType::OVERWORLD;
if (std::string("ruins") == lua_tostring(L, -2)) return MapType::RUINS;
if (std::string("towers") == lua_tostring(L, -2)) return MapType::TOWERS;
if (std::string("forests") == lua_tostring(L, -2)) return MapType::FORESTS;
if (std::string("caves") == lua_tostring(L, -2)) return MapType::CAVES;
return MapType::NONE;
}();
return 0;
//create the room
int newIndex = roomMgr->CreateRoom(mapType);
//return the index
lua_pushinteger(L, newIndex);
return 1;
}
static int unloadRoom(lua_State* L) {