diff options
Diffstat (limited to 'src/systems')
-rw-r--r-- | src/systems/lua.cpp | 52 | ||||
-rw-r--r-- | src/systems/movement.cpp | 8 |
2 files changed, 48 insertions, 12 deletions
diff --git a/src/systems/lua.cpp b/src/systems/lua.cpp index fa1e93e..e412334 100644 --- a/src/systems/lua.cpp +++ b/src/systems/lua.cpp @@ -1,25 +1,69 @@ #include <systems/lua.hpp> -void LuaScript::setGlobal(const LuaVariable& nv) +void LuaScript::setGlobal(const LuaVariable& nv) const { lua_pushnumber(state, std::get<float&>(nv)); lua_setglobal(state, std::get<std::string>(nv).c_str()); } -void LuaScript::operator()(std::vector<LuaVariable> vars) +void LuaScript::getReturns(std::vector<double>& rets) const +{ + int count = lua_gettop(state); + for (int i = 1; i <= count; i++) + rets.emplace_back(lua_tonumber(state, i)); + lua_pop(state, count); +} + +void LuaScript::operator()(const std::string& func, std::vector<LuaVariable> vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(func); + for (auto& v : vars) { + lua_getglobal(state, std::get<std::string>(v).c_str()); + std::get<float&>(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(const std::string& func, std::vector<double>& rets, + std::vector<LuaVariable> vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(func); + getReturns(rets); + for (auto& v : vars) { + lua_getglobal(state, std::get<std::string>(v).c_str()); + std::get<float&>(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(std::vector<LuaVariable> vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(); + for (auto& v : vars) { + lua_getglobal(state, std::get<std::string>(v).c_str()); + std::get<float&>(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(std::vector<double>& rets, std::vector<LuaVariable> vars) const { for (auto& v : vars) setGlobal(v); (*this)(); + getReturns(rets); for (auto& v : vars) { lua_getglobal(state, std::get<std::string>(v).c_str()); std::get<float&>(v) = lua_tonumber(state, -1); } } -void LuaScript::operator()(void) +void LuaScript::operator()(const std::string& s) const { - lua_getglobal(state, "update"); + lua_getglobal(state, s.c_str()); lua_pcall(state, 0, LUA_MULTRET, 0); } diff --git a/src/systems/movement.cpp b/src/systems/movement.cpp index 1a7112a..9948e03 100644 --- a/src/systems/movement.cpp +++ b/src/systems/movement.cpp @@ -77,14 +77,6 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e if (entity.has_component<Wander>()) { entity.component<Wander>()->script({LuaVariable("vely", direction.y), LuaVariable("velx", direction.x)}); - /*auto& countdown = entity.component<Wander>()->countdown; - - if (countdown > 0) { - countdown--; - } else { - countdown = 5000 + randGet() % 10 * 100; - direction.x = (randGet() % 3 - 1) * 0.004f; - }*/ } } }); |