diff --git a/src/entity.cpp b/src/entity.cpp index 3882333..f6057d7 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -72,9 +72,13 @@ void entity::draw() const noexcept { } const auto source = _props.animations.at(_props.action)[_props.frame].frame; - geometry::rect destination{_props.position, source.size()}; + const auto offset = _props.animations.at(_props.action)[_props.frame].offset; + geometry::rect destination{_props.position + offset, source.size()}; destination.scale(_props.scale); + if (_props.action == "dead") { + std::cout << ">>> offset x" << offset.x() << std::endl; + } _props.spritesheet->draw( source, destination, diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp index bf544f0..54f368a 100644 --- a/src/entitymanager.cpp +++ b/src/entitymanager.cpp @@ -41,8 +41,13 @@ std::shared_ptr entitymanager::spawn(const std::string &kind) { geometry::size size{f.at("width").get(), f.at("height").get()}; geometry::rect rect{position, size}; uint64_t duration = f.at("duration").get(); + geometry::point offset{0, 0}; + const auto o = f.value("offset", json::object()); + if (o.contains("x") || o.contains("y")) { + offset = geometry::point{o.value("x", 0), o.value("y", 0)}; + } - keyframes.emplace_back(rect, duration); + keyframes.emplace_back(rect, duration, offset); } } diff --git a/src/entityprops.hpp b/src/entityprops.hpp index 0e59202..7a1b004 100644 --- a/src/entityprops.hpp +++ b/src/entityprops.hpp @@ -11,9 +11,11 @@ namespace framework { struct keyframe { geometry::rect frame; uint64_t duration; + geometry::point offset; keyframe() = default; - keyframe(const geometry::rect &rect, uint64_t duration) : frame(rect), duration(duration) {} + keyframe(const geometry::rect &rect, uint64_t duration, const geometry::point &offset) + : frame(rect), duration(duration), offset(offset) {} }; struct entityprops { diff --git a/src/point.cpp b/src/point.cpp index 05d2971..54cba59 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -20,3 +20,7 @@ int32_t point::y() const noexcept { return _y; } void point::set_y(int32_t y) noexcept { _y = y; } point::operator SDL_Point() const noexcept { return SDL_Point{_x, _y}; } + +point point::operator+(const point &other) const noexcept { + return point(_x + other._x, _y + other._y); +} diff --git a/src/point.hpp b/src/point.hpp index a0ea7aa..2d6b144 100644 --- a/src/point.hpp +++ b/src/point.hpp @@ -20,6 +20,8 @@ class point { operator SDL_Point() const noexcept; + point operator+(const point &other) const noexcept; + private: int32_t _x; int32_t _y;