Implemented trigger creation & deletion via lua
Triggers now have handles for simple understanding, but there's nothing preventing multiple triggers from sharing the same name.
This commit is contained in:
@@ -94,7 +94,6 @@ void World::SendAdminDisconnectForced() {
|
||||
//TODO: (9) World::SendAdminDisconnectForced()
|
||||
}
|
||||
|
||||
|
||||
void World::SendAdminShutdownRequest() {
|
||||
ClientPacket newPacket;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ mapSaver = require("map_saver")
|
||||
roomSystem = require("room_system")
|
||||
characterSystem = require("character_system")
|
||||
networkSystem = require("network")
|
||||
triggerSystem = require("trigger_system")
|
||||
|
||||
local function dumpTable(t)
|
||||
print(t)
|
||||
@@ -43,4 +44,17 @@ end)
|
||||
local overworld, uid = roomSystem.RoomManager.CreateRoom("overworld", "overworld.bmp")
|
||||
roomSystem.Room.Initialize(overworld, mapSaver.Load, mapSaver.Save, mapMaker.DebugIsland, mapSaver.Save)
|
||||
|
||||
--debugging
|
||||
triggerMgr = roomSystem.Room.GetTriggerMgr(overworld)
|
||||
trigger1, uid1 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1")
|
||||
trigger2, uid2 = triggerSystem.TriggerManager.Create(triggerMgr, "handle1")
|
||||
|
||||
print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr))
|
||||
|
||||
local deleted = triggerSystem.TriggerManager.Unload(triggerMgr, triggerSystem.Trigger.GetHandle(trigger1))
|
||||
|
||||
print("triggers:", triggerSystem.TriggerManager.GetCount(triggerMgr))
|
||||
|
||||
print("deleted: ", deleted)
|
||||
|
||||
print("Finished the lua script")
|
||||
|
||||
@@ -23,6 +23,19 @@
|
||||
|
||||
#include "trigger_data.hpp"
|
||||
|
||||
//hamdle
|
||||
static int setHandle(lua_State* L) {
|
||||
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
||||
trigger->SetHandle(lua_tostring(L, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getHandle(lua_State* L) {
|
||||
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
||||
lua_pushstring(L, trigger->GetHandle().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
//origin
|
||||
static int setOrigin(lua_State* L) {
|
||||
TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
|
||||
@@ -74,6 +87,9 @@ static int getReference(lua_State* L) {
|
||||
}
|
||||
|
||||
static const luaL_Reg triggerLib[] = {
|
||||
{"SetHandle", setHandle},
|
||||
{"GetHandle", getHandle},
|
||||
|
||||
{"SetOrigin",setOrigin},
|
||||
{"GetOrigin",getOrigin},
|
||||
|
||||
@@ -82,6 +98,7 @@ static const luaL_Reg triggerLib[] = {
|
||||
|
||||
{"SetScript",setReference},
|
||||
{"GetScript",getReference},
|
||||
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
|
||||
@@ -21,6 +21,14 @@
|
||||
*/
|
||||
#include "trigger_data.hpp"
|
||||
|
||||
std::string TriggerData::SetHandle(std::string s) {
|
||||
return handle = s;
|
||||
}
|
||||
|
||||
std::string TriggerData::GetHandle() const {
|
||||
return handle;
|
||||
}
|
||||
|
||||
int TriggerData::SetScriptReference(int i) {
|
||||
return scriptRef = i;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,9 @@ public:
|
||||
TriggerData() = default;
|
||||
~TriggerData() = default;
|
||||
|
||||
std::string SetHandle(std::string);
|
||||
std::string GetHandle() const;
|
||||
|
||||
Vector2 SetOrigin(Vector2 v);
|
||||
Vector2 GetOrigin();
|
||||
|
||||
@@ -44,8 +47,7 @@ public:
|
||||
int GetScriptReference();
|
||||
|
||||
private:
|
||||
friend class TriggerManager;
|
||||
|
||||
std::string handle;
|
||||
Vector2 origin;
|
||||
BoundingBox bounds;
|
||||
int scriptRef = LUA_NOREF;
|
||||
|
||||
@@ -29,23 +29,13 @@ TriggerManager::~TriggerManager() {
|
||||
UnloadAll();
|
||||
}
|
||||
|
||||
int TriggerManager::Create() {
|
||||
int TriggerManager::Create(std::string handle, Vector2 origin, BoundingBox bounds) {
|
||||
//implicitly creates the element
|
||||
TriggerData& triggerData = elementMap[counter];
|
||||
|
||||
//no real values set
|
||||
triggerData.origin = {0, 0};
|
||||
triggerData.bounds = {0, 0, 0, 0};
|
||||
|
||||
return counter++;
|
||||
}
|
||||
|
||||
int TriggerManager::Create(Vector2 origin, BoundingBox bounds) {
|
||||
//implicitly creates the element
|
||||
TriggerData& triggerData = elementMap[counter];
|
||||
|
||||
triggerData.origin = origin;
|
||||
triggerData.bounds = bounds;
|
||||
triggerData.SetHandle(handle);
|
||||
triggerData.SetOrigin(origin);
|
||||
triggerData.SetBoundingBox(bounds);
|
||||
|
||||
return counter++;
|
||||
}
|
||||
@@ -80,6 +70,15 @@ TriggerData* TriggerManager::Get(int uid) {
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
TriggerData* TriggerManager::Get(std::string handle) {
|
||||
for (std::map<int, TriggerData>::iterator it = elementMap.begin(); it != elementMap.end(); ++it) {
|
||||
if (it->second.GetHandle() == handle) {
|
||||
return &it->second;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int TriggerManager::GetLoadedCount() {
|
||||
return elementMap.size();
|
||||
}
|
||||
|
||||
@@ -38,8 +38,7 @@ public:
|
||||
~TriggerManager();
|
||||
|
||||
//common public methods
|
||||
int Create();
|
||||
int Create(Vector2 origin, BoundingBox bounds);
|
||||
int Create(std::string handle, Vector2 origin, BoundingBox bounds);
|
||||
void Unload(int uid);
|
||||
|
||||
void UnloadAll();
|
||||
@@ -47,6 +46,7 @@ public:
|
||||
|
||||
//accessors & mutators
|
||||
TriggerData* Get(int uid);
|
||||
TriggerData* Get(std::string handle);
|
||||
int GetLoadedCount();
|
||||
std::map<int, TriggerData>* GetContainer();
|
||||
|
||||
|
||||
@@ -27,18 +27,105 @@
|
||||
|
||||
static int create(lua_State* L) {
|
||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||
|
||||
//pad the stack with default parameters
|
||||
if (lua_gettop(L) == 2) {
|
||||
lua_pushnumber(L, 0.0); //vector.x
|
||||
lua_pushnumber(L, 0.0); //vector.y
|
||||
}
|
||||
if (lua_gettop(L) == 4) {
|
||||
lua_pushinteger(L, 0); //bounds.x
|
||||
lua_pushinteger(L, 0); //bounds.y
|
||||
lua_pushinteger(L, 0); //bounds.w
|
||||
lua_pushinteger(L, 0); //bounds.h
|
||||
}
|
||||
|
||||
//create the trigger
|
||||
int index = mgr->Create(
|
||||
lua_tostring(L, 2), //handle
|
||||
{
|
||||
lua_tonumber(L, 3), //vector.x
|
||||
lua_tonumber(L, 4) //vector.y
|
||||
},
|
||||
{
|
||||
lua_tointeger(L, 5), //bounds.x
|
||||
lua_tointeger(L, 6), //bounds.y
|
||||
lua_tointeger(L, 7), //bounds.w
|
||||
lua_tointeger(L, 8) //bounds.h
|
||||
});
|
||||
|
||||
//push to the scipts
|
||||
lua_pushlightuserdata(L, static_cast<void*>(mgr->Get(index)) );
|
||||
lua_pushinteger(L, index);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int unload(lua_State* L) {
|
||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||
int count = 0; //the number removed
|
||||
|
||||
//based on the type
|
||||
switch(lua_type(L, 2)) {
|
||||
//unload this index
|
||||
case LUA_TNUMBER:
|
||||
mgr->UnloadIf([L, &count](std::pair<int, TriggerData const&> it) -> bool {
|
||||
if (it.first == lua_tointeger(L, 2)) {
|
||||
count++;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
//unload this name
|
||||
case LUA_TSTRING:
|
||||
mgr->UnloadIf([L, &count](std::pair<int, TriggerData const&> it) -> bool {
|
||||
if (it.second.GetHandle() == lua_tostring(L, 2)) {
|
||||
count++;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
static int getTrigger(lua_State* L) { //TODO: (1) named triggers
|
||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||
lua_pushlightuserdata(L, mgr->Get(lua_tointeger(L, 2)));
|
||||
//return the number removed
|
||||
lua_pushinteger(L, count);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getTrigger(lua_State* L) {
|
||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||
TriggerData* triggerData = nullptr;
|
||||
|
||||
switch(lua_type(L, 2)) {
|
||||
case LUA_TNUMBER:
|
||||
triggerData = mgr->Get(lua_tointeger(L, 2));
|
||||
break;
|
||||
case LUA_TSTRING:
|
||||
triggerData = mgr->Get(lua_tostring(L, 2));
|
||||
break;
|
||||
}
|
||||
|
||||
if (triggerData) {
|
||||
lua_pushlightuserdata(L, static_cast<void*>(triggerData));
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int forEach(lua_State* L) {
|
||||
//TODO: (9) forEach()
|
||||
}
|
||||
|
||||
static int getLoadedCount(lua_State* L) {
|
||||
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
|
||||
lua_pushinteger(L, mgr->GetLoadedCount());
|
||||
|
||||
Reference in New Issue
Block a user