aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2017-09-28 17:28:59 -0400
committerClyne Sullivan <tullivan99@gmail.com>2017-09-28 17:28:59 -0400
commitbabd5e5596cefc7b2e15825976e084591f275d6f (patch)
tree9f7eeb7fd0c375e95c44f4257849f40676965a80
parent3ffd09ad483a77053ebc566c3ab464a1c85e7a44 (diff)
lua - references
-rw-r--r--include/systems/lua.hpp17
-rw-r--r--src/systems/lua.cpp23
-rw-r--r--src/systems/movement.cpp4
-rw-r--r--xml/entities.xml21
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 <string>
#include <lua.hpp>
+#include <tuple>
+#include <vector>
-#include <vector2.hpp>
+using LuaVariable = std::tuple<std::string, float&>;
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<LuaVariable> 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 <systems/lua.hpp>
+void LuaScript::setGlobal(const LuaVariable& nv)
+{
+ lua_pushnumber(state, std::get<float&>(nv));
+ lua_setglobal(state, std::get<std::string>(nv).c_str());
+}
+
+void LuaScript::operator()(std::vector<LuaVariable> vars)
+{
+ 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()(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<Wander>()) {
- auto vel = entity.component<Wander>()->script();
- direction.x = vel.x;
+ entity.component<Wander>()->script({LuaVariable("vely", direction.y),
+ LuaVariable("velx", direction.x)});
/*auto& countdown = entity.component<Wander>()->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 @@
<Dialog />
<Wander>
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
</Wander>
</npc>
@@ -78,7 +77,19 @@
<Solid />
<Physics />
<Name value="SKIRL" />
- <Wander />
+ <Wander>
+ 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
+ </Wander>
<!--<Aggro arena="arena.xml" />
<Trigger />-->
<Drop>