Updated room system and room API, more to come

There's a lot of verbosity in the scripts, so it might be beneficial to
redice that at some point.
This commit is contained in:
Kayne Ruse
2014-12-31 04:52:10 +11:00
parent f9c19a630d
commit 8e50be24d4
8 changed files with 93 additions and 46 deletions
+8 -7
View File
@@ -25,25 +25,25 @@
static int setRoomName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
room->SetRoomName(lua_tostring(L, 2));
room->SetName(lua_tostring(L, 2));
return 0;
}
static int getRoomName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
lua_pushstring(L, room->GetRoomName().c_str());
lua_pushstring(L, room->GetName().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));
room->SetTileset(lua_tostring(L, 2));
return 0;
}
static int getTilesetName(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
lua_pushstring(L, room->GetTilesetName().c_str());
lua_pushstring(L, room->GetTileset().c_str());
return 1;
}
@@ -56,7 +56,8 @@ 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->SetRoomName(lua_tostring(L, 2));
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));
@@ -70,8 +71,8 @@ static int initialize(lua_State* L) {
static const luaL_Reg roomLib[] = {
{"GetPager",getPager},
{"SetRoomName", setRoomName},
{"GetRoomName", getRoomName},
{"SetName", setRoomName},
{"GetName", getRoomName},
{"SetTileset", setTilesetName},
{"GetTileset", getTilesetName},
{"Initialize", initialize},
+4 -4
View File
@@ -21,19 +21,19 @@
*/
#include "room_data.hpp"
std::string RoomData::SetRoomName(std::string s) {
std::string RoomData::SetName(std::string s) {
return roomName = s;
}
std::string RoomData::GetRoomName() {
std::string RoomData::GetName() {
return roomName;
}
std::string RoomData::SetTilesetName(std::string s) {
std::string RoomData::SetTileset(std::string s) {
return tilesetName = s;
}
std::string RoomData::GetTilesetName() {
std::string RoomData::GetTileset() {
return tilesetName;
}
+6 -4
View File
@@ -40,15 +40,17 @@ public:
~RoomData() = default;
//accessors and mutators
std::string SetRoomName(std::string s);
std::string GetRoomName();
std::string SetName(std::string);
std::string GetName();
std::string SetTilesetName(std::string s);
std::string GetTilesetName();
std::string SetTileset(std::string);
std::string GetTileset();
RegionPagerLua* GetPager();
std::list<Entity*>* GetEntityList();
//TODO: triggers for unload, save, per-second, player enter, player exit, etc.
private:
friend class RoomManager;
+20 -16
View File
@@ -29,27 +29,18 @@
//public access methods
//-------------------------
int RoomManager::Create(std::string roomName) {
int RoomManager::Create(std::string roomName, std::string tileset) {
//create the room
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element
newRoom->SetRoomName(roomName);
newRoom->SetName(roomName);
newRoom->SetTileset(tileset);
newRoom->pager.SetLuaState(lua);
//finish the routine
return counter++;
}
void RoomManager::Unload(int uid) {
//find the room
std::map<int, RoomData>::iterator it = elementMap.find(uid);
if (it == elementMap.end()) {
return;
}
//free the memory
elementMap.erase(uid);
}
void RoomManager::UnloadAll() {
elementMap.clear();
}
@@ -76,14 +67,27 @@ RoomData* RoomManager::Get(int uid) {
return &it->second;
}
int RoomManager::GetLoadedCount() {
return elementMap.size();
RoomData* RoomManager::Get(std::string name) {
for (std::map<int, RoomData>::iterator it = elementMap.begin(); it != elementMap.end(); ++it) {
if (it->second.GetName() == name) {
return &it->second;
}
}
return nullptr;
}
int RoomManager::GetTotalCount() {
int RoomManager::GetLoadedCount() {
return elementMap.size();
}
std::map<int, RoomData>* RoomManager::GetContainer() {
return &elementMap;
}
lua_State* RoomManager::SetLuaState(lua_State* L) {
return lua = L;
}
lua_State* RoomManager::GetLuaState() {
return lua;
}
+4 -5
View File
@@ -37,21 +37,20 @@
class RoomManager: public Singleton<RoomManager> {
public:
//common public methods
int Create(std::string);
void Unload(int uid);
int Create(std::string name, std::string tileset);
void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn);
//accessors and mutators
RoomData* Get(int uid);
RoomData* Get(std::string name);
int GetLoadedCount();
int GetTotalCount();
std::map<int, RoomData>* GetContainer();
//hooks
lua_State* SetLuaState(lua_State* L) { return lua = L; }
lua_State* GetLuaState() { return lua; }
lua_State* SetLuaState(lua_State* L);
lua_State* GetLuaState();
private:
friend Singleton<RoomManager>;
+42 -6
View File
@@ -24,31 +24,67 @@
#include "room_manager.hpp"
int createRoom(lua_State* L) {
//create & get the room
RoomManager& roomMgr = RoomManager::GetSingleton();
int uid = roomMgr.Create(lua_tostring(L, 1));
int uid = roomMgr.Create(lua_tostring(L, 1), lua_tostring(L, 2));
RoomData* room = roomMgr.Get(uid);
//TODO: initialization parameters here?
//return room, uid
lua_pushlightuserdata(L, static_cast<void*>(room));
lua_pushinteger(L, uid);
lua_pushinteger(L, uid); //for debugging, mostly
return 2;
}
int unloadRoom(lua_State* L) {
//TODO: check authorization for room deletion
RoomManager& roomMgr = RoomManager::GetSingleton();
roomMgr.Unload(lua_tointeger(L, 1));
switch(lua_type(L, 1)) {
case LUA_TNUMBER: {
//number
int uid = lua_tointeger(L, 1);
roomMgr.UnloadIf([uid](std::pair<int, RoomData> it){
return it.first == uid;
});
}
break;
case LUA_TSTRING: {
//name
std::string name = lua_tostring(L, 1);
roomMgr.UnloadIf([name](std::pair<int, RoomData> it){
return it.second.GetName() == name;
});
}
break;
case LUA_TLIGHTUSERDATA: {
//the room itself
std::string name = static_cast<RoomData*>(lua_touserdata(L, 1))->GetName();
roomMgr.UnloadIf([name](std::pair<int, RoomData> it){
return it.second.GetName() == name;
});
}
break;
}
return 0;
}
int getRoom(lua_State* L) {
//TODO: integer vs name for getRoom()
RoomManager& roomMgr = RoomManager::GetSingleton();
RoomData* room = nullptr;
RoomData* room = roomMgr.Get(lua_tointeger(L, 1));
switch(lua_type(L, 1)) {
case LUA_TNUMBER:
//number
room = roomMgr.Get(lua_tointeger(L, 1));
break;
case LUA_TSTRING:
//name
room = roomMgr.Get(lua_tostring(L, 1));
break;
}
if (room) {
lua_pushlightuserdata(L, static_cast<void*>(room));