diff --git a/ai_scripts/john.lua b/ai_scripts/john.lua index 3c74b31d..22d71275 100644 --- a/ai_scripts/john.lua +++ b/ai_scripts/john.lua @@ -1,7 +1,7 @@ -function sum(a,b) +function Sum(a,b) return a + b; end -function update() - return 1; +function Update() + return 1, 1, false, false; end \ No newline at end of file diff --git a/sources/Component/IAControllable/IAControllableComponent.cpp b/sources/Component/IAControllable/IAControllableComponent.cpp index 863135b9..7df7d830 100644 --- a/sources/Component/IAControllable/IAControllableComponent.cpp +++ b/sources/Component/IAControllable/IAControllableComponent.cpp @@ -13,7 +13,7 @@ namespace BBM : Component(entity), _scriptPath(scriptPath), state(luaL_newstate()) { luaL_dofile(state, scriptPath.c_str()); - lua_getglobal(state, "update"); + lua_getglobal(state, "Update"); if (!lua_isfunction(state, -1)) std::cout << "No update function in the script" << std::endl; } diff --git a/sources/System/IAControllable/IAControllableSystem.cpp b/sources/System/IAControllable/IAControllableSystem.cpp index f020504d..097be3df 100644 --- a/sources/System/IAControllable/IAControllableSystem.cpp +++ b/sources/System/IAControllable/IAControllableSystem.cpp @@ -13,21 +13,46 @@ namespace BBM typeid(ControllableComponent)}) { } + float IAControllableSystem::getReturnNumber(lua_State *state) + { + float res = 0; + + if (lua_isnil(state, -1)) + return res; + if (!lua_isnumber(state, -1)) + return res; + res = lua_tonumber(state, -1); + lua_pop(state, 1); + + return res; + } + + bool IAControllableSystem::getReturnBool(lua_State *state) + { + bool res = false; + + if (lua_isnil(state, -1)) + return res; + if (!lua_isboolean(state, -1)) + return res; + res = lua_toboolean(state, -1); + lua_pop(state, 1); + return res; + } + void IAControllableSystem::onFixedUpdate(WAL::Entity &entity) { auto &ia = entity.getComponent(); auto &controllable = entity.getComponent(); - lua_getglobal(ia.state, "update"); + lua_getglobal(ia.state, "Update"); //push parameters int nbParams = 0; - int nbReturn = 1; + int nbReturn = 4; lua_pcall(ia.state, nbParams, nbReturn, 0); - if (lua_isnil(ia.state, -1)) - return; - if (!lua_isnumber(ia.state, -1)) - return; - controllable.move.y = lua_tonumber(ia.state, -1); - lua_pop(ia.state, 1); + controllable.bomb = getReturnBool(ia.state); + controllable.jump = getReturnBool(ia.state); + controllable.move.y = getReturnNumber(ia.state); + controllable.move.x = getReturnNumber(ia.state); } } \ No newline at end of file diff --git a/sources/System/IAControllable/IAControllableSystem.hpp b/sources/System/IAControllable/IAControllableSystem.hpp index 8528c01d..b0ec6e70 100644 --- a/sources/System/IAControllable/IAControllableSystem.hpp +++ b/sources/System/IAControllable/IAControllableSystem.hpp @@ -11,6 +11,11 @@ namespace BBM //! @brief A system to handle keyboard entities. class IAControllableSystem : public WAL::System { + private: + //! @brief extract a number from the lua stack + float getReturnNumber(lua_State *state); + //! @brief extract a bool from the lua stack + bool getReturnBool(lua_State *state); public: //! @inherit void onFixedUpdate(WAL::Entity &entity) override;