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
+15 -3
View File
@@ -130,14 +130,14 @@ roomManagerAPI.SetOnCreate(function(room, index)
--
end)
creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare)
--creatureManager with SetOnCreate, SetOnUnload & create & unload
barrierManagerAPI.SetOnCreate(roomAPI.GetBarrierMgr(room), function(barrier)
barrierAPI.SetScript(barrier, barrierTick)
end)
roomAPI.SetOnTick(room, function(room)
ret = 0
--placeholders
roomAPI.ForEachCharacter(room, function(character)
--
end)
@@ -146,7 +146,19 @@ roomManagerAPI.SetOnCreate(function(room, index)
--
end)
--TODO: for each barrier
roomAPI.ForEachBarrier(room, function(creature)
--
end)
--respawn a new rabbit when needed
if creatureManagerAPI.GetLoadedCount(roomAPI.GetCreatureMgr(room)) < 1 and
barrierManagerAPI.GetLoadedCount(roomAPI.GetCreatureMgr(room)) < 2
then
--make a new creature
creatureManagerAPI.Create(roomAPI.GetCreatureMgr(room), "anibunny.png", bunnySquare)
ret = 1
end
return ret
end)
end)
+2 -2
View File
@@ -32,11 +32,11 @@ BarrierManager::~BarrierManager() {
}
//arg: a list of barriers to be updated in the clients
void BarrierManager::Update(std::list<std::pair<const int, BarrierData*>>* barrierList) {
void BarrierManager::Update(std::list<std::pair<const int, BarrierData*>>* barrierList, bool updateAll) {
int ret;
for (auto& it : elementMap) {
ret = it.second.Update(lua);
if (ret) {
if (ret || updateAll) {
barrierList->push_back(std::pair<const int, BarrierData*>(it.first, &it.second));
}
}
+1 -1
View File
@@ -36,7 +36,7 @@ public:
~BarrierManager();
//common public methods
void Update(std::list<std::pair<const int, BarrierData*>>* barrierList);
void Update(std::list<std::pair<const int, BarrierData*>>* barrierList, bool updateAll);
int Create(int instanceIndex);
void Unload(int uid);
+2 -2
View File
@@ -32,11 +32,11 @@ CreatureManager::~CreatureManager() {
}
//arg: a list of creatures to be updated in the clients
void CreatureManager::Update(std::list<std::pair<const int, CreatureData*>>* creatureList) {
void CreatureManager::Update(std::list<std::pair<const int, CreatureData*>>* creatureList, bool updateAll) {
int ret;
for (auto& it : elementMap) {
ret = it.second.Update(lua);
if (ret) {
if (ret || updateAll) {
creatureList->push_back(std::pair<const int, CreatureData*>(it.first, &it.second));
}
}
+1 -1
View File
@@ -37,7 +37,7 @@ public:
~CreatureManager();
//common public methods
void Update(std::list<std::pair<const int, CreatureData*>>* creatureList);
void Update(std::list<std::pair<const int, CreatureData*>>* creatureList, bool updateAll);
int Create(std::string avatar, int scriptRef);
void Unload(int uid);
+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;
@@ -203,3 +212,11 @@ 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;
};