Encapsulated RoomData, did some other refactoring
This commit is contained in:
@@ -25,17 +25,17 @@
|
|||||||
|
|
||||||
static int getPager(lua_State* L) {
|
static int getPager(lua_State* L) {
|
||||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int onCreate(lua_State* L) {
|
static int create(lua_State* L) {
|
||||||
//TODO: onCreate()
|
//TODO: create()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int onUnload(lua_State* L) {
|
static int unload(lua_State* L) {
|
||||||
//TODO: onUnload()
|
//TODO: unload()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,8 +43,8 @@ static int onUnload(lua_State* L) {
|
|||||||
|
|
||||||
static const luaL_Reg roomLib[] = {
|
static const luaL_Reg roomLib[] = {
|
||||||
{"GetPager",getPager},
|
{"GetPager",getPager},
|
||||||
{"OnCreate", onCreate},
|
{"Create", create},
|
||||||
{"OnUnload", onUnload},
|
{"Unload", unload},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -25,12 +25,19 @@
|
|||||||
//map system
|
//map system
|
||||||
#include "region_pager_lua.hpp"
|
#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
|
//members
|
||||||
RegionPagerLua pager;
|
RegionPagerLua pager;
|
||||||
|
|
||||||
//TODO: collision map
|
|
||||||
//TODO: NPCs?
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -21,13 +21,15 @@
|
|||||||
*/
|
*/
|
||||||
#include "room_manager.hpp"
|
#include "room_manager.hpp"
|
||||||
|
|
||||||
|
#include "room_api.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//public access methods
|
//public access methods
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
RoomData* RoomManager::CreateRoom() {
|
int RoomManager::CreateRoom() {
|
||||||
//create the room
|
//create the room
|
||||||
RoomData* newRoom = new RoomData();
|
RoomData* newRoom = new RoomData();
|
||||||
|
|
||||||
@@ -37,11 +39,11 @@ RoomData* RoomManager::CreateRoom() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//register the room
|
//register the room
|
||||||
roomMap[counter++] = newRoom;
|
roomMap[counter] = newRoom;
|
||||||
|
|
||||||
//API hook
|
//API hook
|
||||||
lua_getglobal(luaState, "Room");
|
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
||||||
lua_getfield(luaState, -1, "OnCreate");
|
lua_getfield(luaState, -1, "Create");
|
||||||
lua_pushlightuserdata(luaState, newRoom);
|
lua_pushlightuserdata(luaState, newRoom);
|
||||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
||||||
@@ -49,7 +51,7 @@ RoomData* RoomManager::CreateRoom() {
|
|||||||
lua_pop(luaState, 1);
|
lua_pop(luaState, 1);
|
||||||
|
|
||||||
//finish the routine
|
//finish the routine
|
||||||
return newRoom;
|
return counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RoomManager::UnloadRoom(int uid) {
|
void RoomManager::UnloadRoom(int uid) {
|
||||||
@@ -60,8 +62,8 @@ void RoomManager::UnloadRoom(int uid) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//API hook
|
//API hook
|
||||||
lua_getglobal(luaState, "Room");
|
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
||||||
lua_getfield(luaState, -1, "OnUnload");
|
lua_getfield(luaState, -1, "Unload");
|
||||||
lua_pushlightuserdata(luaState, room);
|
lua_pushlightuserdata(luaState, room);
|
||||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
||||||
@@ -92,11 +94,11 @@ int RoomManager::PushRoom(RoomData* room) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RoomManager::UnloadAll() {
|
void RoomManager::UnloadAll() {
|
||||||
lua_getglobal(luaState, "Room");
|
lua_getglobal(luaState, TORTUGA_ROOM_NAME);
|
||||||
|
|
||||||
for (auto& it : roomMap) {
|
for (auto& it : roomMap) {
|
||||||
//API hook
|
//API hook
|
||||||
lua_getfield(luaState, -1, "OnUnload");
|
lua_getfield(luaState, -1, "Unload");
|
||||||
lua_pushlightuserdata(luaState, it.second);
|
lua_pushlightuserdata(luaState, it.second);
|
||||||
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
if (lua_pcall(luaState, 1, 0, 0) != LUA_OK) {
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(luaState, -1) ));
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public:
|
|||||||
~RoomManager() = default;
|
~RoomManager() = default;
|
||||||
|
|
||||||
//public access methods
|
//public access methods
|
||||||
RoomData* CreateRoom();
|
int CreateRoom();
|
||||||
void UnloadRoom(int uid);
|
void UnloadRoom(int uid);
|
||||||
|
|
||||||
RoomData* GetRoom(int uid);
|
RoomData* GetRoom(int uid);
|
||||||
|
|||||||
@@ -19,11 +19,9 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#include "room_mgr_api.hpp"
|
#include "room_manager_api.hpp"
|
||||||
|
|
||||||
#include "room_api.hpp"
|
|
||||||
#include "room_manager.hpp"
|
#include "room_manager.hpp"
|
||||||
#include "room_data.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@@ -47,10 +45,12 @@ static int createRoom(lua_State* L) {
|
|||||||
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
|
RoomManager* roomMgr = reinterpret_cast<RoomManager*>(lua_touserdata(L, -1));
|
||||||
|
|
||||||
//create the room
|
//create the room
|
||||||
RoomData* newRoom = roomMgr->CreateRoom();
|
int uid = roomMgr->CreateRoom();
|
||||||
|
|
||||||
|
//TODO: any room parameters
|
||||||
|
|
||||||
//return the new room
|
//return the new room
|
||||||
lua_pushlightuserdata(L, newRoom);
|
lua_pushlightuserdata(L, roomMgr->FindRoom(uid));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,20 +60,22 @@ static int unloadRoom(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: any room parameters
|
||||||
|
|
||||||
//unload the specified room
|
//unload the specified room
|
||||||
roomMgr->UnloadRoom(lua_tointeger(L, -2));
|
roomMgr->UnloadRoom(lua_tointeger(L, -2));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const luaL_Reg roomMgrLib[] = {
|
static const luaL_Reg roomManagerLib[] = {
|
||||||
{"GetRoom",getRoom},
|
{"GetRoom",getRoom},
|
||||||
{"CreateRoom",createRoom},
|
{"CreateRoom",createRoom},
|
||||||
{"UnloadRoom",unloadRoom},
|
{"UnloadRoom",unloadRoom},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
LUAMOD_API int openRoomMgrAPI(lua_State* L) {
|
LUAMOD_API int openRoomManagerAPI(lua_State* L) {
|
||||||
luaL_newlib(L, roomMgrLib);
|
luaL_newlib(L, roomManagerLib);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -19,12 +19,12 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#ifndef ROOMMGRAPI_HPP_
|
#ifndef ROOMMANAGERAPI_HPP_
|
||||||
#define ROOMMGRAPI_HPP_
|
#define ROOMMANAGERAPI_HPP_
|
||||||
|
|
||||||
#include "lua/lua.hpp"
|
#include "lua/lua.hpp"
|
||||||
|
|
||||||
#define TORTUGA_ROOM_MGR_NAME "RoomMgr"
|
#define TORTUGA_ROOM_MANAGER_NAME "RoomManager"
|
||||||
LUAMOD_API int openRoomMgrAPI(lua_State* L);
|
LUAMOD_API int openRoomManagerAPI(lua_State* L);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user