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:
Kayne Ruse
2015-02-27 19:35:11 +11:00
parent cb63c9b07c
commit edcb6f05ce
8 changed files with 147 additions and 21 deletions
+89 -2
View File
@@ -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;
}
//return the number removed
lua_pushinteger(L, count);
return 1;
}
static int getTrigger(lua_State* L) { //TODO: (1) named triggers
static int getTrigger(lua_State* L) {
TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
lua_pushlightuserdata(L, mgr->Get(lua_tointeger(L, 2)));
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());