]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Added script conversion code, can now convert table to vec2, vec3 and vec4
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Mon, 7 Oct 2019 21:39:52 +0000 (17:39 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Mon, 7 Oct 2019 21:39:52 +0000 (17:39 -0400)
Assets/world/world1/layers/0/hitbox.png
Makefile
Scripts/init.lua
src/components/Component.hpp
src/components/Position.hpp
src/script.cpp
src/script/vectors.cpp [new file with mode: 0644]
src/script/vectors.hpp [new file with mode: 0644]
src/world.cpp
src/world.hpp

index 881b13ea81b9e4239995238f3a6c402bb327ba98..9ae9622a4111e0845d4b2e8f6c7f71d873d638a8 100644 (file)
Binary files a/Assets/world/world1/layers/0/hitbox.png and b/Assets/world/world1/layers/0/hitbox.png differ
index e5861100cccda8e995cf009841b50e4a8857b9e6..9c9fafe9663e110a5290fec1e8f3beb8890cf795 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -40,7 +40,7 @@ LIBS   = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -lluajit -ldl -lGLEW -lGL \
                 -lSDL2_image -lSOIL -lfreetype
 
 CXXFLAGS = -ggdb -std=c++17 -Wall -Wextra -Werror -pedantic \
-                  -Wno-class-memaccess -Wno-implicit-fallthrough -m64
+                  -Wno-class-memaccess -Wno-implicit-fallthrough -m64 -O1
 
 CXXINCS = -Isrc -I$(LIBDIR)/LuaJIT/src -I$(LIBDIR)/entityx \
                  -I$(LIBDIR)/LuaBridge/Source -I$(LIBDIR)/sol2/include \
index 1693d0c41cb00f09b56a4c05bdaabf11b1be0d19..34e464b66a474d11ec7d5f7847756fc0e4f8aaff 100644 (file)
@@ -28,20 +28,31 @@ player = {
         end
     },
     Position = {
-        x = 15,
-        y = 75
+        15.0, 10.0
     },
     Velocity = {
         x = 0.0,
         y = 0.0
     },
+    Hitbox = {
+        bounds = {
+            {x = -0.5, y = -0.8},
+            {x = 0.5, y = -0.8},
+            {x = -0.5, y = 0.8},
+            {x = 0.5, y = 0.8},
+        }
+    },
     Physics = 0,
     Name = "bord",
-    hellotrue = true,
-    hellofalse = false,
     Render = {
         texture = "Assets/player.png",
-        visible = true
+        visible = true,
+        bounds = {
+            {x = -0.5, y = -0.8},
+            {x = 0.5, y = -0.8},
+            {x = -0.5, y = 0.8},
+            {x = 0.5, y = 0.8},
+        }
     },
     Light = {
         r = 1.0,
@@ -50,11 +61,6 @@ player = {
         strength = 1.0
     },
     Idle = function(self)
-        --if (self.visibleTick >= 20) then
-        --    self.Render.visible = not self.Render.visible
-        --    self.visibleTick = 0
-        --end
-        --self.visibleTick = self.visibleTick + 1
     end,
     visibleTick = 0
 }
@@ -86,7 +92,7 @@ ball = {
 dofile("Scripts/world.lua")
 
 playerSpawn = game.spawn(player);
---game.spawn(ball);
+game.spawn(ball);
 
 -------------------
 --  SERIALIZING  --
index 2928366af1aff1407e7610f62374b00722f25fad..538d42b7cc8dafcaab9f7b1d617282e4489d322d 100644 (file)
@@ -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>
 {
index 56e8707653bc8210a3db5c5ac1b62279286f71af..07009f9d78f6da05872b421958ce22ece078579d 100644 (file)
@@ -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;
     }
 
index 81094576d86c74be2ecab84b9dfb9a54fe9edddc..3c0d0b255b4df6ac16f40a9b11e08091df795c30 100644 (file)
@@ -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 (file)
index 0000000..4f8b344
--- /dev/null
@@ -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 (file)
index 0000000..9ee0b31
--- /dev/null
@@ -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_
index a3923fa8c4d8e06d14b4f960daaba8b38886c617..fb846eb594d922e31e4cd956436c7692ab270882 100644 (file)
@@ -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);
         }
     }
index 6776e2cb34fa86592236039d641484063ad827a1..66d9e3f13ef4aba90e8fa01a98a97017ee28ad62 100644 (file)
@@ -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;
                 }
             }