Using an entity stack for trigger comparisons

This commit is contained in:
Kayne Ruse
2015-03-11 18:18:32 +11:00
parent 4d71d4cc40
commit 670ab22e96
3 changed files with 26 additions and 43 deletions
+1 -15
View File
@@ -32,7 +32,7 @@ roomAPI.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland
local underworld, uidTwo = roomManagerAPI.CreateRoom("underworld", "overworld.bmp") local underworld, uidTwo = roomManagerAPI.CreateRoom("underworld", "overworld.bmp")
roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrassland, mapSaver.Save) roomAPI.Initialize(underworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugGrassland, mapSaver.Save)
--debug: test the trigger system --NOTE: test the trigger system
regionPagerAPI = require("region_pager") regionPagerAPI = require("region_pager")
triggerManagerAPI = require("trigger_manager") triggerManagerAPI = require("trigger_manager")
@@ -58,17 +58,10 @@ createTrigger("door 1", overworld, 128, -128, function(entity)
return return
end end
print("mark 1")
local x, y = characterAPI.GetOrigin(entity) local x, y = characterAPI.GetOrigin(entity)
print("mark 2")
characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (1) take exit coordinates as a parameter characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (1) take exit coordinates as a parameter
print("mark 3")
characterAPI.SetOrigin(entity, 0, 0) characterAPI.SetOrigin(entity, 0, 0)
print("mark 4")
networkAPI.PumpCharacterUpdate(entity) networkAPI.PumpCharacterUpdate(entity)
print("mark 5")
return false
end) end)
createTrigger("door 1", underworld, 128, -128, function(entity) createTrigger("door 1", underworld, 128, -128, function(entity)
@@ -76,17 +69,10 @@ createTrigger("door 1", underworld, 128, -128, function(entity)
return return
end end
print("mark 6")
local x, y = characterAPI.GetOrigin(entity) local x, y = characterAPI.GetOrigin(entity)
print("mark 7")
characterAPI.SetRoomIndex(entity, uidOne) characterAPI.SetRoomIndex(entity, uidOne)
print("mark 8")
characterAPI.SetOrigin(entity, 0, 0) characterAPI.SetOrigin(entity, 0, 0)
print("mark 9")
networkAPI.PumpCharacterUpdate(entity) networkAPI.PumpCharacterUpdate(entity)
print("mark 10")
return false
end) end)
print("Finished the lua script") print("Finished the lua script")
+25 -26
View File
@@ -22,6 +22,7 @@
#include "room_data.hpp" #include "room_data.hpp"
#include <iostream> #include <iostream>
#include <stack>
#include <stdexcept> #include <stdexcept>
void RoomData::RunFrame() { void RoomData::RunFrame() {
@@ -44,42 +45,40 @@ void RoomData::RunFrame() {
it->Update(); it->Update();
} }
//TODO: (3) iterate through the monster map //TODO: (3) iterate through the monster map
//TODO: (3) trigger script for monsters
//build a list of game entities
std::stack<Entity*> entityStack;
for (auto& it : characterList) {
entityStack.push(it);
}
//TODO: (3) push the monster entities
//compare the triggers to the entities, using their real hitboxes //compare the triggers to the entities, using their real hitboxes
//NOTE: this stack solution should prevent problems when modifying the various lists
while(entityStack.size()) {
//get the entity & hitbox
Entity* entity = entityStack.top();
BoundingBox entityBox = entity->GetBounds() + entity->GetOrigin();
//get the trigger & hitbox
for (auto& it : *triggerMgr.GetContainer()) { for (auto& it : *triggerMgr.GetContainer()) {
BoundingBox itBox = it.second.GetBoundingBox() + it.second.GetOrigin(); BoundingBox triggerBox = it.second.GetBoundingBox() + it.second.GetOrigin();
for (auto& character : characterList) {
BoundingBox hitBox = character->GetBounds() + character->GetOrigin();
if ( itBox.CheckOverlap(hitBox) ) { if (entityBox.CheckOverlap(triggerBox)) {
//trigger script //run the trigger script
lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference()); lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference());
lua_pushlightuserdata(lua, character); lua_pushlightuserdata(lua, entity);
//BUG: (0) if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
std::cout << "running scripts" << std::endl;
//run the script
//BUGFIX: changing the character's room via lua invalidates the list, therefore, the script much signal for an early exit
//TODO: (2) fix this somehow (operate on a stack?)
if (lua_pcall(lua, 1, 1, 0) != LUA_OK) {
//error //error
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) )); throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
} }
}
}
//true = safe to continue, false = exit early //next
if (!lua_toboolean(lua, -1)) { entityStack.pop();
std::cout << "Warning!: Early abort" << std::endl;
return;
}
}
}
// for (auto& monster : *monsterMgr.GetContainer()) {
// if (it.second.Compare(static_cast<Entity*>(&monster.second))) {
// //TODO: (1) trigger script
// }
// }
} }
} }
@@ -156,8 +156,6 @@ void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const c
} }
void pumpAndChangeRooms(int characterIndex, int newRoomIndex) { void pumpAndChangeRooms(int characterIndex, int newRoomIndex) {
//BUG: three redundant lookups
//get the character object //get the character object
CharacterData* character = CharacterManager::GetSingleton().Get(characterIndex); CharacterData* character = CharacterManager::GetSingleton().Get(characterIndex);