]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Lua spawned entities have "Idle" function
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 31 Aug 2019 04:40:21 +0000 (00:40 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 31 Aug 2019 04:40:21 +0000 (00:40 -0400)
Every entity spawned from Lua is given the "Scripted" component,
this component holds onto the Entities master table and it's idle
function

Scripts/init.lua
src/components/IdleFunc.hpp [deleted file]
src/components/Script.hpp [new file with mode: 0644]
src/engine.cpp
src/script.cpp

index 851c1806b636fd204360bdfba04454addb192025..807ef9a3cebe8b04d3c7bfc8ead55e81a48aeb34 100644 (file)
@@ -4,7 +4,7 @@ bird = {
         y = 3.4
     },
     Name = "bord",
-    init = function(self)
+    Init = function(self)
         print(self.Position.x .. "," .. self.Position.y)
     end
 }
@@ -18,8 +18,11 @@ dog = {
         texture = "assets/tex.png",
         visible = true
     },
-    init = function(self)
+    Init = function(self)
         print(self.Position.x .. "," .. self.Position.y)
+    end,
+    Idle = function(self)
+        self.Position.x = self.Position.x + 0.01;
     end
 }
 
@@ -31,12 +34,10 @@ animal = {
 }
 
 birdSpawn = game.spawn(bird);
-birdSpawn:init()
-print(birdSpawn.Name.value)
+birdSpawn:Init()
 
 dogSpawn = game.spawn(dog);
-dogSpawn:init()
+dogSpawn:Init()
 dogSpawn.Position.x = 37.5
 
 animalSpawn = game.spawn(animal);
-animalSpawn.Render.texture = "assets/newText.png"
diff --git a/src/components/IdleFunc.hpp b/src/components/IdleFunc.hpp
deleted file mode 100644 (file)
index 29bcd7b..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
- * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef IDLEFUNC_HPP_
-#define IDLEFUNC_HPP_
-
-#include <components/Component.hpp>
-
-struct IdleFunc : Component<IdleFunc>, entityx::Component<IdleFunc>
-{
-    sol::function luafunc;
-    public:
-        IdleFunc() {}
-
-        IdleFunc FromLua(sol::object ref)
-        {
-            if (ref.get_type() == sol::type::function) {
-                this->luafunc = ref;
-            }
-            return *this;
-        }
-};
-
-#endif//IDLEFUNC_HPP_
diff --git a/src/components/Script.hpp b/src/components/Script.hpp
new file mode 100644 (file)
index 0000000..0e7c9db
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
+ * Author: Belle-Isle, Andrew <drumsetmonkey@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SCRIPT_COMPONENT_HPP_
+#define SCRIPT_COMPONENT_HPP_
+
+#include <components/Component.hpp>
+
+struct Scripted : Component<Scripted>, entityx::Component<Scripted>
+{
+    public:
+        sol::table caller;
+        Scripted() {}
+        Scripted(sol::table call): caller(call) {}
+
+
+        ~Scripted()
+        {}
+
+        void cleanup()
+        {
+            caller = sol::nil;
+        }
+
+        Scripted FromLua(sol::object)
+        {
+            //if (ref.get_type() == sol::type::function) {
+            //    this->luafunc = ref;
+            //}
+            //init = true;
+            return *this;
+        }
+
+        void exec() {
+            if (caller["Idle"] == sol::type::function)
+                caller["Idle"](caller); // Call idle function and pass itself
+                                        //  in or to fulfill the 'self' param
+        }
+};
+
+#endif//SCRIPT_COMPONENT_HPP_
index 4803c64b6102f630952c68d82dd892232d388489..317e11616f365c84128fbd5b6f763746e6afed60 100644 (file)
@@ -25,6 +25,9 @@
 #include "window.hpp"
 #include "script.hpp"
 
+#include "components/Script.hpp"
+#include "components/Position.hpp"
+
 int Engine::init(void)
 {
        systems.add<GameRunSystem>();
@@ -44,8 +47,16 @@ void Engine::logicLoop(void)
 
        while (shouldRun()) {
                systems.update<InputSystem>(dt);
+
+        // All entities with an idle function should be run here
+        entities.each<Scripted>([](entityx::Entity, Scripted &f){
+            f.exec();
+        });
                std::this_thread::sleep_for(100ms);
        }
+
+    // Remove all Lua references from entities
+    entities.each<Scripted>([](entityx::Entity, Scripted &f){ f.cleanup(); });
 }
 
 void Engine::renderLoop(void)
index bb413fee04e9597db8dfd5b93b202cecb23aa101..4dc3a2a34c9b6fc197ef7814ed9536a48f90d02f 100644 (file)
@@ -62,36 +62,23 @@ int ScriptSystem::init(void)
     return 0;
 }
 
-// TODO move all of these below once the test printouts are gone
-#include <components/Position.hpp>
-#include <components/Name.hpp>
-#include <components/Render.hpp>
-#include <components/IdleFunc.hpp>
-
 void ScriptSystem::doFile(void)
 {
+
     auto result = lua.script_file("Scripts/init.lua");
+
     if (!result.valid()) {
         std::cout << "Lua error: " << std::endl;
     }
-
-    manager->each<Position>(
-            [&](entityx::Entity, Position& p)
-            {std::cout << p.x << "," << p.y << std::endl;});
-
-    manager->each<Name>(
-            [&](entityx::Entity, Name& n)
-            {std::cout << n.name << std::endl;});
-
-    manager->each<Render>(
-            [&](entityx::Entity, Render& r)
-            {std::cout << r.texture << ": " << r.visible << std::endl;});
 }
 
 /********************
 *  SCRIPT PARSING  *
 ********************/
-// TODO here
+#include <components/Position.hpp>
+#include <components/Name.hpp>
+#include <components/Render.hpp>
+#include <components/Script.hpp>
 
 void ScriptSystem::scriptExport()
 {
@@ -119,29 +106,31 @@ void ScriptSystem::scriptExport()
 
 sol::table ScriptSystem::spawn(sol::object param)
 {
-    sol::table toRet = lua.create_table_with();
-    
+    sol::table* toRet;
     if (param.get_type() == sol::type::table) {
         sol::table tab = param;
 
         entityx::Entity e = manager->create();
+        auto d = e.assign<Scripted>().get();
+        d->caller = lua.create_table();
+        toRet = &(d->caller);
+
+        *toRet = tab; // Copy table to our new entity to preserve functions
+                      //  this reduces the amount of work done in this function
+                      //  as well
 
         if (tab["Position"] != nullptr) {
-            toRet["Position"] = 
+            (*toRet)["Position"] = 
                 e.assign<Position>(Position().FromLua(tab["Position"])).get();
         }
 
-        if (tab["init"] != nullptr) {
-            toRet["init"] = tab["init"];
-        }
-
         if (tab["Name"] != nullptr) {
-            toRet["Name"] =
+            (*toRet)["Name"] =
                 e.assign<Name>(Name().FromLua(tab["Name"])).get();
         }
 
         if (tab["Render"] != nullptr) {
-            toRet["Render"] =
+            (*toRet)["Render"] =
                 e.assign<Render>(Render().FromLua(tab["Render"])).get();
         }
 
@@ -149,5 +138,5 @@ sol::table ScriptSystem::spawn(sol::object param)
         std::cerr << "Parameter to spawn() must be a table!" << std::endl;
     }
 
-    return toRet;
+    return *toRet;
 }