]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Added velocity component, and force add component dependencies when they don't exist
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 31 Aug 2019 05:22:14 +0000 (01:22 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 31 Aug 2019 05:22:14 +0000 (01:22 -0400)
Scripts/init.lua
src/components/Position.hpp
src/components/Script.hpp
src/components/Velocity.hpp [new file with mode: 0644]
src/script.cpp

index 807ef9a3cebe8b04d3c7bfc8ead55e81a48aeb34..f92e36667bcdab0b713ea1110e113b74af9349d2 100644 (file)
@@ -10,6 +10,10 @@ bird = {
 }
 
 dog = {
+    Velocity = {
+        x = 0.01,
+        y = 10.5
+    },
     Position = {
         x = 6.5,
         y = 1.3
index 0d693931b26395db949f3d8aa71da6e632cb3e6b..0462d1ad29855743d2090e207aafae0b2f08d7ea 100644 (file)
@@ -24,7 +24,7 @@
 struct Position : Component<Position>, entityx::Component<Position>
 {
     public:
-        float x,y;
+        float x, y;
         Position(float _x, float _y): x(_x), y(_y) {}
         Position(void): x(0), y(0) {}
 
index 0e7c9dbf6b722251b6c174939867432c20423f74..75708025c03738312a2e0142e442cc9c1df23529 100644 (file)
@@ -1,6 +1,6 @@
-/*
+/**
+ * @file Script.hpp
  * 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
@@ -39,10 +39,6 @@ struct Scripted : Component<Scripted>, entityx::Component<Scripted>
 
         Scripted FromLua(sol::object)
         {
-            //if (ref.get_type() == sol::type::function) {
-            //    this->luafunc = ref;
-            //}
-            //init = true;
             return *this;
         }
 
diff --git a/src/components/Velocity.hpp b/src/components/Velocity.hpp
new file mode 100644 (file)
index 0000000..eacadf2
--- /dev/null
@@ -0,0 +1,47 @@
+/**
+ * @file Velocity.hpp
+ *
+ * Copyright (C) 2019  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 VELOCITY_HPP_
+#define VELOCITY_HPP_
+
+#include <components/Component.hpp>
+
+struct Velocity : Component<Velocity>, entityx::Component<Velocity>
+{
+    public:
+        float x, y;
+        Velocity(): x(0), y(0) {}
+        Velocity(float _x, float _y): x(_x), y(_y) {}
+
+        Velocity FromLua(sol::object ref)
+        {
+            if (ref.get_type() == sol::type::table) {
+                sol::table tab = ref;
+                if (tab["x"] != nullptr)
+                    this->x = tab["x"];
+                if (tab["y"] != nullptr)
+                    this->y = tab["y"];
+            } else {
+                throw std::string("Velocity table not formatted properly");
+            }
+            return *this;
+        }
+};
+
+#endif//VELOCITY_HPP_
index 4dc3a2a34c9b6fc197ef7814ed9536a48f90d02f..1a6bda8615477b6add86827ce249208e23ceb18e 100644 (file)
@@ -79,6 +79,7 @@ void ScriptSystem::doFile(void)
 #include <components/Name.hpp>
 #include <components/Render.hpp>
 #include <components/Script.hpp>
+#include <components/Velocity.hpp>
 
 void ScriptSystem::scriptExport()
 {
@@ -130,10 +131,19 @@ sol::table ScriptSystem::spawn(sol::object param)
         }
 
         if (tab["Render"] != nullptr) {
+            if (!e.has_component<Position>())
+                e.assign<Position>();
             (*toRet)["Render"] =
                 e.assign<Render>(Render().FromLua(tab["Render"])).get();
         }
 
+        if (tab["Velocity"] != nullptr) {
+            if (!e.has_component<Position>())
+                e.assign<Position>();
+            (*toRet)["Velocity"] =
+                e.assign<Velocity>(Velocity().FromLua(tab["Velocity"])).get();
+        }
+
     } else {
         std::cerr << "Parameter to spawn() must be a table!" << std::endl;
     }