From dd57c59c39dcaa35d5bbd2d6fec2585736768ab7 Mon Sep 17 00:00:00 2001 From: Rodrigo Delduca Date: Tue, 12 Nov 2024 13:52:36 -0300 Subject: [PATCH] Work in progress --- src/common.hpp | 8 ++++---- src/entitymanager.cpp | 42 ++++++++++++++++++++++++++++++++++-------- src/entitymanager.hpp | 5 +++-- src/margin.cpp | 38 +++++++++++++++++++++++++++++++------- src/margin.hpp | 30 +++++++++++++++++++++++++----- src/point.hpp | 7 +++++++ src/size.hpp | 7 +++++++ 7 files changed, 111 insertions(+), 26 deletions(-) diff --git a/src/common.hpp b/src/common.hpp index 346ac39..268d3a1 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -8,6 +8,10 @@ #define NOMINMAX #endif +#ifdef EMSCRIPTEN +#include +#endif + #include #include #include @@ -43,10 +47,6 @@ #include #include -#ifdef EMSCRIPTEN -#include -#endif - #include "constants.hpp" #include "deleters.hpp" #include "helpers.hpp" diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp index eed7387..05a93a6 100644 --- a/src/entitymanager.cpp +++ b/src/entitymanager.cpp @@ -13,8 +13,10 @@ using namespace framework; using json = nlohmann::json; -entitymanager::entitymanager() { - // worldDef.gravity = {0.0f, 9.8f}; +entitymanager::entitymanager() + : _space(cpSpaceNew(), &cpSpaceFree) { + const auto gravity = cpv(0, -9.8); + cpSpaceSetGravity(_space.get(), gravity); } entitymanager::~entitymanager() { @@ -29,10 +31,21 @@ std::shared_ptr entitymanager::spawn(const std::string &kind) { const auto j = json::parse(buffer); const auto spritesheet = _resourcemanager->pixmappool()->get(j["spritesheet"].get()); const auto scale = j.value("scale", 1.0); - const auto size = geometry::size{ - static_cast(j.value("width", 0) * scale), - static_cast(j.value("height", 0) * scale) - }; + + const auto position = j["position"].get(); + + // const auto position = geometry::point{ + // static_cast(j.value("x", 0) * scale), + // static_cast(j.value("y", 0) * scale), + // }; + + const auto size = j["size"].get(); + + UNUSED(size); + // const auto size = geometry::size{ + // static_cast(j.value("width", 0) * scale), + // static_cast(j.value("height", 0) * scale) + // }; std::map> animations; for (const auto &[key, frames] : j["animations"].items()) { @@ -58,12 +71,24 @@ std::shared_ptr entitymanager::spawn(const std::string &kind) { // {"dynamic", b2_dynamicBody}, // }; + std::unordered_map> cases = { + {"static", []() { + + }}, + {"dynamic", []() { + + }} + }; + const auto p = j["physics"]; const auto width = p["size"]["width"].get(); const auto height = p["size"]["height"].get(); - // const auto type = mapping[p["type"].get()]; + const auto type = p["type"].get(); const auto margin = p["margin"].get(); + cases[type](); + + UNUSED(position); UNUSED(width); UNUSED(height); UNUSED(margin); @@ -105,7 +130,8 @@ std::shared_ptr entitymanager::find(uint64_t id) const noexcept { void entitymanager::update(float_t delta) { UNUSED(delta); - // b2World_Step(_world, delta, 4); + + cpSpaceStep(_space.get(), 1.0 / 60.0); for (const auto &entity : _entities) { entity->update(); diff --git a/src/entitymanager.hpp b/src/entitymanager.hpp index 16a4346..a487c24 100644 --- a/src/entitymanager.hpp +++ b/src/entitymanager.hpp @@ -5,8 +5,7 @@ #include "eventreceiver.hpp" namespace framework { -class entity; -class resourcemanager; +using space_ptr = std::unique_ptr; class entitymanager : public input::eventreceiver { public: @@ -24,6 +23,8 @@ class entitymanager : public input::eventreceiver { virtual void on_mail(const input::mailevent &event) noexcept override; private: + space_ptr _space; + std::shared_ptr _resourcemanager; std::list> _entities; std::atomic _counter{0}; diff --git a/src/margin.cpp b/src/margin.cpp index 197a369..6dcd34b 100644 --- a/src/margin.cpp +++ b/src/margin.cpp @@ -1,11 +1,35 @@ - #include "margin.hpp" -namespace geometry { -void from_json(const nlohmann::json &j, margin &m) { - m.top = j.at("top").get(); - m.left = j.at("left").get(); - m.bottom = j.at("bottom").get(); - m.right = j.at("right").get(); +using namespace geometry; + +int32_t margin::top() const noexcept { + return _top; +} + +void margin::set_top(int32_t value) noexcept { + _top = value; +} + +int32_t margin::left() const noexcept { + return _left; +} + +void margin::set_left(int32_t value) noexcept { + _left = value; } + +int32_t margin::bottom() const noexcept { + return _bottom; +} + +void margin::set_bottom(int32_t value) noexcept { + _bottom = value; +} + +int32_t margin::right() const noexcept { + return _right; +} + +void margin::set_right(int32_t value) noexcept { + _right = value; } diff --git a/src/margin.hpp b/src/margin.hpp index 89a5a04..e792202 100644 --- a/src/margin.hpp +++ b/src/margin.hpp @@ -5,14 +5,34 @@ namespace geometry { class margin { public: - int32_t top; - int32_t left; - int32_t bottom; - int32_t right; - margin() noexcept = default; ~margin() noexcept = default; + int32_t top() const noexcept; + void set_top(int32_t value) noexcept; + + int32_t left() const noexcept; + void set_left(int32_t value) noexcept; + + int32_t bottom() const noexcept; + void set_bottom(int32_t value) noexcept; + + int32_t right() const noexcept; + void set_right(int32_t value) noexcept; + friend void from_json(const nlohmann::json &j, margin &m); + +private: + int32_t _top{0}; + int32_t _left{0}; + int32_t _bottom{0}; + int32_t _right{0}; }; + +inline void from_json(const nlohmann::json &j, margin &m) { + j.at("top").get_to(m._top); + j.at("left").get_to(m._left); + j.at("bottom").get_to(m._bottom); + j.at("right").get_to(m._right); +} } diff --git a/src/point.hpp b/src/point.hpp index 4a9ae3d..4a34a9d 100644 --- a/src/point.hpp +++ b/src/point.hpp @@ -23,8 +23,15 @@ class point { point operator+(const point &other) const noexcept; point &operator+=(const point &other) noexcept; + friend void from_json(const nlohmann::json &j, point &m) noexcept; + private: int32_t _x{0}; int32_t _y{0}; }; + +inline void from_json(const nlohmann::json &j, point &m) noexcept { + j.at("x").get_to(m._x); + j.at("y").get_to(m._y); +} } diff --git a/src/size.hpp b/src/size.hpp index b79b1b6..02e126b 100644 --- a/src/size.hpp +++ b/src/size.hpp @@ -18,8 +18,15 @@ class size { bool operator==(const size &rhs) const noexcept; bool operator!=(const size &rhs) const noexcept; + friend void from_json(const nlohmann::json &j, size &s) noexcept; + private: int32_t _width; int32_t _height; }; + +inline void from_json(const nlohmann::json &j, size &s) noexcept { + j.at("width").get_to(s._width); + j.at("height").get_to(s._height); +} }