Triggers now support exclusion lists
This commit is contained in:
@@ -29,7 +29,7 @@
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
static int setRoomIndex(lua_State* L) { //TODO: (1) take the room userdata as a parameter?
|
||||
static int setRoomIndex(lua_State* L) { //TODO: (0) take the room userdata as a parameter
|
||||
//NOTE: type-dependant calls to various API functions, see bug #43
|
||||
|
||||
//reverse engineer the character index
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
#include "room_data.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <stdexcept>
|
||||
@@ -61,19 +62,37 @@ void RoomData::RunFrame() {
|
||||
Entity* entity = entityStack.top();
|
||||
BoundingBox entityBox = entity->GetBounds() + entity->GetOrigin();
|
||||
|
||||
//get the trigger & hitbox
|
||||
for (auto& it : *triggerMgr.GetContainer()) {
|
||||
BoundingBox triggerBox = it.second.GetBoundingBox() + it.second.GetOrigin();
|
||||
//get the trigger pair & hitbox
|
||||
for (auto& triggerPair : *triggerMgr.GetContainer()) {
|
||||
BoundingBox triggerBox = triggerPair.second.GetBoundingBox() + triggerPair.second.GetOrigin();
|
||||
|
||||
//find all collisions
|
||||
if (entityBox.CheckOverlap(triggerBox)) {
|
||||
//skip members of the exclusion list
|
||||
if (std::any_of(triggerPair.second.GetExclusionList()->begin(), triggerPair.second.GetExclusionList()->end(), [entity](Entity* ptr) -> bool {
|
||||
return entity == ptr;
|
||||
})) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//run the trigger script
|
||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, it.second.GetScriptReference());
|
||||
lua_rawgeti(lua, LUA_REGISTRYINDEX, triggerPair.second.GetScriptReference());
|
||||
lua_pushlightuserdata(lua, entity);
|
||||
|
||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
||||
//error
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
||||
}
|
||||
|
||||
//push to the exclusion list
|
||||
triggerPair.second.GetExclusionList()->push_back(entity);
|
||||
}
|
||||
else {
|
||||
//remove members of the exclusion list
|
||||
//NOTE: characters in different rooms won't be removed, but that shouldn't be a problem
|
||||
triggerPair.second.GetExclusionList()->remove_if([entity](Entity* ptr) -> bool {
|
||||
return entity == ptr;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +86,21 @@ static int getReference(lua_State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int pushExclusionEntity(lua_State* L) {
|
||||
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
||||
trigger->GetExclusionList()->push_back(static_cast<Entity*>(lua_touserdata(L, 2)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int removeExclusionEntity(lua_State* L) {
|
||||
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
||||
Entity* entity = static_cast<Entity*>(lua_touserdata(L, 2));
|
||||
trigger->GetExclusionList()->remove_if([entity](Entity* ptr){
|
||||
return entity == ptr;
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const luaL_Reg triggerLib[] = {
|
||||
{"SetHandle", setHandle},
|
||||
{"GetHandle", getHandle},
|
||||
@@ -99,6 +114,9 @@ static const luaL_Reg triggerLib[] = {
|
||||
{"SetScript",setReference},
|
||||
{"GetScript",getReference},
|
||||
|
||||
{"PushExclusionEntity", pushExclusionEntity},
|
||||
{"RemoveExclusionEntity", removeExclusionEntity},
|
||||
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
|
||||
@@ -51,4 +51,8 @@ int TriggerData::SetScriptReference(int i) {
|
||||
|
||||
int TriggerData::GetScriptReference() {
|
||||
return scriptRef;
|
||||
}
|
||||
|
||||
std::list<Entity*>* TriggerData::GetExclusionList() {
|
||||
return &exclusionList;
|
||||
}
|
||||
@@ -23,13 +23,14 @@
|
||||
#define TRIGGERDATA_HPP_
|
||||
|
||||
#include "bounding_box.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "vector2.hpp"
|
||||
|
||||
#include "lua.hpp"
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
//TODO: (0) state-system for preventing double triggering
|
||||
class TriggerData {
|
||||
public:
|
||||
TriggerData() = default;
|
||||
@@ -47,11 +48,14 @@ public:
|
||||
int SetScriptReference(int i);
|
||||
int GetScriptReference();
|
||||
|
||||
std::list<Entity*>* GetExclusionList();
|
||||
|
||||
private:
|
||||
std::string handle;
|
||||
Vector2 origin;
|
||||
BoundingBox bounds;
|
||||
int scriptRef = LUA_NOREF;
|
||||
std::list<Entity*> exclusionList;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user