#include "game/entities/bullet.h" #include "core/rendering/sprite.h" // for Sprite #include "game/defaults.hpp" // for PLAY_AREA_LEFT, PLAY_AREA_RIGHT, PLAY_A... class Texture; // Constructor Bullet::Bullet(int x, int y, Bullet::Kind kind, bool powered_up, int owner, Texture *texture, SDL_Renderer *renderer) { sprite_ = new Sprite({x, y, 10, 10}, texture, renderer); // Posición inicial del objeto pos_x_ = x; pos_y_ = y; pos_x_f_ = static_cast(x); pos_y_f_ = static_cast(y); // Alto y ancho del objeto width_ = 10; height_ = 10; // Velocidad inicial en el eje Y vel_y_ = -3; vel_y_s_ = VEL_Y_PX_PER_S; // Tipo de bala this->kind_ = kind; // Identificador del dueño del objeto this->owner_ = owner; // Valores especificos según el tipo switch (kind) { case Bullet::Kind::UP: // Establece la velocidad inicial vel_x_ = 0; vel_x_s_ = 0.0F; // Rectangulo con los gráficos del objeto if (!powered_up) { sprite_->setSpriteClip(0 * width_, 0, sprite_->getWidth(), sprite_->getHeight()); } else { sprite_->setSpriteClip((0 + 3) * width_, 0, sprite_->getWidth(), sprite_->getHeight()); } break; case Bullet::Kind::LEFT: // Establece la velocidad inicial vel_x_ = -2; vel_x_s_ = VEL_X_LEFT_PX_PER_S; // Rectangulo con los gráficos del objeto if (!powered_up) { sprite_->setSpriteClip(1 * width_, 0, sprite_->getWidth(), sprite_->getHeight()); } else { sprite_->setSpriteClip((1 + 3) * width_, 0, sprite_->getWidth(), sprite_->getHeight()); } break; case Bullet::Kind::RIGHT: // Establece la velocidad inicial vel_x_ = 2; vel_x_s_ = VEL_X_RIGHT_PX_PER_S; // Rectangulo con los gráficos del objeto if (!powered_up) { sprite_->setSpriteClip(2 * width_, 0, sprite_->getWidth(), sprite_->getHeight()); } else { sprite_->setSpriteClip((2 + 3) * width_, 0, sprite_->getWidth(), sprite_->getHeight()); } break; default: break; } // Establece el tamaño del circulo de colisión collider_.r = width_ / 2; // Alinea el circulo de colisión con el objeto shiftColliders(); } // Destructor Bullet::~Bullet() { delete sprite_; } // Pinta el objeto en pantalla void Bullet::render() { sprite_->render(); } // Actualiza la posición y estado del objeto en horizontal auto Bullet::move() -> MoveResult { // Variable con el valor de retorno MoveResult msg = MoveResult::OK; // Mueve el objeto a su nueva posición pos_x_ += vel_x_; // Si el objeto se sale del area de juego por los laterales if ((pos_x_ < PLAY_AREA_LEFT - width_) || (pos_x_ > PLAY_AREA_RIGHT)) { // Se deshabilita kind_ = Bullet::Kind::NONE; // Mensaje de salida msg = MoveResult::OUT; } // Mueve el objeto a su nueva posición en vertical pos_y_ += vel_y_; // Si el objeto se sale del area de juego por la parte superior if (pos_y_ < PLAY_AREA_TOP - height_) { // Se deshabilita kind_ = Bullet::Kind::NONE; // Mensaje de salida msg = MoveResult::OUT; } // Actualiza la posición del sprite sprite_->setPosX(pos_x_); sprite_->setPosY(pos_y_); // Alinea el circulo de colisión con el objeto shiftColliders(); return msg; } // Actualiza la posición y estado del objeto (time-based) auto Bullet::move(float dt_s) -> MoveResult { MoveResult msg = MoveResult::OK; pos_x_f_ += vel_x_s_ * dt_s; pos_x_ = static_cast(pos_x_f_); if ((pos_x_ < PLAY_AREA_LEFT - width_) || (pos_x_ > PLAY_AREA_RIGHT)) { kind_ = Bullet::Kind::NONE; msg = MoveResult::OUT; } pos_y_f_ += vel_y_s_ * dt_s; pos_y_ = static_cast(pos_y_f_); if (pos_y_ < PLAY_AREA_TOP - height_) { kind_ = Bullet::Kind::NONE; msg = MoveResult::OUT; } sprite_->setPosX(pos_x_); sprite_->setPosY(pos_y_); shiftColliders(); return msg; } // Comprueba si el objeto está habilitado auto Bullet::isEnabled() const -> bool { return kind_ != Bullet::Kind::NONE; } // Deshabilita el objeto void Bullet::disable() { kind_ = Bullet::Kind::NONE; } // Obtiene el valor de la variable auto Bullet::getPosX() const -> int { return pos_x_; } // Obtiene el valor de la variable auto Bullet::getPosY() const -> int { return pos_y_; } // Establece el valor de la variable void Bullet::setPosX(int x) { pos_x_ = x; pos_x_f_ = static_cast(x); } // Establece el valor de la variable void Bullet::setPosY(int y) { pos_y_ = y; pos_y_f_ = static_cast(y); } // Obtiene el valor de la variable auto Bullet::getVelY() const -> int { return vel_y_; } // Obtiene el valor de la variable auto Bullet::getKind() const -> Bullet::Kind { return kind_; } // Obtiene el valor de la variable auto Bullet::getOwner() const -> int { return owner_; } // Obtiene el circulo de colisión auto Bullet::getCollider() -> Circle & { return collider_; } // Alinea el circulo de colisión con el objeto void Bullet::shiftColliders() { collider_.x = pos_x_ + collider_.r; collider_.y = pos_y_ + collider_.r; }