Compare commits

..

4 Commits

6 changed files with 124 additions and 29 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -1,7 +1,7 @@
#include "bullet.h"
#include "param.h" // for param
#include "sprite.h" // for Sprite
#include <memory> // for std::unique_ptr
#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;
@@ -11,12 +11,13 @@ 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),
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),
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 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)
void Bullet::render() {
void Bullet::render()
{
sprite->render();
}
// Implementación del movimiento usando BulletMoveStatus
BulletMoveStatus Bullet::move() {
BulletMoveStatus Bullet::move()
{
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();
return BulletMoveStatus::OUT;
}
posY += velY;
if (posY < param.game.playArea.rect.y - height) {
if (posY < param.game.playArea.rect.y - height)
{
disable();
return BulletMoveStatus::OUT;
}
@@ -52,47 +57,58 @@ BulletMoveStatus Bullet::move() {
return BulletMoveStatus::OK;
}
bool Bullet::isEnabled() const {
bool Bullet::isEnabled() const
{
return kind != BulletType::NULL_TYPE;
}
void Bullet::disable() {
void Bullet::disable()
{
kind = BulletType::NULL_TYPE;
}
int Bullet::getPosX() const {
int Bullet::getPosX() const
{
return posX;
}
int Bullet::getPosY() const {
int Bullet::getPosY() const
{
return posY;
}
void Bullet::setPosX(int x) {
void Bullet::setPosX(int x)
{
posX = x;
}
void Bullet::setPosY(int y) {
void Bullet::setPosY(int y)
{
posY = y;
}
int Bullet::getVelY() const {
int Bullet::getVelY() const
{
return velY;
}
BulletType Bullet::getKind() const {
BulletType Bullet::getKind() const
{
return kind;
}
int Bullet::getOwner() const {
int Bullet::getOwner() const
{
return owner;
}
circle_t& Bullet::getCollider() {
circle_t &Bullet::getCollider()
{
return collider;
}
void Bullet::shiftColliders() {
void Bullet::shiftColliders()
{
collider.x = posX + collider.r;
collider.y = posY + collider.r;
}

View File

@@ -26,6 +26,7 @@
#include "jail_audio.h" // for JA_DeleteMusic, JA_DeleteSound
#include "logo.h" // for Logo
#include "manage_hiscore_table.h" // for ManageHiScoreTable
#include "on_screen_help.h" // for OnScreenHelp
#include "options.h" // for options, loadOptionsFile, saveO...
#include "param.h" // for param, loadParamsFromFile
#include "screen.h" // for Screen
@@ -34,7 +35,7 @@
#include "utils.h" // for music_file_t, sound_file_t, opt...
#ifndef _WIN32
#include <pwd.h> // for getpwuid, passwd
#include <pwd.h> // for getpwuid, passwd
#endif
// Constructor
@@ -101,6 +102,8 @@ Director::Director(int argc, char *argv[])
Screen::init(window, renderer);
screen = Screen::get();
OnScreenHelp::init();
// Carga los sonidos del juego
loadSounds();
@@ -115,6 +118,7 @@ Director::~Director()
Asset::destroy();
Input::destroy();
Screen::destroy();
OnScreenHelp::destroy();
deleteSounds();
deleteMusics();

View File

@@ -789,7 +789,7 @@ void Game::deployEnemyFormation()
createPowerBall();
// Da un poco de margen para que se creen mas enemigos
enemyDeployCounter = 50;
enemyDeployCounter = 300;
}
else
{
@@ -2642,11 +2642,6 @@ void Game::checkEvents()
{
switch (eventHandler->key.keysym.sym)
{
// CREA UN SPRITE DE 1000 PUNTOS
case SDLK_h:
createItemScoreSprite(param.game.width / 2, param.game.width / 2, n1000Sprite);
break;
// CREA UNA POWERBALL
case SDLK_1:
createPowerBall();

32
source/on_screen_help.cpp Normal file
View File

@@ -0,0 +1,32 @@
#include "on_screen_help.h"
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
OnScreenHelp *OnScreenHelp::onScreenHelp = nullptr;
// [SINGLETON] Crearemos el objeto onScreenHelp con esta función estática
void OnScreenHelp::init()
{
OnScreenHelp::onScreenHelp = new OnScreenHelp();
}
// [SINGLETON] Destruiremos el objeto onScreenHelp con esta función estática
void OnScreenHelp::destroy()
{
delete OnScreenHelp::onScreenHelp;
}
// [SINGLETON] Con este método obtenemos el objeto onScreenHelp y podemos trabajar con él
OnScreenHelp *OnScreenHelp::get()
{
return OnScreenHelp::onScreenHelp;
}
// Constructor
OnScreenHelp::OnScreenHelp()
{
}
// Destructor
OnScreenHelp::~OnScreenHelp()
{
}

48
source/on_screen_help.h Normal file
View File

@@ -0,0 +1,48 @@
#pragma once
#include <SDL2/SDL.h>
class Screen;
class Asset;
class Input;
class Texture;
class Sprite;
enum class OnScreenHelpStatus
{
hidden,
showing,
entering,
exitting,
};
// Classe on_screen_help
class OnScreenHelp
{
private:
// [SINGLETON] Objeto screen privado para Don Melitón
static OnScreenHelp *onScreenHelp;
Screen *screen; // Objeto encargado de dibujar en pantalla
Asset *asset; // Objeto con los ficheros de recursos
Input *input; // Objeto pata gestionar la entrada
SDL_Texture *texture; // Textura donde dibujar
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera
// Constructor
OnScreenHelp();
// Destructor
~OnScreenHelp();
public:
// [SINGLETON] Crearemos el objeto screen con esta función estática
static void init();
// [SINGLETON] Destruiremos el objeto screen con esta función estática
static void destroy();
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
static OnScreenHelp *get();
};