Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Oct 5, 2024
1 parent 64f0e3d commit 1bceef1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
geometry::size size{f.at("width").get<int32_t>(), f.at("height").get<int32_t>()};
geometry::rect rect{position, size};
uint64_t duration = f.at("duration").get<uint64_t>();
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);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions src/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1bceef1

Please sign in to comment.