Renamed the waypoint system to trogger system

This commit is contained in:
Kayne Ruse
2015-02-27 05:21:40 +11:00
parent 84c4dd0497
commit 3431d323e5
11 changed files with 106 additions and 106 deletions
@@ -19,28 +19,28 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "waypoint_api.hpp" #include "trigger_api.hpp"
#include "waypoint_data.hpp" #include "trigger_data.hpp"
//origin //origin
static int setOrigin(lua_State* L) { static int setOrigin(lua_State* L) {
WaypointData* waypoint = static_cast<WaypointData*>(lua_touserdata(L, 1)); TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
waypoint->SetOrigin(Vector2(lua_tonumber(L, 2), lua_tonumber(L, 3))); trigger->SetOrigin(Vector2(lua_tonumber(L, 2), lua_tonumber(L, 3)));
return 0; return 0;
} }
static int getOrigin(lua_State* L) { static int getOrigin(lua_State* L) {
WaypointData* waypoint = static_cast<WaypointData*>(lua_touserdata(L, 1)); TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
lua_pushnumber(L, waypoint->GetOrigin().x); lua_pushnumber(L, trigger->GetOrigin().x);
lua_pushnumber(L, waypoint->GetOrigin().y); lua_pushnumber(L, trigger->GetOrigin().y);
return 2; return 2;
} }
//bounds //bounds
static int setBoundingBox(lua_State* L) { static int setBoundingBox(lua_State* L) {
WaypointData* waypoint = static_cast<WaypointData*>(lua_touserdata(L, 1)); TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
waypoint->SetBoundingBox(BoundingBox( trigger->SetBoundingBox(BoundingBox(
lua_tonumber(L, 2), lua_tonumber(L, 2),
lua_tonumber(L, 3), lua_tonumber(L, 3),
lua_tonumber(L, 4), lua_tonumber(L, 4),
@@ -50,42 +50,42 @@ static int setBoundingBox(lua_State* L) {
} }
static int getBoundingBox(lua_State* L) { static int getBoundingBox(lua_State* L) {
WaypointData* waypoint = static_cast<WaypointData*>(lua_touserdata(L, 1)); TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
lua_pushnumber(L, waypoint->GetBoundingBox().x); lua_pushnumber(L, trigger->GetBoundingBox().x);
lua_pushnumber(L, waypoint->GetBoundingBox().y); lua_pushnumber(L, trigger->GetBoundingBox().y);
lua_pushnumber(L, waypoint->GetBoundingBox().w); lua_pushnumber(L, trigger->GetBoundingBox().w);
lua_pushnumber(L, waypoint->GetBoundingBox().h); lua_pushnumber(L, trigger->GetBoundingBox().h);
return 4; return 4;
} }
//triggers //triggers
static int setTriggerReference(lua_State* L) { static int setReference(lua_State* L) {
WaypointData* waypoint = static_cast<WaypointData*>(lua_touserdata(L, 1)); TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
luaL_unref(L, LUA_REGISTRYINDEX, waypoint->GetTriggerReference()); luaL_unref(L, LUA_REGISTRYINDEX, trigger->GetScriptReference());
waypoint->SetTriggerReference(luaL_ref(L, LUA_REGISTRYINDEX)); trigger->SetScriptReference(luaL_ref(L, LUA_REGISTRYINDEX));
return 0; return 0;
} }
static int getTriggerReference(lua_State* L) { static int getReference(lua_State* L) {
WaypointData* waypoint = static_cast<WaypointData*>(lua_touserdata(L, 1)); TriggerData* trigger = static_cast<TriggerData*>(lua_touserdata(L, 1));
lua_pushinteger(L, waypoint->GetTriggerReference()); lua_pushinteger(L, trigger->GetScriptReference());
lua_gettable(L, LUA_REGISTRYINDEX); lua_gettable(L, LUA_REGISTRYINDEX);
return 1; return 1;
} }
static const luaL_Reg waypointLib[] = { static const luaL_Reg triggerLib[] = {
{"SetOrigin",setOrigin}, {"SetOrigin",setOrigin},
{"GetOrigin",getOrigin}, {"GetOrigin",getOrigin},
{"SetBounds",setBoundingBox}, {"SetBounds",setBoundingBox},
{"GetBounds",getBoundingBox}, {"GetBounds",getBoundingBox},
{"SetTrigger",setTriggerReference}, {"SetScript",setReference},
{"GetTrigger",getTriggerReference}, {"GetScript",getReference},
{nullptr, nullptr} {nullptr, nullptr}
}; };
LUAMOD_API int openWaypointAPI(lua_State* L) { LUAMOD_API int openTriggerAPI(lua_State* L) {
luaL_newlib(L, waypointLib); luaL_newlib(L, triggerLib);
return 1; return 1;
} }
@@ -19,12 +19,12 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef WAYPOINTAPI_HPP_ #ifndef TRIGGERAPI_HPP_
#define WAYPOINTAPI_HPP_ #define TRIGGERAPI_HPP_
#include "lua.hpp" #include "lua.hpp"
#define TORTUGA_WAYPOINT_API "waypoint" #define TORTUGA_TRIGGER_API "trigger"
LUAMOD_API int openWaypointAPI(lua_State* L); LUAMOD_API int openTriggerAPI(lua_State* L);
#endif #endif
@@ -19,28 +19,28 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "waypoint_data.hpp" #include "trigger_data.hpp"
int WaypointData::SetTriggerReference(int i) { int TriggerData::SetScriptReference(int i) {
return triggerRef = i; return scriptRef = i;
} }
int WaypointData::GetTriggerReference() { int TriggerData::GetScriptReference() {
return triggerRef; return scriptRef;
} }
BoundingBox WaypointData::SetBoundingBox(BoundingBox b) { BoundingBox TriggerData::SetBoundingBox(BoundingBox b) {
return bounds = b; return bounds = b;
} }
BoundingBox WaypointData::GetBoundingBox() { BoundingBox TriggerData::GetBoundingBox() {
return bounds; return bounds;
} }
Vector2 WaypointData::SetOrigin(Vector2 v) { Vector2 TriggerData::SetOrigin(Vector2 v) {
return origin = v; return origin = v;
} }
Vector2 WaypointData::GetOrigin() { Vector2 TriggerData::GetOrigin() {
return origin; return origin;
} }
@@ -19,8 +19,8 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef WAYPOINTDATA_HPP_ #ifndef TRIGGERDATA_HPP_
#define WAYPOINTDATA_HPP_ #define TRIGGERDATA_HPP_
#include "bounding_box.hpp" #include "bounding_box.hpp"
#include "vector2.hpp" #include "vector2.hpp"
@@ -29,10 +29,10 @@
#include <string> #include <string>
class WaypointData { class TriggerData {
public: public:
WaypointData() = default; TriggerData() = default;
~WaypointData() = default; ~TriggerData() = default;
Vector2 SetOrigin(Vector2 v); Vector2 SetOrigin(Vector2 v);
Vector2 GetOrigin(); Vector2 GetOrigin();
@@ -40,15 +40,15 @@ public:
BoundingBox SetBoundingBox(BoundingBox b); BoundingBox SetBoundingBox(BoundingBox b);
BoundingBox GetBoundingBox(); BoundingBox GetBoundingBox();
int SetTriggerReference(int i); int SetScriptReference(int i);
int GetTriggerReference(); int GetScriptReference();
private: private:
friend class WaypointManager; friend class TriggerManager;
Vector2 origin; Vector2 origin;
BoundingBox bounds; BoundingBox bounds;
int triggerRef = LUA_NOREF; int scriptRef = LUA_NOREF;
}; };
#endif #endif
@@ -19,47 +19,47 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "waypoint_manager.hpp" #include "trigger_manager.hpp"
WaypointManager::WaypointManager() { TriggerManager::TriggerManager() {
//EMPTY //EMPTY
} }
WaypointManager::~WaypointManager() { TriggerManager::~TriggerManager() {
UnloadAll(); UnloadAll();
} }
int WaypointManager::Create() { int TriggerManager::Create() {
//implicitly creates the element //implicitly creates the element
WaypointData& waypointData = elementMap[counter]; TriggerData& triggerData = elementMap[counter];
//no real values set //no real values set
waypointData.origin = {0, 0}; triggerData.origin = {0, 0};
waypointData.bounds = {0, 0, 0, 0}; triggerData.bounds = {0, 0, 0, 0};
return counter++; return counter++;
} }
int WaypointManager::Create(Vector2 origin, BoundingBox bounds) { int TriggerManager::Create(Vector2 origin, BoundingBox bounds) {
//implicitly creates the element //implicitly creates the element
WaypointData& waypointData = elementMap[counter]; TriggerData& triggerData = elementMap[counter];
waypointData.origin = origin; triggerData.origin = origin;
waypointData.bounds = bounds; triggerData.bounds = bounds;
return counter++; return counter++;
} }
void WaypointManager::Unload(int uid) { void TriggerManager::Unload(int uid) {
elementMap.erase(uid); elementMap.erase(uid);
} }
void WaypointManager::UnloadAll() { void TriggerManager::UnloadAll() {
elementMap.clear(); elementMap.clear();
} }
void WaypointManager::UnloadIf(std::function<bool(std::pair<const int, WaypointData const&>)> fn) { void TriggerManager::UnloadIf(std::function<bool(std::pair<const int, TriggerData const&>)> fn) {
std::map<int, WaypointData>::iterator it = elementMap.begin(); std::map<int, TriggerData>::iterator it = elementMap.begin();
while (it != elementMap.end()) { while (it != elementMap.end()) {
if (fn(*it)) { if (fn(*it)) {
it = elementMap.erase(it); it = elementMap.erase(it);
@@ -70,8 +70,8 @@ void WaypointManager::UnloadIf(std::function<bool(std::pair<const int, WaypointD
} }
} }
WaypointData* WaypointManager::Get(int uid) { TriggerData* TriggerManager::Get(int uid) {
std::map<int, WaypointData>::iterator it = elementMap.find(uid); std::map<int, TriggerData>::iterator it = elementMap.find(uid);
if (it == elementMap.end()) { if (it == elementMap.end()) {
return nullptr; return nullptr;
@@ -80,19 +80,19 @@ WaypointData* WaypointManager::Get(int uid) {
return &it->second; return &it->second;
} }
int WaypointManager::GetLoadedCount() { int TriggerManager::GetLoadedCount() {
return elementMap.size(); return elementMap.size();
} }
std::map<int, WaypointData>* WaypointManager::GetContainer() { std::map<int, TriggerData>* TriggerManager::GetContainer() {
return &elementMap; return &elementMap;
} }
//hooks //hooks
lua_State* WaypointManager::SetLuaState(lua_State* L) { lua_State* TriggerManager::SetLuaState(lua_State* L) {
return lua = L; return lua = L;
} }
lua_State* WaypointManager::GetLuaState() { lua_State* TriggerManager::GetLuaState() {
return lua; return lua;
} }
@@ -19,12 +19,12 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef WAYPOINTMANAGER_HPP_ #ifndef TRIGGERMANAGER_HPP_
#define WAYPOINTMANAGER_HPP_ #define TRIGGERMANAGER_HPP_
#include "bounding_box.hpp" #include "bounding_box.hpp"
#include "vector2.hpp" #include "vector2.hpp"
#include "waypoint_data.hpp" #include "trigger_data.hpp"
#include "lua.hpp" #include "lua.hpp"
@@ -33,10 +33,10 @@
#include <string> #include <string>
//TODO: (1) rename this system to the "trigger" system //TODO: (1) rename this system to the "trigger" system
class WaypointManager { class TriggerManager {
public: public:
WaypointManager(); TriggerManager();
~WaypointManager(); ~TriggerManager();
//common public methods //common public methods
int Create(); int Create();
@@ -44,12 +44,12 @@ public:
void Unload(int uid); void Unload(int uid);
void UnloadAll(); void UnloadAll();
void UnloadIf(std::function<bool(std::pair<const int, WaypointData const&>)> fn); void UnloadIf(std::function<bool(std::pair<const int, TriggerData const&>)> fn);
//accessors & mutators //accessors & mutators
WaypointData* Get(int uid); TriggerData* Get(int uid);
int GetLoadedCount(); int GetLoadedCount();
std::map<int, WaypointData>* GetContainer(); std::map<int, TriggerData>* GetContainer();
//hooks //hooks
lua_State* SetLuaState(lua_State* L); lua_State* SetLuaState(lua_State* L);
@@ -57,7 +57,7 @@ public:
private: private:
//members //members
std::map<int, WaypointData> elementMap; std::map<int, TriggerData> elementMap;
lua_State* lua = nullptr; lua_State* lua = nullptr;
int counter = 0; int counter = 0;
}; };
@@ -19,41 +19,41 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "waypoint_manager_api.hpp" #include "trigger_manager_api.hpp"
#include "waypoint_manager.hpp" #include "trigger_manager.hpp"
//TODO: figure out a way to iterate through elements of managers from lua //TODO: figure out a way to iterate through elements of managers from lua
static int create(lua_State* L) { static int create(lua_State* L) {
WaypointManager* mgr = static_cast<WaypointManager*>(lua_touserdata(L, 1)); TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
} }
static int unload(lua_State* L) { static int unload(lua_State* L) {
WaypointManager* mgr = static_cast<WaypointManager*>(lua_touserdata(L, 1)); TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
} }
static int getWaypoint(lua_State* L) { static int getTrigger(lua_State* L) { //TODO: (1) named triggers
WaypointManager* mgr = static_cast<WaypointManager*>(lua_touserdata(L, 1)); TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
lua_pushlightuserdata(L, mgr->Get(lua_tointeger(L, 2))); lua_pushlightuserdata(L, mgr->Get(lua_tointeger(L, 2)));
return 1; return 1;
} }
static int getLoadedCount(lua_State* L) { static int getLoadedCount(lua_State* L) {
WaypointManager* mgr = static_cast<WaypointManager*>(lua_touserdata(L, 1)); TriggerManager* mgr = static_cast<TriggerManager*>(lua_touserdata(L, 1));
lua_pushinteger(L, mgr->GetLoadedCount()); lua_pushinteger(L, mgr->GetLoadedCount());
return 1; return 1;
} }
static const luaL_Reg waypointManagerLib[] = { static const luaL_Reg triggerManagerLib[] = {
{"Create",create}, {"Create",create},
{"Unload",unload}, {"Unload",unload},
{"GetWaypoint",getWaypoint}, {"GetTrigger",getTrigger},
{"GetCount",getLoadedCount}, {"GetCount",getLoadedCount},
{nullptr, nullptr} {nullptr, nullptr}
}; };
LUAMOD_API int openWaypointManagerAPI(lua_State* L) { LUAMOD_API int openTriggerManagerAPI(lua_State* L) {
luaL_newlib(L, waypointManagerLib); luaL_newlib(L, triggerManagerLib);
return 1; return 1;
} }
@@ -19,12 +19,12 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef WAYPOINTSYSTEMAPI_HPP_ #ifndef TRIGGERMANAGERAPI_HPP_
#define WAYPOINTSYSTEMAPI_HPP_ #define TRIGGERMANAGERAPI_HPP_
#include "lua.hpp" #include "lua.hpp"
#define TORTUGA_WAYPOINT_SYSTEM_API "waypoint_system" #define TORTUGA_TRIGGER_MANAGER_API "trigger_manager"
LUAMOD_API int openWaypointSystemAPI(lua_State* L); LUAMOD_API int openTriggerManagerAPI(lua_State* L);
#endif #endif
@@ -19,27 +19,27 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#include "waypoint_system_api.hpp" #include "trigger_system_api.hpp"
//all waypoint API headers //all trigger API headers
#include "waypoint_api.hpp" #include "trigger_api.hpp"
#include "waypoint_manager_api.hpp" #include "trigger_manager_api.hpp"
//useful "globals" //useful "globals"
//... //...
//This mimics linit.c to create a nested collection of all waypoint modules. //This mimics linit.c to create a nested collection of all trigger modules.
static const luaL_Reg funcs[] = { static const luaL_Reg funcs[] = {
{nullptr, nullptr} {nullptr, nullptr}
}; };
static const luaL_Reg libs[] = { static const luaL_Reg libs[] = {
{"Waypoint", openWaypointAPI}, {"Trigger", openTriggerAPI},
{"WaypointManager", openWaypointManagerAPI}, {"TriggerManager", openTriggerManagerAPI},
{nullptr, nullptr} {nullptr, nullptr}
}; };
int openWaypointSystemAPI(lua_State* L) { int openTriggerSystemAPI(lua_State* L) {
//create the table //create the table
luaL_newlibtable(L, libs); luaL_newlibtable(L, libs);
@@ -19,12 +19,12 @@
* 3. This notice may not be removed or altered from any source * 3. This notice may not be removed or altered from any source
* distribution. * distribution.
*/ */
#ifndef WAYPOINTMANAGERAPI_HPP_ #ifndef TRIGGERSYSTEMAPI_HPP_
#define WAYPOINTMANAGERAPI_HPP_ #define TRIGGERSYSTEMAPI_HPP_
#include "lua.hpp" #include "lua.hpp"
#define TORTUGA_WAYPOINT_MANAGER_API "waypoint_manager" #define TORTUGA_TRIGGER_SYSTEM_API "trigger_system"
LUAMOD_API int openWaypointManagerAPI(lua_State* L); LUAMOD_API int openTriggerSystemAPI(lua_State* L);
#endif #endif