]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
lua - references
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 28 Sep 2017 21:28:59 +0000 (17:28 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 28 Sep 2017 21:28:59 +0000 (17:28 -0400)
include/systems/lua.hpp
src/systems/lua.cpp
src/systems/movement.cpp
xml/entities.xml

index 0d8528b370690ef148a2910640fea6d1be28489e..03c3a389214e0fd4d98c608b4cb54d5f73aea474 100644 (file)
@@ -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 {
index b093aff9a791814be469eba6762699a5fe02a548..fa1e93e3d524cda764946a7a8a3445d7098f5fb4 100644 (file)
@@ -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);
+}
+
index 4eb574d3cd0af8636c6522e486adf87c2c9ee25a..1a7112a8f6f6a0a17070e3399dc0240eace3d990 100644 (file)
@@ -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) {
index aecae45c6ef43552507e632da9f01497b982e28f..edee471fa97eda715e900ca3237466395c7714fb 100644 (file)
        <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>
        <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>