Encapsulated RoomData, did some other refactoring

This commit is contained in:
Kayne Ruse
2014-08-14 00:13:19 +10:00
parent 6d98bab000
commit 98ffcb8cd3
6 changed files with 44 additions and 33 deletions
+7 -7
View File
@@ -25,17 +25,17 @@
static int getPager(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
lua_pushlightuserdata(L, reinterpret_cast<void*>(&room->pager));
lua_pushlightuserdata(L, reinterpret_cast<void*>(room->GetPager()) );
return 1;
}
static int onCreate(lua_State* L) {
//TODO: onCreate()
static int create(lua_State* L) {
//TODO: create()
return 0;
}
static int onUnload(lua_State* L) {
//TODO: onUnload()
static int unload(lua_State* L) {
//TODO: unload()
return 0;
}
@@ -43,8 +43,8 @@ static int onUnload(lua_State* L) {
static const luaL_Reg roomLib[] = {
{"GetPager",getPager},
{"OnCreate", onCreate},
{"OnUnload", onUnload},
{"Create", create},
{"Unload", unload},
{nullptr, nullptr}
};
+11 -4
View File
@@ -25,12 +25,19 @@
//map system
#include "region_pager_lua.hpp"
struct RoomData {
class RoomData {
public:
RoomData() = default;
~RoomData() = default;
//accessors and mutators
RegionPagerLua* GetPager() { return &pager; }
private:
friend class RoomManager;
//members
RegionPagerLua pager;
//TODO: collision map
//TODO: NPCs?
};
#endif
+11 -9
View File
@@ -21,13 +21,15 @@
*/
#include "room_manager.hpp"
#include "room_api.hpp"
#include <stdexcept>
//-------------------------
//public access methods
//-------------------------
RoomData* RoomManager::CreateRoom() {
int RoomManager::CreateRoom() {
//create the room
RoomData* newRoom = new RoomData();
@@ -37,11 +39,11 @@ RoomData* RoomManager::CreateRoom() {
}
//register the room
roomMap[counter++] = newRoom;
roomMap[counter] = newRoom;
//API hook
lua_getglobal(luaState, "Room");
lua_getfield(luaState, -1, "OnCreate");
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) ));
@@ -49,7 +51,7 @@ RoomData* RoomManager::CreateRoom() {
lua_pop(luaState, 1);
//finish the routine
return newRoom;
return counter++;
}
void RoomManager::UnloadRoom(int uid) {
@@ -60,8 +62,8 @@ void RoomManager::UnloadRoom(int uid) {
}
//API hook
lua_getglobal(luaState, "Room");
lua_getfield(luaState, -1, "OnUnload");
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) ));
@@ -92,11 +94,11 @@ int RoomManager::PushRoom(RoomData* room) {
}
void RoomManager::UnloadAll() {
lua_getglobal(luaState, "Room");
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
for (auto& it : roomMap) {
//API hook
lua_getfield(luaState, -1, "OnUnload");
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) ));
+1 -1
View File
@@ -36,7 +36,7 @@ public:
~RoomManager() = default;
//public access methods
RoomData* CreateRoom();
int CreateRoom();
void UnloadRoom(int uid);
RoomData* GetRoom(int uid);
@@ -19,11 +19,9 @@
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "room_mgr_api.hpp"
#include "room_manager_api.hpp"
#include "room_api.hpp"
#include "room_manager.hpp"
#include "room_data.hpp"
#include <string>
@@ -47,10 +45,12 @@ static int createRoom(lua_State* L) {
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
//create the room
RoomData* newRoom = roomMgr->CreateRoom();
int uid = roomMgr->CreateRoom();
//TODO: any room parameters
//return the new room
lua_pushlightuserdata(L, newRoom);
lua_pushlightuserdata(L, roomMgr->FindRoom(uid));
return 1;
}
@@ -60,20 +60,22 @@ static int unloadRoom(lua_State* L) {
lua_gettable(L, LUA_REGISTRYINDEX);
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
//TODO: any room parameters
//unload the specified room
roomMgr->UnloadRoom(lua_tointeger(L, -2));
return 0;
}
static const luaL_Reg roomMgrLib[] = {
static const luaL_Reg roomManagerLib[] = {
{"GetRoom",getRoom},
{"CreateRoom",createRoom},
{"UnloadRoom",unloadRoom},
{nullptr, nullptr}
};
LUAMOD_API int openRoomMgrAPI(lua_State* L) {
luaL_newlib(L, roomMgrLib);
LUAMOD_API int openRoomManagerAPI(lua_State* L) {
luaL_newlib(L, roomManagerLib);
return 1;
}
@@ -19,12 +19,12 @@
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef ROOMMGRAPI_HPP_
#define ROOMMGRAPI_HPP_
#ifndef ROOMMANAGERAPI_HPP_
#define ROOMMANAGERAPI_HPP_
#include "lua/lua.hpp"
#define TORTUGA_ROOM_MGR_NAME "RoomMgr"
LUAMOD_API int openRoomMgrAPI(lua_State* L);
#define TORTUGA_ROOM_MANAGER_NAME "RoomManager"
LUAMOD_API int openRoomManagerAPI(lua_State* L);
#endif