From babd5e5596cefc7b2e15825976e084591f275d6f Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 28 Sep 2017 17:28:59 -0400 Subject: lua - references --- include/systems/lua.hpp | 17 +++++++---------- src/systems/lua.cpp | 23 +++++++++++++++++++++++ src/systems/movement.cpp | 4 ++-- xml/entities.xml | 21 ++++++++++++++++----- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/include/systems/lua.hpp b/include/systems/lua.hpp index 0d8528b..03c3a38 100644 --- a/include/systems/lua.hpp +++ b/include/systems/lua.hpp @@ -3,14 +3,18 @@ #include #include +#include +#include -#include +using LuaVariable = std::tuple; class LuaScript { private: lua_State* state; std::string script; + void setGlobal(const LuaVariable&); + public: LuaScript(const std::string& sc = "") : script(sc) { @@ -20,15 +24,8 @@ public: lua_pcall(state, 0, 0, 0); } - inline auto operator()(void) { - lua_getglobal(state, "update"); - lua_pcall(state, 0, LUA_MULTRET, 0); - if (lua_gettop(state) != 2) - return vec2(); - vec2 ret (lua_tonumber(state, 1), lua_tonumber(state, 2)); - lua_pop(state, 2); - return ret; - } + void operator()(std::vector vars); + void operator()(void); }; class LuaSystem { diff --git a/src/systems/lua.cpp b/src/systems/lua.cpp index b093aff..fa1e93e 100644 --- a/src/systems/lua.cpp +++ b/src/systems/lua.cpp @@ -1,2 +1,25 @@ #include +void LuaScript::setGlobal(const LuaVariable& nv) +{ + lua_pushnumber(state, std::get(nv)); + lua_setglobal(state, std::get(nv).c_str()); +} + +void LuaScript::operator()(std::vector vars) +{ + 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()(void) +{ + lua_getglobal(state, "update"); + lua_pcall(state, 0, LUA_MULTRET, 0); +} + diff --git a/src/systems/movement.cpp b/src/systems/movement.cpp index 4eb574d..1a7112a 100644 --- a/src/systems/movement.cpp +++ b/src/systems/movement.cpp @@ -75,8 +75,8 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e // make the entity wander // TODO initialX and range? if (entity.has_component()) { - auto vel = entity.component()->script(); - direction.x = vel.x; + entity.component()->script({LuaVariable("vely", direction.y), + LuaVariable("velx", direction.x)}); /*auto& countdown = entity.component()->countdown; if (countdown > 0) { diff --git a/xml/entities.xml b/xml/entities.xml index aecae45..edee471 100644 --- a/xml/entities.xml +++ b/xml/entities.xml @@ -52,15 +52,14 @@ countdown = 0 - velx = 0 update = function() if (countdown == 0) then - countdown = 5000 + math.random(0, 2000) - velx = (math.random(0, 3) - 1) * 0.004 + countdown = math.random(4000, 6000) + velx = math.random(-1, 1) * 0.004 + vely = .1 end countdown = countdown - 1 - return velx, 0 end @@ -78,7 +77,19 @@ - + + countdown = 0 + velx = 0 + + update = function() + if (countdown == 0) then + countdown = math.random(3000, 5000) + velx = math.random(-1, 1) * 0.005 + end + countdown = countdown - 1 + return velx + end + -- cgit v1.2.3