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
for (auto& it : *triggerMgr.GetContainer()) { //NOTE: this stack solution should prevent problems when modifying the various lists
BoundingBox itBox = it.second.GetBoundingBox() + it.second.GetOrigin(); while(entityStack.size()) {
for (auto& character : characterList) { //get the entity & hitbox
BoundingBox hitBox = character->GetBounds() + character->GetOrigin(); Entity* entity = entityStack.top();
BoundingBox entityBox = entity->GetBounds() + entity->GetOrigin();
if ( itBox.CheckOverlap(hitBox) ) { //get the trigger & hitbox
//trigger script for (auto& it : *triggerMgr.GetContainer()) {
BoundingBox triggerBox = it.second.GetBoundingBox() + it.second.GetOrigin();
if (entityBox.CheckOverlap(triggerBox)) {
//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
if (!lua_toboolean(lua, -1)) {
std::cout << "Warning!: Early abort" << std::endl;
return;
}
} }
} }
// for (auto& monster : *monsterMgr.GetContainer()) {
// if (it.second.Compare(static_cast<Entity*>(&monster.second))) { //next
// //TODO: (1) trigger script entityStack.pop();
// }
// }
} }
} }
@@ -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);