The rooms are ticking
This commit is contained in:
@@ -14,6 +14,12 @@ end
|
|||||||
--test the room hooks
|
--test the room hooks
|
||||||
roomSystem.RoomManager.SetOnCreate(function(room, index)
|
roomSystem.RoomManager.SetOnCreate(function(room, index)
|
||||||
print("", "Creating room: ", roomSystem.Room.GetName(room), index)
|
print("", "Creating room: ", roomSystem.Room.GetName(room), index)
|
||||||
|
|
||||||
|
--called ~60 times per second
|
||||||
|
--TODO: test this
|
||||||
|
-- roomSystem.Room.SetOnTick(room, function(room)
|
||||||
|
-- print("", "","tick")
|
||||||
|
-- end)
|
||||||
end)
|
end)
|
||||||
roomSystem.RoomManager.SetOnUnload(function(room, index)
|
roomSystem.RoomManager.SetOnUnload(function(room, index)
|
||||||
print("", "Unloading room: ", roomSystem.Room.GetName(room), index)
|
print("", "Unloading room: ", roomSystem.Room.GetName(room), index)
|
||||||
|
|||||||
@@ -65,6 +65,15 @@ static int getWaypointMgr(lua_State* L) {
|
|||||||
return 1;
|
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) {
|
static int initialize(lua_State* L) {
|
||||||
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
|
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
|
|
||||||
@@ -88,6 +97,8 @@ static const luaL_Reg roomLib[] = {
|
|||||||
{"GetMonsterMgr",getMonsterMgr},
|
{"GetMonsterMgr",getMonsterMgr},
|
||||||
{"GetWaypointMgr",getWaypointMgr},
|
{"GetWaypointMgr",getWaypointMgr},
|
||||||
|
|
||||||
|
{"SetOnTick", setOnTick},
|
||||||
|
|
||||||
{"Initialize", initialize},
|
{"Initialize", initialize},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,6 +21,22 @@
|
|||||||
*/
|
*/
|
||||||
#include "room_data.hpp"
|
#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) {
|
std::string RoomData::SetName(std::string s) {
|
||||||
return roomName = s;
|
return roomName = s;
|
||||||
}
|
}
|
||||||
@@ -53,6 +69,26 @@ std::list<CharacterData*>* RoomData::GetCharacterList() {
|
|||||||
return &characterList;
|
return &characterList;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RoomData::RunFrame() {
|
lua_State* RoomData::SetLuaState(lua_State* L) {
|
||||||
//TODO: (1) EMPTY
|
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;
|
||||||
}
|
}
|
||||||
@@ -37,6 +37,8 @@ public:
|
|||||||
RoomData() = default;
|
RoomData() = default;
|
||||||
~RoomData() = default;
|
~RoomData() = default;
|
||||||
|
|
||||||
|
void RunFrame();
|
||||||
|
|
||||||
//accessors and mutators
|
//accessors and mutators
|
||||||
std::string SetName(std::string);
|
std::string SetName(std::string);
|
||||||
std::string GetName();
|
std::string GetName();
|
||||||
@@ -49,21 +51,34 @@ public:
|
|||||||
WaypointManager* GetWaypointMgr();
|
WaypointManager* GetWaypointMgr();
|
||||||
std::list<CharacterData*>* GetCharacterList();
|
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:
|
private:
|
||||||
friend class RoomManager;
|
//metadata
|
||||||
|
|
||||||
//members
|
|
||||||
std::string roomName;
|
std::string roomName;
|
||||||
std::string tilesetName;
|
std::string tilesetName;
|
||||||
|
|
||||||
|
//members
|
||||||
RegionPagerLua pager;
|
RegionPagerLua pager;
|
||||||
MonsterManager monsterMgr;
|
MonsterManager monsterMgr;
|
||||||
WaypointManager waypointMgr;
|
WaypointManager waypointMgr;
|
||||||
std::list<CharacterData*> characterList;
|
std::list<CharacterData*> characterList;
|
||||||
|
|
||||||
|
//API
|
||||||
|
lua_State* lua = nullptr;
|
||||||
|
sqlite3* database = nullptr;
|
||||||
|
|
||||||
|
//hooks
|
||||||
|
int tickRef = LUA_NOREF;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -35,10 +35,13 @@ int RoomManager::Create(std::string roomName, std::string tileset) {
|
|||||||
newRoom->SetName(roomName);
|
newRoom->SetName(roomName);
|
||||||
newRoom->SetTileset(tileset);
|
newRoom->SetTileset(tileset);
|
||||||
|
|
||||||
newRoom->pager.SetLuaState(lua);
|
newRoom->GetPager()->SetLuaState(lua);
|
||||||
newRoom->monsterMgr.SetLuaState(lua);
|
newRoom->GetMonsterMgr()->SetLuaState(lua);
|
||||||
newRoom->monsterMgr.SetDatabase(database);
|
newRoom->GetMonsterMgr()->SetDatabase(database);
|
||||||
newRoom->waypointMgr.SetLuaState(lua);
|
newRoom->GetWaypointMgr()->SetLuaState(lua);
|
||||||
|
|
||||||
|
newRoom->SetLuaState(lua);
|
||||||
|
newRoom->SetDatabase(database);
|
||||||
|
|
||||||
//get the hook
|
//get the hook
|
||||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, createRef);
|
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"));
|
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) {
|
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"));
|
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;
|
return character == ptr;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,9 +70,11 @@ private:
|
|||||||
|
|
||||||
//members
|
//members
|
||||||
std::map<int, RoomData> elementMap;
|
std::map<int, RoomData> elementMap;
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
//API
|
||||||
lua_State* lua = nullptr;
|
lua_State* lua = nullptr;
|
||||||
sqlite3* database = nullptr;
|
sqlite3* database = nullptr;
|
||||||
int counter = 0;
|
|
||||||
|
|
||||||
//hooks
|
//hooks
|
||||||
int createRef = LUA_NOREF;
|
int createRef = LUA_NOREF;
|
||||||
|
|||||||
@@ -110,7 +110,6 @@ static int setOnUnload(lua_State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static const luaL_Reg roomManagerLib[] = {
|
static const luaL_Reg roomManagerLib[] = {
|
||||||
{"CreateRoom", createRoom},
|
{"CreateRoom", createRoom},
|
||||||
{"UnloadRoom", unloadRoom},
|
{"UnloadRoom", unloadRoom},
|
||||||
|
|||||||
Reference in New Issue
Block a user