Added character iteration to the rooms
This commit is contained in:
@@ -20,7 +20,7 @@ roomSystem.RoomManager.SetOnCreate(function(room, index)
|
|||||||
|
|
||||||
--called ~60 times per second
|
--called ~60 times per second
|
||||||
roomSystem.Room.SetOnTick(room, function(room)
|
roomSystem.Room.SetOnTick(room, function(room)
|
||||||
characterSystem.CharacterManager.ForEach(function(character)
|
roomSystem.Room.ForEachCharacter(room, function(character)
|
||||||
print(characterSystem.Character.GetHandle(character))
|
print(characterSystem.Character.GetHandle(character))
|
||||||
end)
|
end)
|
||||||
--[[
|
--[[
|
||||||
|
|||||||
@@ -23,6 +23,9 @@
|
|||||||
|
|
||||||
#include "room_data.hpp"
|
#include "room_data.hpp"
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
static int setRoomName(lua_State* L) {
|
static int setRoomName(lua_State* L) {
|
||||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
room->SetName(lua_tostring(L, 2));
|
room->SetName(lua_tostring(L, 2));
|
||||||
@@ -67,7 +70,22 @@ static int getTriggerMgr(lua_State* L) {
|
|||||||
|
|
||||||
//TODO: character list
|
//TODO: character list
|
||||||
|
|
||||||
//TODO: (1) forEachCharacter
|
static int forEachCharacter(lua_State* L) {
|
||||||
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
|
//pass each character to the given function
|
||||||
|
for (auto& it : *room->GetCharacterList()) {
|
||||||
|
lua_pushvalue(L, -1);
|
||||||
|
lua_pushlightuserdata(L, static_cast<void*>(it));
|
||||||
|
//call each iteration, throwing an exception if something happened
|
||||||
|
if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Lua error: ";
|
||||||
|
os << lua_tostring(L, -1);
|
||||||
|
throw(std::runtime_error(os.str()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int setOnTick(lua_State* L) {
|
static int setOnTick(lua_State* L) {
|
||||||
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
@@ -76,6 +94,12 @@ static int setOnTick(lua_State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int getOnTick(lua_State* L) {
|
||||||
|
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
|
||||||
|
lua_rawgeti(lua, LUA_REGISTRYINDEX, room->GetTickReference());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
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));
|
||||||
|
|
||||||
@@ -99,7 +123,10 @@ static const luaL_Reg roomLib[] = {
|
|||||||
{"GetMonsterMgr",getMonsterMgr},
|
{"GetMonsterMgr",getMonsterMgr},
|
||||||
{"GetTriggerMgr",getTriggerMgr},
|
{"GetTriggerMgr",getTriggerMgr},
|
||||||
|
|
||||||
|
{"ForEachCharacter", forEachCharacter},
|
||||||
|
|
||||||
{"SetOnTick", setOnTick},
|
{"SetOnTick", setOnTick},
|
||||||
|
{"GetOnTick", getOnTick},
|
||||||
|
|
||||||
{"Initialize", initialize},
|
{"Initialize", initialize},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
|
|||||||
Reference in New Issue
Block a user