- Sprite: nou hook virtual `collisionBox() -> SDL_FRect` (default `pos_`). Subclasses amb falsa perspectiva el sobreescriuen amb una sub-caixa més petita. - MovingSprite: nou `tryMove(dx, dy, CollisionFn)` que aplica eix a eix i reverteix l'eix que col·lisiona; deixa lliscar el sprite contra obstacles. - Player: override `collisionBox()` retornant `getBaseBox()` i `update()` reescrita per fer `animate(dt) + tryMove(vx*dt, vy*dt, collidesFn)` en lloc del patró antic `update + revertir + reaplicar`. El comportament observable és idèntic: la integració `v*dt` parteix de la mateixa posició (post-snap), els revertits per col·lisió són els mateixos i el `MOVED` que controla l'animació stand/walk segueix comparant amb `FRAME_START_X/Y`. Smoke tests 13/13 OK; log Game start → Bye! sota xvfb-run idèntic al baseline. clang-tidy i cppcheck nets sobre els fitxers modificats. Refs: docs/audit-game.md (PATCH-01). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
84 lines
2.0 KiB
C++
84 lines
2.0 KiB
C++
#include "core/rendering/sprite/moving_sprite.hpp"
|
|
|
|
#include <utility>
|
|
|
|
#include "core/rendering/surface.hpp"
|
|
|
|
MovingSprite::MovingSprite() { Sprite::clear(); }
|
|
|
|
MovingSprite::MovingSprite(std::shared_ptr<Surface> surface)
|
|
: Sprite(std::move(surface)) { Sprite::clear(); }
|
|
|
|
// pos_ ja queda inicialitzat pel ctor delegat Sprite(surface, pos); només
|
|
// cal sincronitzar la posició float x_/y_ (i el flip si ve).
|
|
MovingSprite::MovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos)
|
|
: Sprite(std::move(surface), pos),
|
|
x_(pos.x),
|
|
y_(pos.y) {}
|
|
|
|
MovingSprite::MovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos, SDL_FlipMode flip)
|
|
: Sprite(std::move(surface), pos),
|
|
x_(pos.x),
|
|
y_(pos.y),
|
|
flip_(flip) {}
|
|
|
|
void MovingSprite::clear() {
|
|
x_ = 0.0F;
|
|
y_ = 0.0F;
|
|
vx_ = 0.0F;
|
|
vy_ = 0.0F;
|
|
ax_ = 0.0F;
|
|
ay_ = 0.0F;
|
|
flip_ = SDL_FLIP_NONE;
|
|
Sprite::clear();
|
|
}
|
|
|
|
void MovingSprite::move(float delta_time) {
|
|
vx_ += ax_ * delta_time;
|
|
vy_ += ay_ * delta_time;
|
|
x_ += vx_ * delta_time;
|
|
y_ += vy_ * delta_time;
|
|
pos_.x = static_cast<int>(x_);
|
|
pos_.y = static_cast<int>(y_);
|
|
}
|
|
|
|
void MovingSprite::update(float delta_time) {
|
|
move(delta_time);
|
|
}
|
|
|
|
void MovingSprite::render() {
|
|
surface_->render(static_cast<int>(pos_.x), static_cast<int>(pos_.y), &clip_, flip_);
|
|
}
|
|
|
|
void MovingSprite::setPos(SDL_FRect rect) {
|
|
x_ = rect.x;
|
|
y_ = rect.y;
|
|
pos_ = rect;
|
|
}
|
|
|
|
void MovingSprite::setPos(float x, float y) {
|
|
x_ = x;
|
|
y_ = y;
|
|
pos_.x = static_cast<int>(x_);
|
|
pos_.y = static_cast<int>(y_);
|
|
}
|
|
|
|
void MovingSprite::setPosX(float value) {
|
|
x_ = value;
|
|
pos_.x = static_cast<int>(x_);
|
|
}
|
|
|
|
void MovingSprite::setPosY(float value) {
|
|
y_ = value;
|
|
pos_.y = static_cast<int>(y_);
|
|
}
|
|
|
|
void MovingSprite::tryMove(float dx, float dy, const CollisionFn& collides) {
|
|
const float OLD_X = getPosX();
|
|
setPosX(OLD_X + dx);
|
|
if (collides(collisionBox())) { setPosX(OLD_X); }
|
|
const float OLD_Y = getPosY();
|
|
setPosY(OLD_Y + dy);
|
|
if (collides(collisionBox())) { setPosY(OLD_Y); }
|
|
}
|