Updated RoomManager and the API
This commit is contained in:
@@ -21,37 +21,64 @@
|
|||||||
*/
|
*/
|
||||||
#include "room_manager.hpp"
|
#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>
|
#include <stdexcept>
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//public access methods
|
//public access methods
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
RoomData* RoomManager::CreateRoom(int uid) {
|
int RoomManager::CreateRoom(MapType mapType) {
|
||||||
//don't overwrite existing rooms
|
//create the room
|
||||||
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
|
RoomData* newRoom = new RoomData();
|
||||||
if (it != roomMap.end()) {
|
|
||||||
throw(std::runtime_error("Cannot overwrite an existing room"));
|
//set the state
|
||||||
}
|
|
||||||
roomMap[uid] = new RoomData();
|
|
||||||
//TODO: create room in the API
|
|
||||||
if (luaState) {
|
if (luaState) {
|
||||||
roomMap[uid]->pager.SetLuaState(luaState);
|
newRoom->pager.SetLuaState(luaState);
|
||||||
}
|
|
||||||
return roomMap[uid];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RoomData* RoomManager::UnloadRoom(int uid) {
|
//create the generator
|
||||||
//TODO: unload room in the API
|
newRoom->generator = [mapType]() -> BaseGenerator* {
|
||||||
delete roomMap[uid];
|
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++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int RoomManager::UnloadRoom(int uid) {
|
||||||
|
RoomData* room = FindRoom(uid);
|
||||||
|
if (!room) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete room->generator;
|
||||||
|
delete room;
|
||||||
roomMap.erase(uid);
|
roomMap.erase(uid);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
RoomData* RoomManager::GetRoom(int uid) {
|
RoomData* RoomManager::GetRoom(int uid) {
|
||||||
RoomData* ptr = FindRoom(uid);
|
RoomData* ptr = FindRoom(uid);
|
||||||
if (ptr) return ptr;
|
if (ptr) return ptr;
|
||||||
ptr = CreateRoom(uid);
|
int newIndex = CreateRoom(MapType::NONE);
|
||||||
return ptr;
|
return FindRoom(newIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
RoomData* RoomManager::FindRoom(int uid) {
|
RoomData* RoomManager::FindRoom(int uid) {
|
||||||
@@ -62,11 +89,7 @@ RoomData* RoomManager::FindRoom(int uid) {
|
|||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
RoomData* RoomManager::PushRoom(int uid, RoomData* room) {
|
int RoomManager::PushRoom(RoomData* room) {
|
||||||
//unload existing rooms with this index
|
roomMap[counter] = room;
|
||||||
std::map<int, RoomData*>::iterator it = roomMap.find(uid);
|
return counter++;
|
||||||
if (it != roomMap.end()) {
|
|
||||||
UnloadRoom(uid);
|
|
||||||
}
|
|
||||||
roomMap[uid] = room;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,12 +36,12 @@ public:
|
|||||||
~RoomManager() = default;
|
~RoomManager() = default;
|
||||||
|
|
||||||
//public access methods
|
//public access methods
|
||||||
RoomData* CreateRoom(int uid);
|
int CreateRoom(MapType);
|
||||||
RoomData* UnloadRoom(int uid);
|
int UnloadRoom(int uid);
|
||||||
|
|
||||||
RoomData* GetRoom(int uid);
|
RoomData* GetRoom(int uid);
|
||||||
RoomData* FindRoom(int uid);
|
RoomData* FindRoom(int uid);
|
||||||
RoomData* PushRoom(int uid, RoomData*);
|
int PushRoom(RoomData*);
|
||||||
|
|
||||||
//accessors and mutators
|
//accessors and mutators
|
||||||
std::map<int, RoomData*>* GetContainer() { return &roomMap; }
|
std::map<int, RoomData*>* GetContainer() { return &roomMap; }
|
||||||
@@ -52,6 +52,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::map<int, RoomData*> roomMap;
|
std::map<int, RoomData*> roomMap;
|
||||||
lua_State* luaState = nullptr;
|
lua_State* luaState = nullptr;
|
||||||
|
int counter = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -24,6 +24,8 @@
|
|||||||
#include "room_manager.hpp"
|
#include "room_manager.hpp"
|
||||||
#include "room_data.hpp"
|
#include "room_data.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
static int getRoom(lua_State* L) {
|
static int getRoom(lua_State* L) {
|
||||||
//get the room manager
|
//get the room manager
|
||||||
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX);
|
lua_pushstring(L, ROOM_MANAGER_PSEUDOINDEX);
|
||||||
@@ -41,9 +43,22 @@ static int createRoom(lua_State* L) {
|
|||||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||||
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
|
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) {
|
static int unloadRoom(lua_State* L) {
|
||||||
|
|||||||
Reference in New Issue
Block a user