From d1b8fcb0d9cc5f0063e04218c21496559679c87e Mon Sep 17 00:00:00 2001 From: emb <> Date: Wed, 18 Nov 2015 23:06:40 -0600 Subject: [PATCH] Adding state machine code to repository. --- RetroFE/Source/Lua/LuaEvent.cpp | 41 +++++++++++++++++++++++++++++++++ RetroFE/Source/Lua/LuaEvent.h | 14 +++++++++++ RetroFE/Source/StateMachine.cpp | 30 ++++++++++++++++++++++++ RetroFE/Source/StateMachine.h | 23 ++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 RetroFE/Source/Lua/LuaEvent.cpp create mode 100644 RetroFE/Source/Lua/LuaEvent.h create mode 100644 RetroFE/Source/StateMachine.cpp create mode 100644 RetroFE/Source/StateMachine.h diff --git a/RetroFE/Source/Lua/LuaEvent.cpp b/RetroFE/Source/Lua/LuaEvent.cpp new file mode 100644 index 0000000..299d54d --- /dev/null +++ b/RetroFE/Source/Lua/LuaEvent.cpp @@ -0,0 +1,41 @@ +#include "LuaEvent.h" + +void LuaEvent::registerCallback(std::string type, std::string functionName, std::string complete) +{ + callbacks_[type] = functionName; + completeCallbacks_[type] = complete; +} + + +void LuaEvent::trigger(lua_State *l, std::string type) +{ + lua_getglobal(l, callbacks_[type].c_str()); + lua_call(l, 0, 0); +} + +bool LuaEvent::isComplete(lua_State *l, std::string type) +{ + bool retval = true; + lua_getglobal(l, callbacks_[type].c_str()); + if(lua_pcall(l, 0, 1, 0) == 0) + { + if(!lua_isnil(l, 1)) + { + if(!lua_toboolean(l, 1)) + { + retval = false; + } + } + lua_pop(l, 1); + } + + return retval; + // arguments + //lua_pushnumber(l, x); + //lua_pushnumber(l, y); + // call the function with 2 arguments, return 1 result + //lua_call(L, 2, 1); + //sum = (int)lua_tointeger(L, -1); + //lua_pop(L, 1); +} + diff --git a/RetroFE/Source/Lua/LuaEvent.h b/RetroFE/Source/Lua/LuaEvent.h new file mode 100644 index 0000000..575d48f --- /dev/null +++ b/RetroFE/Source/Lua/LuaEvent.h @@ -0,0 +1,14 @@ +#pragma once +#include "Lua.h" +#include +#include + +class LuaEvent { +public: + void registerCallback(std::string type, std::string functionName, std::string complete); + void trigger(lua_State *l, std::string type); + bool isComplete(lua_State *l, std::string type); +private: + std::map callbacks_; + std::map completeCallbacks_; +}; \ No newline at end of file diff --git a/RetroFE/Source/StateMachine.cpp b/RetroFE/Source/StateMachine.cpp new file mode 100644 index 0000000..33a7ec6 --- /dev/null +++ b/RetroFE/Source/StateMachine.cpp @@ -0,0 +1,30 @@ +#include "StateMachine.h" + +StateMachine::StateMachine(lua_State *l, LuaEvent *e) +: luaState_(l) +, luaEvent_(e) +, state_(ON_INIT_ENTER) +{ +} + +void StateMachine::update(float dt) +{ + switch(state_) { + case ON_INIT_ENTER: + luaEvent_->trigger(luaState_, "onInitEnter"); + state_ = ON_INIT_ENTER_WAIT; + break; + + case ON_INIT_ENTER_WAIT: + if(luaEvent_->isComplete(luaState_, "onInitEnter")) { + state_ = ON_INIT_EXIT; + } + break; + + case ON_INIT_EXIT: + luaEvent_->trigger(luaState_, "onInitExit"); + state_ = ON_INIT_EXIT_WAIT; + break; + + } +} diff --git a/RetroFE/Source/StateMachine.h b/RetroFE/Source/StateMachine.h new file mode 100644 index 0000000..b879f66 --- /dev/null +++ b/RetroFE/Source/StateMachine.h @@ -0,0 +1,23 @@ +#pragma once + +#include "Lua/Lua.h" +#include "Lua/LuaEvent.h" + +class StateMachine +{ +public: + StateMachine(lua_State *l, LuaEvent *e); + void initialize(); + void update(float dt); + +private: + lua_State *luaState_; + LuaEvent *luaEvent_; + + enum { + ON_INIT_ENTER, + ON_INIT_ENTER_WAIT, + ON_INIT_EXIT, + ON_INIT_EXIT_WAIT, + } state_; +}; \ No newline at end of file