Added respawning rabbits

This commit is contained in:
2016-04-14 23:33:39 +10:00
parent cc6981e35f
commit fb2d49f1e0
8 changed files with 80 additions and 12 deletions
+35
View File
@@ -109,6 +109,24 @@ static int forEachCreature(lua_State* L) {
return 0;
}
static int forEachBarrier(lua_State* L) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
BarrierManager* barrierMgr = room->GetBarrierMgr();
//pass each barrier to the given function
for (auto& it : *barrierMgr->GetContainer()) {
lua_pushvalue(L, -1);
lua_pushlightuserdata(L, static_cast<void*>(&it.second));
//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) {
RoomData* room = reinterpret_cast<RoomData*>(lua_touserdata(L, 1));
luaL_unref(L, LUA_REGISTRYINDEX, room->GetTickReference());
@@ -122,6 +140,19 @@ static int getOnTick(lua_State* L) {
return 1;
}
//TODO: autogen docs
static int setTag(lua_State* L) {
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
room->SetTag(lua_tostring(L, 2), lua_tostring(L, 3));
return 0;
}
static int getTag(lua_State* L) {
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
lua_pushstring(L, room->GetTag(lua_tostring(L, 2)).c_str());
return 1;
}
static int initialize(lua_State* L) {
RoomData* room = static_cast<RoomData*>(lua_touserdata(L, 1));
@@ -148,10 +179,14 @@ static const luaL_Reg roomLib[] = {
{"ForEachCharacter", forEachCharacter},
{"ForEachCreature", forEachCreature},
{"ForEachBarrier", forEachBarrier},
{"SetOnTick", setOnTick},
{"GetOnTick", getOnTick},
{"SetTag", setTag},
{"GetTag", getTag},
{"Initialize", initialize},
{nullptr, nullptr}
};
+20 -3
View File
@@ -36,12 +36,21 @@ void RoomData::RunFrame() {
//get the hook
lua_rawgeti(lua, LUA_REGISTRYINDEX, tickRef);
//returning 1 means pump all
bool updateAll = false;
if (!lua_isnil(lua, -1)) {
//call the tick function, with this as a parameter
lua_pushlightuserdata(lua, this);
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
if (lua_pcall(lua, 1, 1, 0) != LUA_OK) {
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
}
//pump creatures & barriers
if (lua_tonumber(lua, -1)) {
updateAll = true;
}
lua_pop(lua, 1);
}
else {
lua_pop(lua, 1);
@@ -55,8 +64,8 @@ void RoomData::RunFrame() {
for (auto& it : characterList) {
it->Update();
}
creatureMgr.Update(&creatureList);
barrierMgr.Update(&barrierList);
creatureMgr.Update(&creatureList, updateAll);
barrierMgr.Update(&barrierList, updateAll);
//build a list of entities for use with the triggers
std::stack<Entity*> entityStack;
@@ -202,4 +211,12 @@ int RoomData::SetTickReference(int i) {
int RoomData::GetTickReference() {
return tickRef;
}
std::string RoomData::SetTag(std::string key, std::string value) {
return tags[key] = value;
}
std::string RoomData::GetTag(std::string key) {
return tags[key];
}
+4
View File
@@ -70,6 +70,9 @@ public:
int GetTickReference();
//TODO: other triggers like player entry & exit, etc.
std::string SetTag(std::string key, std::string value);
std::string GetTag(std::string key);
private:
//metadata
std::string roomName;
@@ -90,4 +93,5 @@ private:
//hooks
int tickRef = LUA_NOREF;
std::map<std::string, std::string> tags;
};