diff options
author | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-10-07 18:35:47 -0400 |
---|---|---|
committer | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-10-07 18:35:47 -0400 |
commit | 5f53889d4357d8dba6e726ed38358eca96dbeb47 (patch) | |
tree | 8d82a65d007f7bf69dcd650c95bc3ee1463a3002 | |
parent | 1b63e00b6b08133f5ee37ed90043eee0f67942fd (diff) |
Added ability to change rendering offsets and sizes
-rw-r--r-- | Scripts/init.lua | 16 | ||||
-rw-r--r-- | src/components/Component.hpp | 2 | ||||
-rw-r--r-- | src/components/Position.hpp | 9 | ||||
-rw-r--r-- | src/components/Render.hpp | 19 | ||||
-rw-r--r-- | src/components/Velocity.hpp | 21 | ||||
-rw-r--r-- | src/physics.cpp | 4 | ||||
-rw-r--r-- | src/render.cpp | 19 | ||||
-rw-r--r-- | src/script.cpp | 4 | ||||
-rw-r--r-- | src/texture.cpp | 22 |
9 files changed, 66 insertions, 50 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua index 34e464b..46fe040 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -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 = { diff --git a/src/components/Component.hpp b/src/components/Component.hpp index 538d42b..5b0e3af 100644 --- a/src/components/Component.hpp +++ b/src/components/Component.hpp @@ -24,6 +24,8 @@ #include <entityx/entityx.h> #include <sol/sol.hpp> +#include <glm/glm.hpp> + #include <script/vectors.hpp> template<typename T> diff --git a/src/components/Position.hpp b/src/components/Position.hpp index 07009f9..fc5cb9a 100644 --- a/src/components/Position.hpp +++ b/src/components/Position.hpp @@ -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)); } diff --git a/src/components/Render.hpp b/src/components/Render.hpp index 81ca591..a9af51a 100644 --- a/src/components/Render.hpp +++ b/src/components/Render.hpp @@ -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" diff --git a/src/components/Velocity.hpp b/src/components/Velocity.hpp index 776c1dd..888cbb5 100644 --- a/src/components/Velocity.hpp +++ b/src/components/Velocity.hpp @@ -25,25 +25,24 @@ 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)); } diff --git a/src/physics.cpp b/src/physics.cpp index 1f70ecc..85b929c 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -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 diff --git a/src/render.cpp b/src/render.cpp index cdd31f8..7d9f9e9 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -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; diff --git a/src/script.cpp b/src/script.cpp index 3c0d0b2..4cfcddd 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -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); diff --git a/src/texture.cpp b/src/texture.cpp index b013bef..331413b 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -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"]); } } |