From 1b63e00b6b08133f5ee37ed90043eee0f67942fd Mon Sep 17 00:00:00 2001
From: Andy Belle-Isle <drumsetmonkey@gmail.com>
Date: Mon, 7 Oct 2019 17:39:52 -0400
Subject: Added script conversion code, can now convert table to vec2, vec3 and
 vec4

---
 src/components/Component.hpp |   2 +
 src/components/Position.hpp  |  13 ++----
 src/script.cpp               |   2 +-
 src/script/vectors.cpp       | 108 +++++++++++++++++++++++++++++++++++++++++++
 src/script/vectors.hpp       |  31 +++++++++++++
 src/world.cpp                |   1 -
 src/world.hpp                |  12 ++---
 7 files changed, 152 insertions(+), 17 deletions(-)
 create mode 100644 src/script/vectors.cpp
 create mode 100644 src/script/vectors.hpp

(limited to 'src')

diff --git a/src/components/Component.hpp b/src/components/Component.hpp
index 2928366..538d42b 100644
--- a/src/components/Component.hpp
+++ b/src/components/Component.hpp
@@ -24,6 +24,8 @@
 #include <entityx/entityx.h>
 #include <sol/sol.hpp>
 
+#include <script/vectors.hpp>
+
 template<typename T>
 class Component : public entityx::Component<T>
 {
diff --git a/src/components/Position.hpp b/src/components/Position.hpp
index 56e8707..07009f9 100644
--- a/src/components/Position.hpp
+++ b/src/components/Position.hpp
@@ -31,15 +31,10 @@ public:
 
     Position 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("Position table not formatted properly");
-        }
+        glm::vec2 vec = Script::to<glm::vec2>(ref);
+        this->x = vec.x;
+        this->y = vec.y;
+
         return *this;
     }
 
diff --git a/src/script.cpp b/src/script.cpp
index 8109457..3c0d0b2 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -145,7 +145,7 @@ void ScriptSystem::scriptExport(void)
 
 sol::table ScriptSystem::spawn(sol::object param)
 {
-    sol::table* toRet; // "Entitiy" table to be returned
+    sol::table* toRet = NULL; // "Entitiy" table to be returned
     if (param.get_type() == sol::type::table) {
         sol::table tab = param; // Cast the generic parameter to a table
 
diff --git a/src/script/vectors.cpp b/src/script/vectors.cpp
new file mode 100644
index 0000000..4f8b344
--- /dev/null
+++ b/src/script/vectors.cpp
@@ -0,0 +1,108 @@
+/*
+ * 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/>.
+ */
+
+#include "vectors.hpp"
+
+#include <glm/glm.hpp>
+
+#include <iostream>
+
+namespace Script
+{
+    template<class T>
+    T to(sol::object obj)
+    {
+        (void)obj;
+        T fake;
+        return fake;
+    }
+
+    template<>
+    glm::vec2 to<glm::vec2>(sol::object obj)
+    {
+        glm::vec2 toReturn;
+
+        if (obj.get_type() == sol::type::table) {
+            sol::table table = obj;
+            // X
+            if (table["x"] == sol::type::number) {
+                toReturn.x = table["x"];
+            } else if (table[1] == sol::type::number) {
+                toReturn.x = table[1];
+            }
+            // Y
+            if (table["y"] == sol::type::number) {
+                toReturn.y = table["y"];
+            } else if (table[2] == sol::type::number) {
+                toReturn.y = table[2];
+            }
+        } else {
+            std::cerr << "Vectors must be in table form" << std::endl;
+        }
+
+        return toReturn;
+    }
+
+    template<>
+    glm::vec3 to<glm::vec3>(sol::object obj)
+    {
+        glm::vec3 toReturn;
+
+        if (obj.get_type() == sol::type::table) {
+            sol::table table = obj;
+            glm::vec2 base = to<glm::vec2>(table);
+            toReturn.x = base.x;
+            toReturn.y = base.y;
+            
+            // Z
+            if (table["z"] == sol::type::number) {
+                toReturn.z = table["z"];
+            } else if (table[3] == sol::type::number) {
+                toReturn.z = table[3];
+            }
+        } else {
+            std::cerr << "Vectors must be in table form" << std::endl;
+        }
+
+        return toReturn;
+    }
+
+    template<>
+    glm::vec4 to<glm::vec4>(sol::object obj)
+    {
+        glm::vec4 toReturn;
+
+        if (obj.get_type() == sol::type::table) {
+            sol::table table = obj;
+            glm::vec3 base = to<glm::vec3>(table);
+            toReturn.x = base.x;
+            toReturn.y = base.y;
+            toReturn.z = base.z;
+            
+            // W
+            if (table["w"] == sol::type::number) {
+                toReturn.w = table["w"];
+            } else if (table[4] == sol::type::number) {
+                toReturn.w = table[4];
+            }
+        } else {
+            std::cerr << "Vectors must be in table form" << std::endl;
+        }
+
+        return toReturn;
+    }
+}
diff --git a/src/script/vectors.hpp b/src/script/vectors.hpp
new file mode 100644
index 0000000..9ee0b31
--- /dev/null
+++ b/src/script/vectors.hpp
@@ -0,0 +1,31 @@
+/**
+ * @file vectors.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 SCRIPT_VECTORS_HPP_
+#define SCRIPT_VECTORS_HPP_
+
+#include <sol/sol.hpp>
+
+namespace Script
+{
+    template<class T>
+    T to(sol::object obj);
+}
+
+#endif//SCRIPT_VECTORS_HPP_
diff --git a/src/world.cpp b/src/world.cpp
index a3923fa..fb846eb 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -132,7 +132,6 @@ double World::getHeight(double x, double y, double z)
                     Y = h;
                 h++;
             }
-            std::cout << l.drawLayer << "," << wx << "," << Y << std::endl;
             return (Y/unitSize);
         }
     }
diff --git a/src/world.hpp b/src/world.hpp
index 6776e2c..66d9e3f 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -94,16 +94,16 @@ public:
                                 &width, &height, &channels,
                                 SOIL_LOAD_RGBA);
 
-            for (int w = 0; w < width*4; w+=4) {
+            for (int w = 0; w < width*channels; w+=channels) {
                 hitbox.push_back(std::vector<bool>(height));
                 for (int h = 0; h < height; h++) {
-                    unsigned char* c = &box[(w) + (width*h*4)];
-                    // we want to read the alpha
-                    if (c[3]) {
-                        hitbox[w/4][height-h] = true;
+                    unsigned char* c = &box[(w) + (width*h*channels)];
+                    // we want to read the last channel (alpha)
+                    if (c[channels-1]) {
+                        hitbox[w/channels][height-h] = true;
                     }
                     else
-                        hitbox[w/4][height-h] = false;
+                        hitbox[w/channels][height-h] = false;
                 }
             }
 
-- 
cgit v1.2.3