diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/Component.hpp | 4 | ||||
-rw-r--r-- | src/components/Physics.hpp | 30 | ||||
-rw-r--r-- | src/components/Position.hpp | 25 | ||||
-rw-r--r-- | src/components/Render.hpp | 19 | ||||
-rw-r--r-- | src/components/Velocity.hpp | 21 |
5 files changed, 76 insertions, 23 deletions
diff --git a/src/components/Component.hpp b/src/components/Component.hpp index 576a059..3075cea 100644 --- a/src/components/Component.hpp +++ b/src/components/Component.hpp @@ -24,6 +24,10 @@ #include <entityx/entityx.h> #include <sol/sol.hpp> +#include <glm/glm.hpp> + +#include <script/vectors.hpp> + template<typename T> class Component : public entityx::Component<T> { diff --git a/src/components/Physics.hpp b/src/components/Physics.hpp index edf5ac5..2d6b9b2 100644 --- a/src/components/Physics.hpp +++ b/src/components/Physics.hpp @@ -25,8 +25,38 @@ struct Physics : Component<Physics> { public: bool standing = true; + bool gravity = true; + 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 + }; + Physics FromLua([[maybe_unused]] sol::object ref) { + if (ref.get_type() == sol::type::table) { + sol::table tab = ref; + + if (tab["gravity"].get_type() == sol::type::boolean) + this->gravity = tab["gravity"]; + + if (tab["hitbox"].get_type() == sol::type::table) { + sol::table hitbox = tab["hitbox"]; + if (hitbox["ll"] == sol::type::table) + corners[0] = Script::to<glm::vec2>(hitbox["ll"]); + if (hitbox["lr"] == sol::type::table) + corners[1] = Script::to<glm::vec2>(hitbox["lr"]); + if (hitbox["ul"] == sol::type::table) + corners[2] = Script::to<glm::vec2>(hitbox["ul"]); + if (hitbox["ur"] == sol::type::table) + corners[3] = Script::to<glm::vec2>(hitbox["ur"]); + } + } else { + throw std::string( + "Physics component table formatted incorrectly" + ); + } return *this; } diff --git a/src/components/Position.hpp b/src/components/Position.hpp index fcd62f8..a0adff5 100644 --- a/src/components/Position.hpp +++ b/src/components/Position.hpp @@ -24,25 +24,26 @@ struct Position : Component<Position> { public: - double x, y; + float x, y, z; - Position(double _x = 0, double _y = 0) : - x(_x), y(_y) {} + Position(float _x = 0.0f, float _y = 0.0f, float _z = 0.0f) : + x(_x), y(_y), z(_z) {} 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::vec3 vec = Script::to<glm::vec3>(ref); + this->x = vec.x; + this->y = vec.y; + this->z = vec.z; + return *this; } + glm::vec3 vec() + { + return glm::vec3(x, y, z); + } + 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 93be5d8..10a04fc 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 f48a9f3..420dd3d 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)); } |