Mes autos, const i constexpr perl codi Ara la classe Screen es un poc pitjor
114 lines
2.3 KiB
C++
114 lines
2.3 KiB
C++
#include "bullet.h"
|
|
#include "param.h" // for param
|
|
#include "sprite.h" // for Sprite
|
|
#include <memory> // for std::unique_ptr
|
|
|
|
// Constantes evaluables en tiempo de compilación
|
|
constexpr int BULLET_WIDTH = 12;
|
|
constexpr int BULLET_HEIGHT = 12;
|
|
constexpr int BULLET_VELY = -3;
|
|
constexpr int BULLET_VELX_LEFT = -2;
|
|
constexpr int BULLET_VELX_RIGHT = 2;
|
|
|
|
// Constructor
|
|
Bullet::Bullet(int x, int y, BulletType kind, bool poweredUp, int owner, SDL_Rect *playArea, Texture *texture)
|
|
: posX(x), posY(y), width(BULLET_WIDTH), height(BULLET_HEIGHT), velX(0), velY(BULLET_VELY),
|
|
kind(kind), owner(owner), playArea(playArea)
|
|
{
|
|
velX = (kind == BulletType::LEFT) ? BULLET_VELX_LEFT : (kind == BulletType::RIGHT) ? BULLET_VELX_RIGHT : 0;
|
|
|
|
auto spriteOffset = poweredUp ? 3 : 0;
|
|
auto kindIndex = static_cast<int>(kind);
|
|
sprite = std::make_unique<Sprite>(SDL_Rect{x, y, BULLET_WIDTH, BULLET_HEIGHT}, texture);
|
|
sprite->setSpriteClip((kindIndex + spriteOffset) * width, 0, sprite->getWidth(), sprite->getHeight());
|
|
|
|
collider.r = width / 2;
|
|
shiftColliders();
|
|
}
|
|
|
|
// Implementación de render (llama al render del sprite)
|
|
void Bullet::render()
|
|
{
|
|
sprite->render();
|
|
}
|
|
|
|
// Implementación del movimiento usando BulletMoveStatus
|
|
BulletMoveStatus Bullet::move()
|
|
{
|
|
posX += velX;
|
|
if (posX < param.game.playArea.rect.x - width || posX > playArea->w)
|
|
{
|
|
disable();
|
|
return BulletMoveStatus::OUT;
|
|
}
|
|
|
|
posY += velY;
|
|
if (posY < param.game.playArea.rect.y - height)
|
|
{
|
|
disable();
|
|
return BulletMoveStatus::OUT;
|
|
}
|
|
|
|
sprite->setPosX(posX);
|
|
sprite->setPosY(posY);
|
|
shiftColliders();
|
|
|
|
return BulletMoveStatus::OK;
|
|
}
|
|
|
|
bool Bullet::isEnabled() const
|
|
{
|
|
return kind != BulletType::NULL_TYPE;
|
|
}
|
|
|
|
void Bullet::disable()
|
|
{
|
|
kind = BulletType::NULL_TYPE;
|
|
}
|
|
|
|
int Bullet::getPosX() const
|
|
{
|
|
return posX;
|
|
}
|
|
|
|
int Bullet::getPosY() const
|
|
{
|
|
return posY;
|
|
}
|
|
|
|
void Bullet::setPosX(int x)
|
|
{
|
|
posX = x;
|
|
}
|
|
|
|
void Bullet::setPosY(int y)
|
|
{
|
|
posY = y;
|
|
}
|
|
|
|
int Bullet::getVelY() const
|
|
{
|
|
return velY;
|
|
}
|
|
|
|
BulletType Bullet::getKind() const
|
|
{
|
|
return kind;
|
|
}
|
|
|
|
int Bullet::getOwner() const
|
|
{
|
|
return owner;
|
|
}
|
|
|
|
circle_t &Bullet::getCollider()
|
|
{
|
|
return collider;
|
|
}
|
|
|
|
void Bullet::shiftColliders()
|
|
{
|
|
collider.x = posX + collider.r;
|
|
collider.y = posY + collider.r;
|
|
}
|