Afegit formato Allman a bullet.cpp
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#include "bullet.h"
|
#include "bullet.h"
|
||||||
#include "param.h" // for param
|
#include "param.h" // for param
|
||||||
#include "sprite.h" // for Sprite
|
#include "sprite.h" // for Sprite
|
||||||
#include <memory> // for std::unique_ptr
|
#include <memory> // for std::unique_ptr
|
||||||
|
|
||||||
// Constantes evaluables en tiempo de compilación
|
// Constantes evaluables en tiempo de compilación
|
||||||
constexpr int BULLET_WIDTH = 12;
|
constexpr int BULLET_WIDTH = 12;
|
||||||
@@ -11,12 +11,13 @@ constexpr int BULLET_VELX_LEFT = -2;
|
|||||||
constexpr int BULLET_VELX_RIGHT = 2;
|
constexpr int BULLET_VELX_RIGHT = 2;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Bullet::Bullet(int x, int y, BulletType kind, bool poweredUp, int owner, SDL_Rect* playArea, Texture* texture)
|
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),
|
: posX(x), posY(y), width(BULLET_WIDTH), height(BULLET_HEIGHT), velX(0), velY(BULLET_VELY),
|
||||||
kind(kind), owner(owner), playArea(playArea),
|
kind(kind), owner(owner), playArea(playArea),
|
||||||
sprite(std::unique_ptr<Sprite>(new Sprite(SDL_Rect{x, y, BULLET_WIDTH, BULLET_HEIGHT}, texture))) // Crear manualmente el std::unique_ptr
|
sprite(std::unique_ptr<Sprite>(new Sprite(SDL_Rect{x, y, BULLET_WIDTH, BULLET_HEIGHT}, texture))) // Crear manualmente el std::unique_ptr
|
||||||
{
|
{
|
||||||
velX = (kind == BulletType::LEFT) ? BULLET_VELX_LEFT : (kind == BulletType::RIGHT) ? BULLET_VELX_RIGHT : 0;
|
velX = (kind == BulletType::LEFT) ? BULLET_VELX_LEFT : (kind == BulletType::RIGHT) ? BULLET_VELX_RIGHT
|
||||||
|
: 0;
|
||||||
|
|
||||||
auto spriteOffset = poweredUp ? 3 : 0;
|
auto spriteOffset = poweredUp ? 3 : 0;
|
||||||
auto kindIndex = static_cast<int>(kind);
|
auto kindIndex = static_cast<int>(kind);
|
||||||
@@ -27,20 +28,24 @@ Bullet::Bullet(int x, int y, BulletType kind, bool poweredUp, int owner, SDL_Rec
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Implementación de render (llama al render del sprite)
|
// Implementación de render (llama al render del sprite)
|
||||||
void Bullet::render() {
|
void Bullet::render()
|
||||||
|
{
|
||||||
sprite->render();
|
sprite->render();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implementación del movimiento usando BulletMoveStatus
|
// Implementación del movimiento usando BulletMoveStatus
|
||||||
BulletMoveStatus Bullet::move() {
|
BulletMoveStatus Bullet::move()
|
||||||
|
{
|
||||||
posX += velX;
|
posX += velX;
|
||||||
if (posX < param.game.playArea.rect.x - width || posX > playArea->w) {
|
if (posX < param.game.playArea.rect.x - width || posX > playArea->w)
|
||||||
|
{
|
||||||
disable();
|
disable();
|
||||||
return BulletMoveStatus::OUT;
|
return BulletMoveStatus::OUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
posY += velY;
|
posY += velY;
|
||||||
if (posY < param.game.playArea.rect.y - height) {
|
if (posY < param.game.playArea.rect.y - height)
|
||||||
|
{
|
||||||
disable();
|
disable();
|
||||||
return BulletMoveStatus::OUT;
|
return BulletMoveStatus::OUT;
|
||||||
}
|
}
|
||||||
@@ -52,47 +57,58 @@ BulletMoveStatus Bullet::move() {
|
|||||||
return BulletMoveStatus::OK;
|
return BulletMoveStatus::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Bullet::isEnabled() const {
|
bool Bullet::isEnabled() const
|
||||||
|
{
|
||||||
return kind != BulletType::NULL_TYPE;
|
return kind != BulletType::NULL_TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bullet::disable() {
|
void Bullet::disable()
|
||||||
|
{
|
||||||
kind = BulletType::NULL_TYPE;
|
kind = BulletType::NULL_TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bullet::getPosX() const {
|
int Bullet::getPosX() const
|
||||||
|
{
|
||||||
return posX;
|
return posX;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bullet::getPosY() const {
|
int Bullet::getPosY() const
|
||||||
|
{
|
||||||
return posY;
|
return posY;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bullet::setPosX(int x) {
|
void Bullet::setPosX(int x)
|
||||||
|
{
|
||||||
posX = x;
|
posX = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bullet::setPosY(int y) {
|
void Bullet::setPosY(int y)
|
||||||
|
{
|
||||||
posY = y;
|
posY = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bullet::getVelY() const {
|
int Bullet::getVelY() const
|
||||||
|
{
|
||||||
return velY;
|
return velY;
|
||||||
}
|
}
|
||||||
|
|
||||||
BulletType Bullet::getKind() const {
|
BulletType Bullet::getKind() const
|
||||||
|
{
|
||||||
return kind;
|
return kind;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bullet::getOwner() const {
|
int Bullet::getOwner() const
|
||||||
|
{
|
||||||
return owner;
|
return owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
circle_t& Bullet::getCollider() {
|
circle_t &Bullet::getCollider()
|
||||||
|
{
|
||||||
return collider;
|
return collider;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bullet::shiftColliders() {
|
void Bullet::shiftColliders()
|
||||||
|
{
|
||||||
collider.x = posX + collider.r;
|
collider.x = posX + collider.r;
|
||||||
collider.y = posY + collider.r;
|
collider.y = posY + collider.r;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user