#include "core/rendering/sprite/moving_sprite.hpp" #include #include "core/rendering/surface.hpp" MovingSprite::MovingSprite() { Sprite::clear(); } MovingSprite::MovingSprite(std::shared_ptr 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, SDL_FRect pos) : Sprite(std::move(surface), pos), x_(pos.x), y_(pos.y) {} MovingSprite::MovingSprite(std::shared_ptr 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(x_); pos_.y = static_cast(y_); } void MovingSprite::update(float delta_time) { move(delta_time); } void MovingSprite::render() { surface_->render(static_cast(pos_.x), static_cast(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(x_); pos_.y = static_cast(y_); } void MovingSprite::setPosX(float value) { x_ = value; pos_.x = static_cast(x_); } void MovingSprite::setPosY(float value) { y_ = value; pos_.y = static_cast(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); } }