From b709a392436d4ed17e214cd9e302ddbd23d71c21 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 12 Oct 2017 08:32:21 -0400 Subject: more lua scripting --- src/systems/lua.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++---- src/systems/movement.cpp | 8 -------- 2 files changed, 48 insertions(+), 12 deletions(-) (limited to 'src/systems') 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 -void LuaScript::setGlobal(const LuaVariable& nv) +void LuaScript::setGlobal(const LuaVariable& nv) const { lua_pushnumber(state, std::get(nv)); lua_setglobal(state, std::get(nv).c_str()); } -void LuaScript::operator()(std::vector vars) +void LuaScript::getReturns(std::vector& 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 vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(func); + for (auto& v : vars) { + lua_getglobal(state, std::get(v).c_str()); + std::get(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(const std::string& func, std::vector& rets, + std::vector vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(func); + getReturns(rets); + for (auto& v : vars) { + lua_getglobal(state, std::get(v).c_str()); + std::get(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(std::vector vars) const +{ + for (auto& v : vars) + setGlobal(v); + (*this)(); + for (auto& v : vars) { + lua_getglobal(state, std::get(v).c_str()); + std::get(v) = lua_tonumber(state, -1); + } +} + +void LuaScript::operator()(std::vector& rets, std::vector vars) const { for (auto& v : vars) setGlobal(v); (*this)(); + getReturns(rets); for (auto& v : vars) { lua_getglobal(state, std::get(v).c_str()); std::get(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()) { entity.component()->script({LuaVariable("vely", direction.y), LuaVariable("velx", direction.x)}); - /*auto& countdown = entity.component()->countdown; - - if (countdown > 0) { - countdown--; - } else { - countdown = 5000 + randGet() % 10 * 100; - direction.x = (randGet() % 3 - 1) * 0.004f; - }*/ } } }); -- cgit v1.2.3