finished rewriting the room API
This commit is contained in:
@@ -29,21 +29,36 @@ static int getPager(lua_State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create(lua_State* L) {
|
static int setRoomName(lua_State* L) {
|
||||||
//EMPTY
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
//NOTE: This can be used to set defaults for the pager
|
room->SetRoomName(lua_tostring(L, 2));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int unload(lua_State* L) {
|
static int getRoomName(lua_State* L) {
|
||||||
//EMPTY
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
|
lua_pushstring(L, room->GetRoomName().c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int setTilesetName(lua_State* L) {
|
||||||
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
|
room->SetTilesetName(lua_tostring(L, 2));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int getTilesetName(lua_State* L) {
|
||||||
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
|
lua_pushstring(L, room->GetTilesetName().c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static const luaL_Reg roomLib[] = {
|
static const luaL_Reg roomLib[] = {
|
||||||
{"GetPager",getPager},
|
{"GetPager",getPager},
|
||||||
{"Create", create},
|
{"SetRoomName", setRoomName},
|
||||||
{"Unload", unload},
|
{"GetRoomName", getRoomName},
|
||||||
|
{"SetTileset", setTilesetName},
|
||||||
|
{"GetTileset", getTilesetName},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ public:
|
|||||||
//accessors and mutators
|
//accessors and mutators
|
||||||
RegionPagerLua* GetPager() { return &pager; }
|
RegionPagerLua* GetPager() { return &pager; }
|
||||||
|
|
||||||
|
std::string SetRoomName(std::string s) { return roomName = s; }
|
||||||
|
std::string GetRoomName() { return roomName; }
|
||||||
|
|
||||||
std::string SetTilesetName(std::string s) { return tilesetName = s; }
|
std::string SetTilesetName(std::string s) { return tilesetName = s; }
|
||||||
std::string GetTilesetName() { return tilesetName; }
|
std::string GetTilesetName() { return tilesetName; }
|
||||||
|
|
||||||
@@ -43,7 +46,10 @@ private:
|
|||||||
|
|
||||||
//members
|
//members
|
||||||
RegionPagerLua pager;
|
RegionPagerLua pager;
|
||||||
|
std::string roomName;
|
||||||
std::string tilesetName;
|
std::string tilesetName;
|
||||||
|
//TODO: pass the room name & tileset name to the clients
|
||||||
|
//TODO: lua references i.e. create, unload, etc.
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -30,74 +30,44 @@
|
|||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
int RoomManager::Create() {
|
int RoomManager::Create() {
|
||||||
/* //create the room
|
//create the room
|
||||||
RoomData* newRoom =
|
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
|
||||||
newRoom->pager.SetLuaState(luaState);
|
newRoom->pager.SetLuaState(lua);
|
||||||
|
|
||||||
//register the room
|
|
||||||
roomMap[counter] = newRoom;
|
|
||||||
|
|
||||||
//API hook
|
|
||||||
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
|
||||||
lua_getfield(luaState, -1, "Create");
|
|
||||||
lua_pushlightuserdata(luaState, newRoom);
|
|
||||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
|
||||||
}
|
|
||||||
lua_pop(luaState, 1);
|
|
||||||
|
|
||||||
//finish the routine
|
//finish the routine
|
||||||
*/ return counter++;
|
return counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
int RoomManager::Load() {
|
int RoomManager::Load() {
|
||||||
//TODO: RoomManageR::Load()
|
//TODO: RoomManager::Load()
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int RoomManager::Save(int uid) {
|
int RoomManager::Save(int uid) {
|
||||||
//TODO: RoomManageR::Save(uid)
|
//TODO: RoomManager::Save(uid)
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RoomManager::Unload(int uid) {
|
void RoomManager::Unload(int uid) {
|
||||||
/* //find the room
|
//find the room
|
||||||
RoomData* room = elementMap.find(uid);
|
std::map<int, RoomData>::iterator it = elementMap.find(uid);
|
||||||
if (room == elementMap.end()) {
|
if (it == elementMap.end()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//API hook
|
|
||||||
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
|
||||||
lua_getfield(luaState, -1, "Unload");
|
|
||||||
lua_pushlightuserdata(luaState, room);
|
|
||||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
|
||||||
}
|
|
||||||
lua_pop(luaState, 1);
|
|
||||||
|
|
||||||
//free the memory
|
//free the memory
|
||||||
delete room;
|
elementMap.erase(uid);
|
||||||
roomMap.erase(uid);
|
}
|
||||||
*/}
|
|
||||||
|
|
||||||
void RoomManager::Delete(int uid) {
|
void RoomManager::Delete(int uid) {
|
||||||
//
|
//TODO: RoomManager::Delete(int uid)
|
||||||
|
//NOTE: aliased to RoomManager::Unload(int uid)
|
||||||
|
Unload(uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RoomManager::UnloadAll() {
|
void RoomManager::UnloadAll() {
|
||||||
/* lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
elementMap.clear();
|
||||||
|
|
||||||
for (auto& it : roomMap) {
|
|
||||||
//API hook
|
|
||||||
lua_getfield(luaState, -1, "Unload");
|
|
||||||
lua_pushlightuserdata(luaState, it.second);
|
|
||||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
lua_pop(luaState, 1);
|
|
||||||
roomMap.clear();
|
|
||||||
*/}
|
|
||||||
|
|
||||||
void RoomManager::UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) {
|
void RoomManager::UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) {
|
||||||
std::map<int, RoomData>::iterator it = elementMap.begin();
|
std::map<int, RoomData>::iterator it = elementMap.begin();
|
||||||
|
|||||||
@@ -23,34 +23,34 @@
|
|||||||
|
|
||||||
#include "room_manager.hpp"
|
#include "room_manager.hpp"
|
||||||
|
|
||||||
#include <string>
|
int createRoom(lua_State* L) {
|
||||||
/*
|
//create & get the room
|
||||||
static int getRoom(lua_State* L) {
|
RoomManager& roomMgr = RoomManager::GetSingleton();
|
||||||
//find, push and return the room
|
int uid = roomMgr.Create();
|
||||||
RoomData* room = RoomManager::GetSingleton().GetRoom(lua_tointeger(L, -2));
|
RoomData* room = roomMgr.Get(uid);
|
||||||
lua_pushlightuserdata(L, reinterpret_cast<void*>(room));
|
|
||||||
return 1;
|
//setup the room
|
||||||
|
//TODO: room parameters only set via lua, fix this
|
||||||
|
room->SetRoomName(lua_tostring(L, 1));
|
||||||
|
room->SetTilesetName(lua_tostring(L, 2));
|
||||||
|
|
||||||
|
//return room, uid
|
||||||
|
lua_pushlightuserdata(L, static_cast<void*>(room));
|
||||||
|
lua_pushinteger(L, uid);
|
||||||
|
|
||||||
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int createRoom(lua_State* L) {
|
int unloadRoom(lua_State* L) {
|
||||||
//TODO: check parameter count for the glue functions
|
//TODO: check authorization for room deletion
|
||||||
|
RoomManager& roomMgr = RoomManager::GetSingleton();
|
||||||
//create, find and return the room
|
roomMgr.Unload(lua_tointeger(L, 1));
|
||||||
int uid = RoomManager::GetSingleton().CreateRoom();
|
|
||||||
lua_pushlightuserdata(L, RoomManager::GetSingleton().FindRoom(uid));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int unloadRoom(lua_State* L) {
|
|
||||||
//unload the specified room
|
|
||||||
RoomManager::GetSingleton().UnloadRoom(lua_tointeger(L, -2));
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
static const luaL_Reg roomManagerLib[] = {
|
static const luaL_Reg roomManagerLib[] = {
|
||||||
// {"GetRoom",getRoom},
|
{"CreateRoom", createRoom},
|
||||||
// {"CreateRoom",createRoom},
|
{"UnloadRoom", unloadRoom},
|
||||||
// {"UnloadRoom",unloadRoom},
|
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user