The rooms are ticking

This commit is contained in:
Kayne Ruse
2015-02-18 00:07:38 +11:00
parent a106134dd1
commit e011e6bdc5
7 changed files with 87 additions and 15 deletions
+11
View File
@@ -65,6 +65,15 @@ static int getWaypointMgr(lua_State* L) {
return 1;
}
//TODO: character list
static int setOnTick(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
luaL_unref(L, LUA_REGISTRYINDEX, room->GetTickReference());
room->SetTickReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0;
}
static int initialize(lua_State* L) {
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
@@ -88,6 +97,8 @@ static const luaL_Reg roomLib[] = {
{"GetMonsterMgr",getMonsterMgr},
{"GetWaypointMgr",getWaypointMgr},
{"SetOnTick", setOnTick},
{"Initialize", initialize},
{nullptr, nullptr}
};
+38 -2
View File
@@ -21,6 +21,22 @@
*/
#include "room_data.hpp"
void RoomData::RunFrame() {
//get the hook
lua_rawgeti(lua, LUA_REGISTRYINDEX, tickRef);
if (lua_isnil(lua, -1)) {
lua_pop(lua, 1);
return;
}
//call the tick function, with this as a parameter
lua_pushlightuserdata(lua, this);
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
}
}
std::string RoomData::SetName(std::string s) {
return roomName = s;
}
@@ -53,6 +69,26 @@ std::list<CharacterData*>* RoomData::GetCharacterList() {
return &characterList;
}
void RoomData::RunFrame() {
//TODO: (1) EMPTY
lua_State* RoomData::SetLuaState(lua_State* L) {
return lua = L;
}
lua_State* RoomData::GetLuaState() {
return lua;
}
sqlite3* RoomData::SetDatabase(sqlite3* db) {
return database = db;
}
sqlite3* RoomData::GetDatabase() {
return database;
}
int RoomData::SetTickReference(int i) {
return tickRef = i;
}
int RoomData::GetTickReference() {
return tickRef;
}
+20 -5
View File
@@ -37,6 +37,8 @@ public:
RoomData() = default;
~RoomData() = default;
void RunFrame();
//accessors and mutators
std::string SetName(std::string);
std::string GetName();
@@ -49,21 +51,34 @@ public:
WaypointManager* GetWaypointMgr();
std::list<CharacterData*>* GetCharacterList();
void RunFrame();
//API interfaces
lua_State* SetLuaState(lua_State* L);
lua_State* GetLuaState();
sqlite3* SetDatabase(sqlite3* db);
sqlite3* GetDatabase();
//TODO: triggers for unload, save, per-second, player enter, player exit, etc.
//hooks
int SetTickReference(int i);
int GetTickReference();
//TODO: other triggers like player entry & exit, etc.
private:
friend class RoomManager;
//members
//metadata
std::string roomName;
std::string tilesetName;
//members
RegionPagerLua pager;
MonsterManager monsterMgr;
WaypointManager waypointMgr;
std::list<CharacterData*> characterList;
//API
lua_State* lua = nullptr;
sqlite3* database = nullptr;
//hooks
int tickRef = LUA_NOREF;
};
#endif
+9 -6
View File
@@ -35,10 +35,13 @@ int RoomManager::Create(std::string roomName, std::string tileset) {
newRoom->SetName(roomName);
newRoom->SetTileset(tileset);
newRoom->pager.SetLuaState(lua);
newRoom->monsterMgr.SetLuaState(lua);
newRoom->monsterMgr.SetDatabase(database);
newRoom->waypointMgr.SetLuaState(lua);
newRoom->GetPager()->SetLuaState(lua);
newRoom->GetMonsterMgr()->SetLuaState(lua);
newRoom->GetMonsterMgr()->SetDatabase(database);
newRoom->GetWaypointMgr()->SetLuaState(lua);
newRoom->SetLuaState(lua);
newRoom->SetDatabase(database);
//get the hook
lua_rawgeti(lua, LUA_REGISTRYINDEX, createRef);
@@ -123,7 +126,7 @@ void RoomManager::PushCharacter(CharacterData* character) {
throw(std::runtime_error("Failed to push an character to a non-existant room"));
}
room->characterList.push_back(character);
room->GetCharacterList()->push_back(character);
}
void RoomManager::PopCharacter(CharacterData const* character) {
@@ -138,7 +141,7 @@ void RoomManager::PopCharacter(CharacterData const* character) {
throw(std::runtime_error("Failed to pop an character to a non-existant room"));
}
room->characterList.remove_if([character](CharacterData* ptr) {
room->GetCharacterList()->remove_if([character](CharacterData* ptr) {
return character == ptr;
});
}
+3 -1
View File
@@ -70,9 +70,11 @@ private:
//members
std::map<int, RoomData> elementMap;
int counter = 0;
//API
lua_State* lua = nullptr;
sqlite3* database = nullptr;
int counter = 0;
//hooks
int createRef = LUA_NOREF;
-1
View File
@@ -110,7 +110,6 @@ static int setOnUnload(lua_State* L) {
return 0;
}
static const luaL_Reg roomManagerLib[] = {
{"CreateRoom", createRoom},
{"UnloadRoom", unloadRoom},