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")
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")
triggerManagerAPI = require("trigger_manager")
@@ -58,17 +58,10 @@ createTrigger("door 1", overworld, 128, -128, function(entity)
return
end
print("mark 1")
local x, y = characterAPI.GetOrigin(entity)
print("mark 2")
characterAPI.SetRoomIndex(entity, uidTwo) --TODO: (1) take exit coordinates as a parameter
print("mark 3")
characterAPI.SetOrigin(entity, 0, 0)
print("mark 4")
networkAPI.PumpCharacterUpdate(entity)
print("mark 5")
return false
end)
createTrigger("door 1", underworld, 128, -128, function(entity)
@@ -76,17 +69,10 @@ createTrigger("door 1", underworld, 128, -128, function(entity)
return
end
print("mark 6")
local x, y = characterAPI.GetOrigin(entity)
print("mark 7")
characterAPI.SetRoomIndex(entity, uidOne)
print("mark 8")
characterAPI.SetOrigin(entity, 0, 0)
print("mark 9")
networkAPI.PumpCharacterUpdate(entity)
print("mark 10")
return false
end)
print("Finished the lua script")
+25 -26
View File
@@ -22,6 +22,7 @@
#include "room_data.hpp"
#include <iostream>
#include <stack>
#include <stdexcept>
void RoomData::RunFrame() {
@@ -44,42 +45,40 @@ void RoomData::RunFrame() {
it->Update();
}
//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
for (auto& it : *triggerMgr.GetContainer()) {
BoundingBox itBox = it.second.GetBoundingBox() + it.second.GetOrigin();
for (auto& character : characterList) {
BoundingBox hitBox = character->GetBounds() + character->GetOrigin();
//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();
if ( itBox.CheckOverlap(hitBox) ) {
//trigger script
//get the trigger & hitbox
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_pushlightuserdata(lua, character);
lua_pushlightuserdata(lua, entity);
//BUG: (0)
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) {
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
//error
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))) {
// //TODO: (1) trigger script
// }
// }
//next
entityStack.pop();
}
}
@@ -156,8 +156,6 @@ void copyCharacterToPacket(CharacterPacket* const packet, CharacterData* const c
}
void pumpAndChangeRooms(int characterIndex, int newRoomIndex) {
//BUG: three redundant lookups
//get the character object
CharacterData* character = CharacterManager::GetSingleton().Get(characterIndex);