]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Added ability to change rendering offsets and sizes
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Mon, 7 Oct 2019 22:35:47 +0000 (18:35 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Mon, 7 Oct 2019 22:35:47 +0000 (18:35 -0400)
Scripts/init.lua
src/components/Component.hpp
src/components/Position.hpp
src/components/Render.hpp
src/components/Velocity.hpp
src/physics.cpp
src/render.cpp
src/script.cpp
src/texture.cpp

index 34e464b66a474d11ec7d5f7847756fc0e4f8aaff..46fe040f6948966a868ca58fc3182aa55759ab7b 100644 (file)
@@ -34,24 +34,24 @@ player = {
         x = 0.0,
         y = 0.0
     },
-    Hitbox = {
-        bounds = {
+    Physics = {
+        gravity = true,
+        hitbox = {
             {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",
     Render = {
         texture = "Assets/player.png",
         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},
+        offset = {
+            ll = {x = -0.5, y = -0.8},
+            lr = {x = 0.5, y = -0.8},
+            ul = {x = -0.5, y = 0.8},
+            ur = {x = 0.5, y = 0.8},
         }
     },
     Light = {
index 538d42b7cc8dafcaab9f7b1d617282e4489d322d..5b0e3af2db15dfc86b5dcf71366e0ac0e65e3df4 100644 (file)
@@ -24,6 +24,8 @@
 #include <entityx/entityx.h>
 #include <sol/sol.hpp>
 
+#include <glm/glm.hpp>
+
 #include <script/vectors.hpp>
 
 template<typename T>
index 07009f9d78f6da05872b421958ce22ece078579d..fc5cb9ac11dbe620bfa50a50dedbabcd67cbb0f7 100644 (file)
@@ -24,9 +24,9 @@
 struct Position : Component<Position>
 {
 public:
-    double x, y;
+    float x, y;
 
-    Position(double _x = 0, double _y = 0) :
+    Position(float _x = 0, float _y = 0) :
         x(_x), y(_y) {}
 
     Position FromLua(sol::object ref)
@@ -38,6 +38,11 @@ public:
         return *this;
     }
 
+    glm::vec2 vec()
+    {
+        return glm::vec2(x, y);
+    }
+
     void serialize(cereal::JSONOutputArchive& ar) final {
         ar(CEREAL_NVP(x), CEREAL_NVP(y));
     }
index 81ca591c011ef37e5837944cf82c782eaf3c98cb..a9af51a79f822ed86a55f8491a1f0a8e6e879b52 100644 (file)
@@ -28,6 +28,12 @@ public:
     Texture normal;
     bool visible;
     bool flipX = false;
+    glm::vec2 corners[4] = {
+        glm::vec2(-0.5, -0.5), // lower left
+        glm::vec2( 0.5, -0.5), // lower right
+        glm::vec2(-0.5,  0.5), // upper left
+        glm::vec2( 0.5,  0.5)  // upper right
+    };
 
     Render(std::string _file) :
         texture(_file), visible(true) {}
@@ -46,6 +52,19 @@ public:
                 this->normal = Texture(tab.get<std::string>("normal"));
             if (tab["flipx"].get_type() == sol::type::boolean)
                 this->flipX = tab["flipx"];
+
+            if (tab["offset"].get_type() == sol::type::table) {
+                sol::table offset = tab["offset"];
+                if (offset["ll"] == sol::type::table)
+                    corners[0] = Script::to<glm::vec2>(offset["ll"]);
+                if (offset["lr"] == sol::type::table)
+                    corners[1] = Script::to<glm::vec2>(offset["lr"]);
+                if (offset["ul"] == sol::type::table)
+                    corners[2] = Script::to<glm::vec2>(offset["ul"]);
+                if (offset["ur"] == sol::type::table)
+                    corners[3] = Script::to<glm::vec2>(offset["ur"]);
+            }
+
         } else {
             throw std::string(
                 "Render component table formatted incorrectly"
index 776c1ddcac7196348792cd82a33d22f5b9355b7e..888cbb5f06208d936a377666af6f9b69ae2cc2d1 100644 (file)
 struct Velocity : Component<Velocity>
 {
 public:
-    double x, y;
+    float x, y;
 
-    Velocity(double _x = 0, double _y = 0) :
+    Velocity(float _x = 0, float _y = 0) :
         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");
-        }
+        glm::vec2 vel = Script::to<glm::vec2>(ref);
+        this->x = vel.x;
+        this->y = vel.y;
         return *this;
     }
 
+    glm::vec2 vec()
+    {
+        return glm::vec2(x, y);
+    }
+
     void serialize(cereal::JSONOutputArchive& ar) final {
         ar(CEREAL_NVP(x), CEREAL_NVP(y));
     }
index 1f70ecc6375b317834ba426f46d5d37c47a6762e..85b929cf800a557f5c62d87f1f5d237d2b1518c7 100644 (file)
@@ -41,10 +41,10 @@ void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
         pos.x += (vel.x * dt/1000.0);
         pos.y += (vel.y * dt/1000.0);
 
-        // TODO make this intersect world instead of 0 y
+        // If the entity has physics
         if (has_phys) {
 
-            double fallPosition = currentWorld->getHeight(pos.x, pos.y, 0.0);
+            float fallPosition = currentWorld->getHeight(pos.x, pos.y, 0.0);
 
             Physics *p = e.component<Physics>().get();
             // TODO only make this occur when the entity has a hitbox
index cdd31f83f8e4ea48773dcdd8a336087b223e76f6..7d9f9e9037d619ec50fa31111f7ab225aa1f918e 100644 (file)
@@ -167,19 +167,18 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
         //if (e.has_component<Scripted>()) {
         //    e.component<Scripted>()->updateRender();
         //}
-
-        float w = 0.5f;
-        float h = (float)rend.texture.height/rend.texture.width;
+        
+        auto& c = rend.corners;
 
         GLuint tri_vbo;
         GLfloat tri_data[] = {
-                (float)p.x-w, (float)p.y  , 0.0f, 0.0f, 1.0f, 1.0f,
-                (float)p.x+w, (float)p.y  , 0.0f, 1.0f, 1.0f, 1.0f,
-                (float)p.x-w, (float)p.y+h, 0.0f, 0.0f, 0.0f, 1.0f,
-                                                             
-                (float)p.x+w, (float)p.y  , 0.0f, 1.0f, 1.0f, 1.0f,
-                (float)p.x+w, (float)p.y+h, 0.0f, 1.0f, 0.0f, 1.0f,
-                (float)p.x-w, (float)p.y+h, 0.0f, 0.0f, 0.0f, 1.0f,
+                p.x+c[0].x, p.y+c[0].y, 0.0f, 0.0f, 1.0f, 1.0f,
+                p.x+c[1].x, p.y+c[1].y, 0.0f, 1.0f, 1.0f, 1.0f,
+                p.x+c[2].x, p.y+c[2].y, 0.0f, 0.0f, 0.0f, 1.0f,
+                                   
+                p.x+c[1].x, p.y+c[1].y, 0.0f, 1.0f, 1.0f, 1.0f,
+                p.x+c[3].x, p.y+c[3].y, 0.0f, 1.0f, 0.0f, 1.0f,
+                p.x+c[2].x, p.y+c[2].y, 0.0f, 0.0f, 0.0f, 1.0f,
         };
 
         bool flipped = false;
index 3c0d0b255b4df6ac16f40a9b11e08091df795c30..4cfcdddbaaaaa1fc1c89391ff3bfe42036660db3 100644 (file)
@@ -94,7 +94,7 @@ void ScriptSystem::scriptExport(void)
         [this](sol::object t){ return worldSystem.addWorld(t); };
 
     lua.new_usertype<Position>("Position",
-            sol::constructors<Position(double x, double y), Position()>(),
+            sol::constructors<Position(float x, float y), Position()>(),
             "x", &Position::x,
             "y", &Position::y);
 
@@ -109,7 +109,7 @@ void ScriptSystem::scriptExport(void)
             "flipx", &Render::flipX);
 
     lua.new_usertype<Velocity>("Velocity",
-            sol::constructors<Velocity(double, double), Velocity()>(),
+            sol::constructors<Velocity(float, float), Velocity()>(),
             "x", &Velocity::x,
             "y", &Velocity::y);
 
index b013bef5c6d85af7768860f5efd363527e2e3f9d..331413b9505fedaa73cd500ceed905fbedf0ac2c 100644 (file)
@@ -26,6 +26,8 @@
 #include <unordered_map>
 #include <iostream>
 
+#include <script/vectors.hpp>
+
 struct TextureData
 {
     GLuint tex = 0;
@@ -101,20 +103,10 @@ Texture::Texture(sol::object param)
         else
             return; // If we don't have image data just return a null image
 
-        if (tab["offset"] == sol::type::table) {
-            sol::table off = tab["offset"];
-            if (off["x"] == sol::type::number)
-                offset.x = off.get<float>("x")/width;
-            if (off["y"] == sol::type::number)
-                offset.y = off.get<float>("y")/height;
-        }
-
-        if (tab["size"] == sol::type::table) {
-            sol::table siz = tab["size"];
-            if (siz["x"] == sol::type::number)
-                size.x = siz.get<float>("x")/width;
-            if (siz["y"] == sol::type::number)
-                size.y = siz.get<float>("y")/height;
-        }
+        if (tab["offset"].get_type() == sol::type::table)
+            offset = Script::to<glm::vec2>(tab["offset"]);
+
+        if (tab["size"].get_type() == sol::type::table)
+            size = Script::to<glm::vec2>(tab["size"]);
     }
 }