Tratando de eliminar las variables globales
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#include "gamedirector.h"
|
#include "gamedirector.h"
|
||||||
|
|
||||||
//Calcula el cuadrado de la distancia entre dos puntos
|
// Calcula el cuadrado de la distancia entre dos puntos
|
||||||
double distanceSquared(int x1, int y1, int x2, int y2)
|
double distanceSquared(int x1, int y1, int x2, int y2)
|
||||||
{
|
{
|
||||||
int deltaX = x2 - x1;
|
int deltaX = x2 - x1;
|
||||||
@@ -8,34 +8,99 @@ double distanceSquared(int x1, int y1, int x2, int y2)
|
|||||||
return deltaX * deltaX + deltaY * deltaY;
|
return deltaX * deltaX + deltaY * deltaY;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Detector de colisiones entre dos circulos
|
// Detector de colisiones entre dos circulos
|
||||||
bool checkCollision(Circle &a, Circle &b)
|
bool checkCollision(Circle &a, Circle &b)
|
||||||
{
|
{
|
||||||
//Calcula el radio total al cuadrado
|
// Calcula el radio total al cuadrado
|
||||||
int totalRadiusSquared = a.r + b.r;
|
int totalRadiusSquared = a.r + b.r;
|
||||||
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
|
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
|
||||||
|
|
||||||
//Si la distancia entre el centro de los circulos es inferior a la suma de sus radios
|
// Si la distancia entre el centro de los circulos es inferior a la suma de sus radios
|
||||||
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared))
|
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared))
|
||||||
{
|
{
|
||||||
//Los circulos han colisionado
|
// Los circulos han colisionado
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//En caso contrario
|
// En caso contrario
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Constructor
|
// Constructor
|
||||||
GameDirector::GameDirector()
|
GameDirector::GameDirector()
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Iniciador
|
// Destructor
|
||||||
|
GameDirector::~GameDirector()
|
||||||
|
{
|
||||||
|
// Libera los efectos de sonido
|
||||||
|
Mix_FreeChunk(gPopBalloonFX);
|
||||||
|
Mix_FreeChunk(gBulletFX);
|
||||||
|
gPopBalloonFX = NULL;
|
||||||
|
gBulletFX = NULL;
|
||||||
|
|
||||||
|
// Libra la música
|
||||||
|
Mix_FreeMusic(gTitleMusic);
|
||||||
|
gTitleMusic = NULL;
|
||||||
|
Mix_FreeMusic(gPlayingMusic);
|
||||||
|
gPlayingMusic = NULL;
|
||||||
|
|
||||||
|
// Libera el mando
|
||||||
|
SDL_JoystickClose(gGameController);
|
||||||
|
gGameController = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iniciador
|
||||||
void GameDirector::init()
|
void GameDirector::init()
|
||||||
{
|
{
|
||||||
//Variables
|
// Carga la música del titulo
|
||||||
|
gTitleMusic = Mix_LoadMUS("media/music/title.ogg");
|
||||||
|
if (gTitleMusic == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to load title music! SDL_mixer Error: %s\n", Mix_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga la música del juego
|
||||||
|
gPlayingMusic = Mix_LoadMUS("media/music/playing.ogg");
|
||||||
|
if (gPlayingMusic == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to load playing music! SDL_mixer Error: %s\n", Mix_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga los efectos de sonido para la explosión de los globos
|
||||||
|
gPopBalloonFX = Mix_LoadWAV("media/sound/balloon.wav");
|
||||||
|
if (gPopBalloonFX == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to load balloon sound effect! SDL_mixer Error: %s\n", Mix_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga los efectos de sonido para los disparos del jugador
|
||||||
|
gBulletFX = Mix_LoadWAV("media/sound/bullet.wav");
|
||||||
|
if (gBulletFX == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to load bullet sound effect! SDL_mixer Error: %s\n", Mix_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba los mandos
|
||||||
|
if (SDL_NumJoysticks() < 1)
|
||||||
|
{
|
||||||
|
printf("Warning: No joysticks connected!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Carga el mando
|
||||||
|
gGameController = SDL_JoystickOpen(0);
|
||||||
|
if (gGameController == NULL)
|
||||||
|
{
|
||||||
|
printf("Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
|
||||||
|
}
|
||||||
|
printf("%i joysticks were found.\n", SDL_NumJoysticks());
|
||||||
|
std::cout << SDL_JoystickNumButtons(gGameController) << " buttons\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Variables
|
||||||
mGameStatus = GAME_STATE_TITLE;
|
mGameStatus = GAME_STATE_TITLE;
|
||||||
mOldTicks = 0;
|
mOldTicks = 0;
|
||||||
mMaxBalloons = 50;
|
mMaxBalloons = 50;
|
||||||
@@ -49,22 +114,22 @@ void GameDirector::init()
|
|||||||
mHiScoreText = std::to_string(mHiScore);
|
mHiScoreText = std::to_string(mHiScore);
|
||||||
mGetReady = true;
|
mGetReady = true;
|
||||||
|
|
||||||
//Objeto jugador
|
// Objeto jugador
|
||||||
player.init();
|
player.init();
|
||||||
|
|
||||||
//Establece a cero todos los valores del vector de objetos globo
|
// Establece a cero todos los valores del vector de objetos globo
|
||||||
resetBalloons();
|
resetBalloons();
|
||||||
|
|
||||||
//Crea dos objetos globo y los centra en el area de juego
|
// Crea dos objetos globo y los centra en el area de juego
|
||||||
//balloon[0].init(0, BLOCK, BALLOON_4, BALLON_VELX_POSITIVE, 0);
|
// balloon[0].init(0, BLOCK, BALLOON_4, BALLON_VELX_POSITIVE, 0);
|
||||||
//balloon[0].allignTo(PLAY_AREA_WIDTH / 2);
|
// balloon[0].allignTo(PLAY_AREA_WIDTH / 2);
|
||||||
//balloon[1].init(0, BLOCK, BALLOON_4, BALLON_VELX_NEGATIVE, 0);
|
// balloon[1].init(0, BLOCK, BALLOON_4, BALLON_VELX_NEGATIVE, 0);
|
||||||
//balloon[1].allignTo(PLAY_AREA_WIDTH / 2);
|
// balloon[1].allignTo(PLAY_AREA_WIDTH / 2);
|
||||||
|
|
||||||
//Con los globos creados, calcula el nivel de amenaza
|
// Con los globos creados, calcula el nivel de amenaza
|
||||||
calculateMenaceLevel();
|
calculateMenaceLevel();
|
||||||
|
|
||||||
//Establece a cero todos los valores del vector de objetos bala
|
// Establece a cero todos los valores del vector de objetos bala
|
||||||
resetBullets();
|
resetBullets();
|
||||||
|
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
@@ -73,22 +138,22 @@ void GameDirector::init()
|
|||||||
bulletTest.init(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2, BULLET_UP);
|
bulletTest.init(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2, BULLET_UP);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//Los fondos
|
// Los fondos
|
||||||
gameBackground.init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - (0 * BLOCK), &gGameBackgroundTexture);
|
gameBackground.init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - (0 * BLOCK), &gGameBackgroundTexture);
|
||||||
titleBackground.init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, &gTitleBackgroundTexture);
|
titleBackground.init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, &gTitleBackgroundTexture);
|
||||||
|
|
||||||
//Objetos texto, uno de cada color
|
// Objetos texto, uno de cada color
|
||||||
whiteText.init(&gWhiteFontTexture);
|
whiteText.init(&gWhiteFontTexture);
|
||||||
blackText.init(&gBlackFontTexture);
|
blackText.init(&gBlackFontTexture);
|
||||||
|
|
||||||
//Inicializa el objeto con el menu del titulo
|
// Inicializa el objeto con el menu del titulo
|
||||||
menuTitle.init(0, 16 * BLOCK, MENU_SELECTOR_WHITE, MENU_BACKGROUND_TRANSPARENT);
|
menuTitle.init(0, 16 * BLOCK, MENU_SELECTOR_WHITE, MENU_BACKGROUND_TRANSPARENT);
|
||||||
menuTitle.addItem("START");
|
menuTitle.addItem("START");
|
||||||
menuTitle.addItem("EXIT");
|
menuTitle.addItem("EXIT");
|
||||||
menuTitle.setBackgroundColor(0, 0, 0, 255);
|
menuTitle.setBackgroundColor(0, 0, 0, 255);
|
||||||
menuTitle.centerMenuOnScreen();
|
menuTitle.centerMenuOnScreen();
|
||||||
|
|
||||||
//Inicializa el objeto con el menu de pausa
|
// Inicializa el objeto con el menu de pausa
|
||||||
menuPause.init(0, 12 * BLOCK, MENU_SELECTOR_WHITE, MENU_BACKGROUND_SOLID);
|
menuPause.init(0, 12 * BLOCK, MENU_SELECTOR_WHITE, MENU_BACKGROUND_SOLID);
|
||||||
menuPause.addItem("CONTINUE");
|
menuPause.addItem("CONTINUE");
|
||||||
menuPause.addItem("EXIT TO TITLE");
|
menuPause.addItem("EXIT TO TITLE");
|
||||||
@@ -96,7 +161,7 @@ void GameDirector::init()
|
|||||||
menuPause.centerMenuOnScreen();
|
menuPause.centerMenuOnScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Hace una pausa de milisegundos
|
// Hace una pausa de milisegundos
|
||||||
void GameDirector::sleep(Uint16 time)
|
void GameDirector::sleep(Uint16 time)
|
||||||
{
|
{
|
||||||
Uint32 ticks = SDL_GetTicks();
|
Uint32 ticks = SDL_GetTicks();
|
||||||
@@ -106,19 +171,19 @@ void GameDirector::sleep(Uint16 time)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void GameDirector::setScore(Uint32 score)
|
void GameDirector::setScore(Uint32 score)
|
||||||
{
|
{
|
||||||
mScore = score;
|
mScore = score;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void GameDirector::setHiScore(Uint32 score)
|
void GameDirector::setHiScore(Uint32 score)
|
||||||
{
|
{
|
||||||
mHiScore = score;
|
mHiScore = score;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Actualiza el valor de HiScore en caso necesario
|
// Actualiza el valor de HiScore en caso necesario
|
||||||
void GameDirector::updateHiScore()
|
void GameDirector::updateHiScore()
|
||||||
{
|
{
|
||||||
if (mScore > mHiScore)
|
if (mScore > mHiScore)
|
||||||
@@ -127,7 +192,7 @@ void GameDirector::updateHiScore()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Transforma un valor numérico en una cadena de 6 cifras
|
// Transforma un valor numérico en una cadena de 6 cifras
|
||||||
std::string GameDirector::updateScoreText(Uint32 num)
|
std::string GameDirector::updateScoreText(Uint32 num)
|
||||||
{
|
{
|
||||||
switch (num)
|
switch (num)
|
||||||
@@ -161,7 +226,7 @@ std::string GameDirector::updateScoreText(Uint32 num)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pinta el marcador en pantalla usando un objeto texto
|
// Pinta el marcador en pantalla usando un objeto texto
|
||||||
void GameDirector::renderScoreBoard(Text &text)
|
void GameDirector::renderScoreBoard(Text &text)
|
||||||
{
|
{
|
||||||
mScoreText = updateScoreText(mScore);
|
mScoreText = updateScoreText(mScore);
|
||||||
@@ -172,7 +237,7 @@ void GameDirector::renderScoreBoard(Text &text)
|
|||||||
text.write(HISCORE_NUMBER_X, HISCORE_NUMBER_Y, mHiScoreText);
|
text.write(HISCORE_NUMBER_X, HISCORE_NUMBER_Y, mHiScoreText);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Mueve todos los globos activos
|
// Mueve todos los globos activos
|
||||||
void GameDirector::moveBalloons()
|
void GameDirector::moveBalloons()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
||||||
@@ -184,7 +249,7 @@ void GameDirector::moveBalloons()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pinta en pantalla todos los globos activos
|
// Pinta en pantalla todos los globos activos
|
||||||
void GameDirector::renderBalloons()
|
void GameDirector::renderBalloons()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
||||||
@@ -196,7 +261,7 @@ void GameDirector::renderBalloons()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Devuelve el primer indice no activo del vector de globos
|
// Devuelve el primer indice no activo del vector de globos
|
||||||
Uint8 GameDirector::getBallonFreeIndex()
|
Uint8 GameDirector::getBallonFreeIndex()
|
||||||
{
|
{
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -213,7 +278,7 @@ Uint8 GameDirector::getBallonFreeIndex()
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Crea un globo nuevo en el vector de globos
|
// Crea un globo nuevo en el vector de globos
|
||||||
Uint8 GameDirector::createNewBalloon(int x, int y, Uint8 kind, float velx, Uint16 creationtimer)
|
Uint8 GameDirector::createNewBalloon(int x, int y, Uint8 kind, float velx, Uint16 creationtimer)
|
||||||
{
|
{
|
||||||
Uint8 index = getBallonFreeIndex();
|
Uint8 index = getBallonFreeIndex();
|
||||||
@@ -221,7 +286,7 @@ Uint8 GameDirector::createNewBalloon(int x, int y, Uint8 kind, float velx, Uint1
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Establece a cero todos los valores del vector de objetos globo
|
// Establece a cero todos los valores del vector de objetos globo
|
||||||
void GameDirector::resetBalloons()
|
void GameDirector::resetBalloons()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
||||||
@@ -230,7 +295,7 @@ void GameDirector::resetBalloons()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Explosiona un globo. Lo destruye y crea otros dos si es el caso
|
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
|
||||||
void GameDirector::popBalloon(Uint8 index)
|
void GameDirector::popBalloon(Uint8 index)
|
||||||
{
|
{
|
||||||
if (balloon[index].isActive())
|
if (balloon[index].isActive())
|
||||||
@@ -239,12 +304,12 @@ void GameDirector::popBalloon(Uint8 index)
|
|||||||
Uint8 freeIndex = 0;
|
Uint8 freeIndex = 0;
|
||||||
switch (kind)
|
switch (kind)
|
||||||
{
|
{
|
||||||
//Si es del tipo más pequeño, simplemente elimina el globo
|
// Si es del tipo más pequeño, simplemente elimina el globo
|
||||||
case BALLOON_1:
|
case BALLOON_1:
|
||||||
balloon[index].erase();
|
balloon[index].erase();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//En cualquier otro caso, crea dos globos de un tipo inferior
|
// En cualquier otro caso, crea dos globos de un tipo inferior
|
||||||
default:
|
default:
|
||||||
freeIndex = getBallonFreeIndex();
|
freeIndex = getBallonFreeIndex();
|
||||||
balloon[freeIndex].init(0, balloon[index].getPosY(), balloon[index].getKind() - 1, BALLON_VELX_NEGATIVE, 0);
|
balloon[freeIndex].init(0, balloon[index].getPosY(), balloon[index].getKind() - 1, BALLON_VELX_NEGATIVE, 0);
|
||||||
@@ -256,14 +321,14 @@ void GameDirector::popBalloon(Uint8 index)
|
|||||||
balloon[freeIndex].allignTo(balloon[index].getPosX() + (balloon[index].getWidth() / 2));
|
balloon[freeIndex].allignTo(balloon[index].getPosX() + (balloon[index].getWidth() / 2));
|
||||||
balloon[freeIndex].setVelY(-2.5);
|
balloon[freeIndex].setVelY(-2.5);
|
||||||
|
|
||||||
//Elimina el globo
|
// Elimina el globo
|
||||||
balloon[index].erase();
|
balloon[index].erase();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Detiene todos los globos
|
// Detiene todos los globos
|
||||||
void GameDirector::stopAllBalloons()
|
void GameDirector::stopAllBalloons()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
||||||
@@ -275,7 +340,7 @@ void GameDirector::stopAllBalloons()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pone en marcha todos los globos
|
// Pone en marcha todos los globos
|
||||||
void GameDirector::startAllBalloons()
|
void GameDirector::startAllBalloons()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
||||||
@@ -287,7 +352,7 @@ void GameDirector::startAllBalloons()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Obtiene el numero de globos activos
|
// Obtiene el numero de globos activos
|
||||||
Uint8 GameDirector::countBalloons()
|
Uint8 GameDirector::countBalloons()
|
||||||
{
|
{
|
||||||
Uint8 num = 0;
|
Uint8 num = 0;
|
||||||
@@ -301,7 +366,7 @@ Uint8 GameDirector::countBalloons()
|
|||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Comprueba la colisión entre el jugador y los globos activos
|
// Comprueba la colisión entre el jugador y los globos activos
|
||||||
bool GameDirector::checkPlayerBallonCollision()
|
bool GameDirector::checkPlayerBallonCollision()
|
||||||
{
|
{
|
||||||
bool result = false;
|
bool result = false;
|
||||||
@@ -319,7 +384,7 @@ bool GameDirector::checkPlayerBallonCollision()
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Comprueba y procesa la colisión entre las balas y los globos
|
// Comprueba y procesa la colisión entre las balas y los globos
|
||||||
void GameDirector::processBulletBallonCollision()
|
void GameDirector::processBulletBallonCollision()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
for (Uint8 i = 0; i < mMaxBalloons; i++)
|
||||||
@@ -344,7 +409,7 @@ void GameDirector::processBulletBallonCollision()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Mueve las balas activas
|
// Mueve las balas activas
|
||||||
void GameDirector::moveBullets()
|
void GameDirector::moveBullets()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBullets; i++)
|
for (Uint8 i = 0; i < mMaxBullets; i++)
|
||||||
@@ -356,7 +421,7 @@ void GameDirector::moveBullets()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pinta las balas activas
|
// Pinta las balas activas
|
||||||
void GameDirector::renderBullets()
|
void GameDirector::renderBullets()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBullets; i++)
|
for (Uint8 i = 0; i < mMaxBullets; i++)
|
||||||
@@ -368,7 +433,7 @@ void GameDirector::renderBullets()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Devuelve el primer indice no activo del vector de balas
|
// Devuelve el primer indice no activo del vector de balas
|
||||||
Uint8 GameDirector::getBulletFreeIndex()
|
Uint8 GameDirector::getBulletFreeIndex()
|
||||||
{
|
{
|
||||||
Uint8 index = 0;
|
Uint8 index = 0;
|
||||||
@@ -385,7 +450,7 @@ Uint8 GameDirector::getBulletFreeIndex()
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Establece a cero todos los valores del vector de objetos bala
|
// Establece a cero todos los valores del vector de objetos bala
|
||||||
void GameDirector::resetBullets()
|
void GameDirector::resetBullets()
|
||||||
{
|
{
|
||||||
for (Uint8 i = 0; i < mMaxBullets; i++)
|
for (Uint8 i = 0; i < mMaxBullets; i++)
|
||||||
@@ -394,13 +459,13 @@ void GameDirector::resetBullets()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Crea un objeto bala
|
// Crea un objeto bala
|
||||||
void GameDirector::createBullet(int x, int y, Uint8 kind)
|
void GameDirector::createBullet(int x, int y, Uint8 kind)
|
||||||
{
|
{
|
||||||
bullet[getBulletFreeIndex()].init(x, y, kind);
|
bullet[getBulletFreeIndex()].init(x, y, kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Calcula y establece el valor de amenaza en funcion de los globos activos
|
// Calcula y establece el valor de amenaza en funcion de los globos activos
|
||||||
void GameDirector::calculateMenaceLevel()
|
void GameDirector::calculateMenaceLevel()
|
||||||
{
|
{
|
||||||
mMenaceLevel = 0;
|
mMenaceLevel = 0;
|
||||||
@@ -431,27 +496,27 @@ void GameDirector::calculateMenaceLevel()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Obtiene el valor de la variable
|
// Obtiene el valor de la variable
|
||||||
Uint8 GameDirector::getMenaceLevel()
|
Uint8 GameDirector::getMenaceLevel()
|
||||||
{
|
{
|
||||||
return mMenaceLevel;
|
return mMenaceLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Gestiona el nivel de amenaza
|
// Gestiona el nivel de amenaza
|
||||||
void GameDirector::checkMenaceLevel()
|
void GameDirector::checkMenaceLevel()
|
||||||
{
|
{
|
||||||
//Aumenta el nivel de amenaza en función de la puntuación
|
// Aumenta el nivel de amenaza en función de la puntuación
|
||||||
mMenaceLevelThreshold = 7 + (4 * (mScore / 10000));
|
mMenaceLevelThreshold = 7 + (4 * (mScore / 10000));
|
||||||
|
|
||||||
//Si el nivel de amenza es inferior al umbral
|
// Si el nivel de amenza es inferior al umbral
|
||||||
if (mMenaceLevel < mMenaceLevelThreshold)
|
if (mMenaceLevel < mMenaceLevelThreshold)
|
||||||
{
|
{
|
||||||
Uint8 index = 0;
|
Uint8 index = 0;
|
||||||
|
|
||||||
//Obtiene el centro del jugador en el eje X
|
// Obtiene el centro del jugador en el eje X
|
||||||
int x = player.getPosX() + (player.getWidth() / 2);
|
int x = player.getPosX() + (player.getWidth() / 2);
|
||||||
|
|
||||||
//Crea un globo sobre el jugador en dirección hacia el centro
|
// Crea un globo sobre el jugador en dirección hacia el centro
|
||||||
if (x < (PLAY_AREA_WIDTH / 2))
|
if (x < (PLAY_AREA_WIDTH / 2))
|
||||||
{
|
{
|
||||||
index = createNewBalloon(0, PLAY_AREA_TOP + BLOCK - 37, BALLOON_4, BALLON_VELX_POSITIVE, 400);
|
index = createNewBalloon(0, PLAY_AREA_TOP + BLOCK - 37, BALLOON_4, BALLON_VELX_POSITIVE, 400);
|
||||||
@@ -462,34 +527,34 @@ void GameDirector::checkMenaceLevel()
|
|||||||
}
|
}
|
||||||
balloon[index].allignTo(x);
|
balloon[index].allignTo(x);
|
||||||
|
|
||||||
//Recalcula el nivel de amenaza con el nuevo globo
|
// Recalcula el nivel de amenaza con el nuevo globo
|
||||||
calculateMenaceLevel();
|
calculateMenaceLevel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Gestiona la entrada de teclado y mando durante el juego
|
// Gestiona la entrada de teclado y mando durante el juego
|
||||||
void GameDirector::checkGameInput()
|
void GameDirector::checkGameInput()
|
||||||
{
|
{
|
||||||
//Obtiene el estado de las teclas pulsadas del teclado
|
// Obtiene el estado de las teclas pulsadas del teclado
|
||||||
const Uint8 *keystates = SDL_GetKeyboardState(NULL);
|
const Uint8 *keystates = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
//Si está pulsada la tecla izquierda o el mando hacia la izquierda
|
// Si está pulsada la tecla izquierda o el mando hacia la izquierda
|
||||||
if ((keystates[SDL_SCANCODE_LEFT] != 0) || (SDL_JoystickGetAxis(gGameController, 0) < -JOYSTICK_DEAD_ZONE))
|
if ((keystates[SDL_SCANCODE_LEFT] != 0) || (SDL_JoystickGetAxis(gGameController, 0) < -JOYSTICK_DEAD_ZONE))
|
||||||
{
|
{
|
||||||
player.checkInput(INPUT_LEFT);
|
player.checkInput(INPUT_LEFT);
|
||||||
}
|
}
|
||||||
//Si está pulsada la tecla derecha o el mando hacia la derecha
|
// Si está pulsada la tecla derecha o el mando hacia la derecha
|
||||||
else if ((keystates[SDL_SCANCODE_RIGHT] != 0) || (SDL_JoystickGetAxis(gGameController, 0) > JOYSTICK_DEAD_ZONE))
|
else if ((keystates[SDL_SCANCODE_RIGHT] != 0) || (SDL_JoystickGetAxis(gGameController, 0) > JOYSTICK_DEAD_ZONE))
|
||||||
{
|
{
|
||||||
player.checkInput(INPUT_RIGHT);
|
player.checkInput(INPUT_RIGHT);
|
||||||
}
|
}
|
||||||
//Ninguna de las dos direcciones pulsadas
|
// Ninguna de las dos direcciones pulsadas
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
player.checkInput(NO_INPUT);
|
player.checkInput(NO_INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Comprobamos la tecla o el botón de disparo central
|
// Comprobamos la tecla o el botón de disparo central
|
||||||
if ((SDL_JoystickGetButton(gGameController, BUTTON_X)) || (keystates[SDL_SCANCODE_W] != 0))
|
if ((SDL_JoystickGetButton(gGameController, BUTTON_X)) || (keystates[SDL_SCANCODE_W] != 0))
|
||||||
{
|
{
|
||||||
if (player.canFire())
|
if (player.canFire())
|
||||||
@@ -497,12 +562,12 @@ void GameDirector::checkGameInput()
|
|||||||
createBullet(player.getPosX() + (player.getWidth() / 2) - 4, player.getPosY(), BULLET_UP);
|
createBullet(player.getPosX() + (player.getWidth() / 2) - 4, player.getPosY(), BULLET_UP);
|
||||||
player.setFireCooldown(10);
|
player.setFireCooldown(10);
|
||||||
|
|
||||||
//Reproduce el sonido de disparo
|
// Reproduce el sonido de disparo
|
||||||
Mix_PlayChannel(-1, gBulletFX, 0);
|
Mix_PlayChannel(-1, gBulletFX, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Comprobamos la tecla o el botón de disparo izquierdo
|
// Comprobamos la tecla o el botón de disparo izquierdo
|
||||||
if ((SDL_JoystickGetButton(gGameController, BUTTON_Y)) || (keystates[SDL_SCANCODE_Q] != 0))
|
if ((SDL_JoystickGetButton(gGameController, BUTTON_Y)) || (keystates[SDL_SCANCODE_Q] != 0))
|
||||||
{
|
{
|
||||||
if (player.canFire())
|
if (player.canFire())
|
||||||
@@ -510,12 +575,12 @@ void GameDirector::checkGameInput()
|
|||||||
createBullet(player.getPosX() + (player.getWidth() / 2) - 4, player.getPosY(), BULLET_LEFT);
|
createBullet(player.getPosX() + (player.getWidth() / 2) - 4, player.getPosY(), BULLET_LEFT);
|
||||||
player.setFireCooldown(10);
|
player.setFireCooldown(10);
|
||||||
|
|
||||||
//Reproduce el sonido de disparo
|
// Reproduce el sonido de disparo
|
||||||
Mix_PlayChannel(-1, gBulletFX, 0);
|
Mix_PlayChannel(-1, gBulletFX, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Comprobamos la tecla o el botón de disparo derecho
|
// Comprobamos la tecla o el botón de disparo derecho
|
||||||
if ((SDL_JoystickGetButton(gGameController, BUTTON_A)) || (keystates[SDL_SCANCODE_E] != 0))
|
if ((SDL_JoystickGetButton(gGameController, BUTTON_A)) || (keystates[SDL_SCANCODE_E] != 0))
|
||||||
{
|
{
|
||||||
if (player.canFire())
|
if (player.canFire())
|
||||||
@@ -523,38 +588,38 @@ void GameDirector::checkGameInput()
|
|||||||
createBullet(player.getPosX() + (player.getWidth() / 2) - 4, player.getPosY(), BULLET_RIGHT);
|
createBullet(player.getPosX() + (player.getWidth() / 2) - 4, player.getPosY(), BULLET_RIGHT);
|
||||||
player.setFireCooldown(10);
|
player.setFireCooldown(10);
|
||||||
|
|
||||||
//Reproduce el sonido de disparo
|
// Reproduce el sonido de disparo
|
||||||
Mix_PlayChannel(-1, gBulletFX, 0);
|
Mix_PlayChannel(-1, gBulletFX, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Comprobamos la tecla o el botón de pausa/menu
|
// Comprobamos la tecla o el botón de pausa/menu
|
||||||
if ((SDL_JoystickGetButton(gGameController, BUTTON_START)) || (keystates[SDL_SCANCODE_ESCAPE] != 0))
|
if ((SDL_JoystickGetButton(gGameController, BUTTON_START)) || (keystates[SDL_SCANCODE_ESCAPE] != 0))
|
||||||
{
|
{
|
||||||
setGameStatus(GAME_STATE_PAUSED);
|
setGameStatus(GAME_STATE_PAUSED);
|
||||||
|
|
||||||
//Detiene la música
|
// Detiene la música
|
||||||
Mix_HaltMusic();
|
Mix_HaltMusic();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Gestiona la entrada de teclado y mando durante el menu
|
// Gestiona la entrada de teclado y mando durante el menu
|
||||||
void GameDirector::checkMenuInput(Menu *menu)
|
void GameDirector::checkMenuInput(Menu *menu)
|
||||||
{
|
{
|
||||||
//Obtiene el estado de las teclas pulsadas del teclado
|
// Obtiene el estado de las teclas pulsadas del teclado
|
||||||
const Uint8 *keystates = SDL_GetKeyboardState(NULL);
|
const Uint8 *keystates = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
//Si está pulsada la tecla izquierda o el mando hacia la izquierda
|
// Si está pulsada la tecla izquierda o el mando hacia la izquierda
|
||||||
if ((keystates[SDL_SCANCODE_UP] != 0) || (SDL_JoystickGetAxis(gGameController, 1) < -JOYSTICK_DEAD_ZONE))
|
if ((keystates[SDL_SCANCODE_UP] != 0) || (SDL_JoystickGetAxis(gGameController, 1) < -JOYSTICK_DEAD_ZONE))
|
||||||
{
|
{
|
||||||
menu->checkInput(INPUT_UP);
|
menu->checkInput(INPUT_UP);
|
||||||
}
|
}
|
||||||
//Si está pulsada la tecla derecha o el mando hacia la derecha
|
// Si está pulsada la tecla derecha o el mando hacia la derecha
|
||||||
else if ((keystates[SDL_SCANCODE_DOWN] != 0) || (SDL_JoystickGetAxis(gGameController, 1) > JOYSTICK_DEAD_ZONE))
|
else if ((keystates[SDL_SCANCODE_DOWN] != 0) || (SDL_JoystickGetAxis(gGameController, 1) > JOYSTICK_DEAD_ZONE))
|
||||||
{
|
{
|
||||||
menu->checkInput(INPUT_DOWN);
|
menu->checkInput(INPUT_DOWN);
|
||||||
}
|
}
|
||||||
//Comprobamos la tecla o el botón de menu/pausa
|
// Comprobamos la tecla o el botón de menu/pausa
|
||||||
else if (keystates[SDL_SCANCODE_RETURN] != 0 || (SDL_JoystickGetButton(gGameController, BUTTON_A)))
|
else if (keystates[SDL_SCANCODE_RETURN] != 0 || (SDL_JoystickGetButton(gGameController, BUTTON_A)))
|
||||||
{
|
{
|
||||||
menu->checkInput(INPUT_FIRE);
|
menu->checkInput(INPUT_FIRE);
|
||||||
@@ -643,19 +708,19 @@ void GameDirector::checkMenuInput(Menu *menu)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//Obtiene el valor de la variable
|
// Obtiene el valor de la variable
|
||||||
Uint8 GameDirector::getGameStatus()
|
Uint8 GameDirector::getGameStatus()
|
||||||
{
|
{
|
||||||
return mGameStatus;
|
return mGameStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void GameDirector::setGameStatus(Uint8 status)
|
void GameDirector::setGameStatus(Uint8 status)
|
||||||
{
|
{
|
||||||
mGameStatus = status;
|
mGameStatus = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pinta una transición en pantalla
|
// Pinta una transición en pantalla
|
||||||
void GameDirector::renderTransition(Uint8 index)
|
void GameDirector::renderTransition(Uint8 index)
|
||||||
{
|
{
|
||||||
switch (index)
|
switch (index)
|
||||||
@@ -724,7 +789,7 @@ void GameDirector::renderTransition(Uint8 index)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pinta el texto GetReady en pantalla
|
// Pinta el texto GetReady en pantalla
|
||||||
void GameDirector::renderGetReady()
|
void GameDirector::renderGetReady()
|
||||||
{
|
{
|
||||||
if (mGetReady)
|
if (mGetReady)
|
||||||
@@ -746,41 +811,41 @@ void GameDirector::renderGetReady()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Bucle para el titulo del juego
|
// Bucle para el titulo del juego
|
||||||
void GameDirector::runTitle()
|
void GameDirector::runTitle()
|
||||||
{
|
{
|
||||||
//Si la música no está sonando
|
// Si la música no está sonando
|
||||||
if (Mix_PlayingMusic() == 0)
|
if (Mix_PlayingMusic() == 0)
|
||||||
{
|
{
|
||||||
//Reproduce la música
|
// Reproduce la música
|
||||||
Mix_PlayMusic(gTitleMusic, -1);
|
Mix_PlayMusic(gTitleMusic, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Comprueba los eventos que hay en la cola
|
// Comprueba los eventos que hay en la cola
|
||||||
while (SDL_PollEvent(&eventHandler) != 0)
|
while (SDL_PollEvent(&eventHandler) != 0)
|
||||||
{
|
{
|
||||||
//Evento de salida de la aplicación
|
// Evento de salida de la aplicación
|
||||||
if (eventHandler.type == SDL_QUIT)
|
if (eventHandler.type == SDL_QUIT)
|
||||||
{
|
{
|
||||||
setGameStatus(GAME_STATE_QUIT);
|
setGameStatus(GAME_STATE_QUIT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Limpia la pantalla
|
// Limpia la pantalla
|
||||||
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xFF);
|
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xFF);
|
||||||
SDL_RenderClear(gRenderer);
|
SDL_RenderClear(gRenderer);
|
||||||
|
|
||||||
//Dibuja los objetos
|
// Dibuja los objetos
|
||||||
titleBackground.render();
|
titleBackground.render();
|
||||||
menuTitle.render(whiteText);
|
menuTitle.render(whiteText);
|
||||||
|
|
||||||
//Actualiza la pantalla
|
// Actualiza la pantalla
|
||||||
SDL_RenderPresent(gRenderer);
|
SDL_RenderPresent(gRenderer);
|
||||||
|
|
||||||
//Comprueba las entradas para el menu
|
// Comprueba las entradas para el menu
|
||||||
checkMenuInput(&menuTitle);
|
checkMenuInput(&menuTitle);
|
||||||
|
|
||||||
//Comprueba si se ha seleccionado algún item del menú
|
// Comprueba si se ha seleccionado algún item del menú
|
||||||
switch (menuTitle.getItemSelected())
|
switch (menuTitle.getItemSelected())
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
@@ -801,44 +866,44 @@ void GameDirector::runTitle()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Bucle para el juego
|
// Bucle para el juego
|
||||||
void GameDirector::runGame()
|
void GameDirector::runGame()
|
||||||
{
|
{
|
||||||
//Si la música no está sonando
|
// Si la música no está sonando
|
||||||
if (Mix_PlayingMusic() == 0)
|
if (Mix_PlayingMusic() == 0)
|
||||||
{
|
{
|
||||||
//Reproduce la música
|
// Reproduce la música
|
||||||
Mix_PlayMusic(gPlayingMusic, -1);
|
Mix_PlayMusic(gPlayingMusic, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Lógica del juego
|
// Lógica del juego
|
||||||
//Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
||||||
if (SDL_GetTicks() - mOldTicks > mGameSpeed)
|
if (SDL_GetTicks() - mOldTicks > mGameSpeed)
|
||||||
{
|
{
|
||||||
//Actualiza el contador de ticks
|
// Actualiza el contador de ticks
|
||||||
mOldTicks = SDL_GetTicks();
|
mOldTicks = SDL_GetTicks();
|
||||||
|
|
||||||
//Comprueba el teclado/mando
|
// Comprueba el teclado/mando
|
||||||
checkGameInput();
|
checkGameInput();
|
||||||
|
|
||||||
//Comprueba los eventos que hay en la cola
|
// Comprueba los eventos que hay en la cola
|
||||||
while (SDL_PollEvent(&eventHandler) != 0)
|
while (SDL_PollEvent(&eventHandler) != 0)
|
||||||
{
|
{
|
||||||
//Evento de salida de la aplicación
|
// Evento de salida de la aplicación
|
||||||
if (eventHandler.type == SDL_QUIT)
|
if (eventHandler.type == SDL_QUIT)
|
||||||
{
|
{
|
||||||
setGameStatus(GAME_STATE_QUIT);
|
setGameStatus(GAME_STATE_QUIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Tecla T pulsada
|
// Tecla T pulsada
|
||||||
if (eventHandler.key.keysym.sym == SDLK_t)
|
if (eventHandler.key.keysym.sym == SDLK_t)
|
||||||
{
|
{
|
||||||
//startAllBalloons();
|
// startAllBalloons();
|
||||||
popBalloon(0);
|
popBalloon(0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
//W key pressed
|
// W key pressed
|
||||||
if (eventHandler.key.keysym.sym == SDLK_w)
|
if (eventHandler.key.keysym.sym == SDLK_w)
|
||||||
{
|
{
|
||||||
bulletTest.setPosY(bulletTest.getPosY() - 1);
|
bulletTest.setPosY(bulletTest.getPosY() - 1);
|
||||||
@@ -846,7 +911,7 @@ void GameDirector::runGame()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//S key pressed
|
// S key pressed
|
||||||
if (eventHandler.key.keysym.sym == SDLK_s)
|
if (eventHandler.key.keysym.sym == SDLK_s)
|
||||||
{
|
{
|
||||||
bulletTest.setPosY(bulletTest.getPosY() + 1);
|
bulletTest.setPosY(bulletTest.getPosY() + 1);
|
||||||
@@ -854,7 +919,7 @@ void GameDirector::runGame()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//A key pressed
|
// A key pressed
|
||||||
if (eventHandler.key.keysym.sym == SDLK_a)
|
if (eventHandler.key.keysym.sym == SDLK_a)
|
||||||
{
|
{
|
||||||
bulletTest.setPosX(bulletTest.getPosX() - 1);
|
bulletTest.setPosX(bulletTest.getPosX() - 1);
|
||||||
@@ -862,7 +927,7 @@ void GameDirector::runGame()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//D key pressed
|
// D key pressed
|
||||||
if (eventHandler.key.keysym.sym == SDLK_d)
|
if (eventHandler.key.keysym.sym == SDLK_d)
|
||||||
{
|
{
|
||||||
bulletTest.setPosX(bulletTest.getPosX() + 1);
|
bulletTest.setPosX(bulletTest.getPosX() + 1);
|
||||||
@@ -872,37 +937,37 @@ void GameDirector::runGame()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//Actualiza el jugador
|
// Actualiza el jugador
|
||||||
player.update();
|
player.update();
|
||||||
|
|
||||||
//Mueve los globos
|
// Mueve los globos
|
||||||
moveBalloons();
|
moveBalloons();
|
||||||
|
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
balloonTest.move();
|
balloonTest.move();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//Mueve las balas
|
// Mueve las balas
|
||||||
moveBullets();
|
moveBullets();
|
||||||
|
|
||||||
//Procesa las colisiones entre globos y balas
|
// Procesa las colisiones entre globos y balas
|
||||||
processBulletBallonCollision();
|
processBulletBallonCollision();
|
||||||
|
|
||||||
//Comprueba el nivel de amenaza
|
// Comprueba el nivel de amenaza
|
||||||
checkMenaceLevel();
|
checkMenaceLevel();
|
||||||
|
|
||||||
//Comprueba la colisión entre el jugador y los globos
|
// Comprueba la colisión entre el jugador y los globos
|
||||||
if (checkPlayerBallonCollision())
|
if (checkPlayerBallonCollision())
|
||||||
{
|
{
|
||||||
//stopAllBalloons();
|
// stopAllBalloons();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Limpia la pantalla
|
// Limpia la pantalla
|
||||||
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xFF);
|
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xFF);
|
||||||
SDL_RenderClear(gRenderer);
|
SDL_RenderClear(gRenderer);
|
||||||
|
|
||||||
//Dibuja los objetos
|
// Dibuja los objetos
|
||||||
gameBackground.render();
|
gameBackground.render();
|
||||||
renderBalloons();
|
renderBalloons();
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
@@ -913,31 +978,31 @@ void GameDirector::runGame()
|
|||||||
whiteText.write(0, 0, "X");
|
whiteText.write(0, 0, "X");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
//whiteText.write(0, 0, std::to_string(mMenaceLevelThreshold));
|
// whiteText.write(0, 0, std::to_string(mMenaceLevelThreshold));
|
||||||
//whiteText.write(0, BLOCK, std::to_string(player.getPosX() + player.getWidth()));
|
// whiteText.write(0, BLOCK, std::to_string(player.getPosX() + player.getWidth()));
|
||||||
renderBullets();
|
renderBullets();
|
||||||
player.render();
|
player.render();
|
||||||
renderScoreBoard(whiteText);
|
renderScoreBoard(whiteText);
|
||||||
renderGetReady();
|
renderGetReady();
|
||||||
|
|
||||||
//Actualiza la pantalla
|
// Actualiza la pantalla
|
||||||
SDL_RenderPresent(gRenderer);
|
SDL_RenderPresent(gRenderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Bucle para el menu de pausa del juego
|
// Bucle para el menu de pausa del juego
|
||||||
void GameDirector::runPausedGame()
|
void GameDirector::runPausedGame()
|
||||||
{
|
{
|
||||||
//Comprueba los eventos que hay en la cola
|
// Comprueba los eventos que hay en la cola
|
||||||
while (SDL_PollEvent(&eventHandler) != 0)
|
while (SDL_PollEvent(&eventHandler) != 0)
|
||||||
{
|
{
|
||||||
//Evento de salida de la aplicación
|
// Evento de salida de la aplicación
|
||||||
if (eventHandler.type == SDL_QUIT)
|
if (eventHandler.type == SDL_QUIT)
|
||||||
{
|
{
|
||||||
setGameStatus(GAME_STATE_QUIT);
|
setGameStatus(GAME_STATE_QUIT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Dibuja los objetos
|
// Dibuja los objetos
|
||||||
gameBackground.render();
|
gameBackground.render();
|
||||||
renderBalloons();
|
renderBalloons();
|
||||||
renderBullets();
|
renderBullets();
|
||||||
@@ -945,13 +1010,13 @@ void GameDirector::runPausedGame()
|
|||||||
renderScoreBoard(whiteText);
|
renderScoreBoard(whiteText);
|
||||||
menuPause.render(whiteText);
|
menuPause.render(whiteText);
|
||||||
|
|
||||||
//Limpia la pantalla
|
// Limpia la pantalla
|
||||||
SDL_RenderPresent(gRenderer);
|
SDL_RenderPresent(gRenderer);
|
||||||
|
|
||||||
//Comprueba las entradas para el menu
|
// Comprueba las entradas para el menu
|
||||||
checkMenuInput(&menuPause);
|
checkMenuInput(&menuPause);
|
||||||
|
|
||||||
//Comprueba si se ha seleccionado algún item del menú
|
// Comprueba si se ha seleccionado algún item del menú
|
||||||
switch (menuPause.getItemSelected())
|
switch (menuPause.getItemSelected())
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
|
|||||||
@@ -13,182 +13,196 @@
|
|||||||
#ifndef GAMEDIRECTOR_H
|
#ifndef GAMEDIRECTOR_H
|
||||||
#define GAMEDIRECTOR_H
|
#define GAMEDIRECTOR_H
|
||||||
|
|
||||||
//GameDirector
|
// GameDirector
|
||||||
class GameDirector
|
class GameDirector
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
//Constructor
|
// Constructor
|
||||||
GameDirector();
|
GameDirector();
|
||||||
|
|
||||||
//Iniciador
|
// Destructor
|
||||||
|
~GameDirector();
|
||||||
|
|
||||||
|
// Iniciador
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
//Hace una pausa de milisegundos
|
// Hace una pausa de milisegundos
|
||||||
void sleep(Uint16 time);
|
void sleep(Uint16 time);
|
||||||
|
|
||||||
//Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void setScore(Uint32 score);
|
void setScore(Uint32 score);
|
||||||
|
|
||||||
//Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void setHiScore(Uint32 score);
|
void setHiScore(Uint32 score);
|
||||||
|
|
||||||
//Actualiza el valor de HiScore en caso necesario
|
// Actualiza el valor de HiScore en caso necesario
|
||||||
void updateHiScore();
|
void updateHiScore();
|
||||||
|
|
||||||
//Transforma un valor numérico en una cadena de 6 cifras
|
// Transforma un valor numérico en una cadena de 6 cifras
|
||||||
std::string updateScoreText(Uint32 num);
|
std::string updateScoreText(Uint32 num);
|
||||||
|
|
||||||
//Pinta el marcador en pantalla usando un objeto texto
|
// Pinta el marcador en pantalla usando un objeto texto
|
||||||
void renderScoreBoard(Text &text);
|
void renderScoreBoard(Text &text);
|
||||||
|
|
||||||
//Mueve todos los globos activos
|
// Mueve todos los globos activos
|
||||||
void moveBalloons();
|
void moveBalloons();
|
||||||
|
|
||||||
//Pinta en pantalla todos los globos activos
|
// Pinta en pantalla todos los globos activos
|
||||||
void renderBalloons();
|
void renderBalloons();
|
||||||
|
|
||||||
//Devuelve el primer indice no activo del vector de globos
|
// Devuelve el primer indice no activo del vector de globos
|
||||||
Uint8 getBallonFreeIndex();
|
Uint8 getBallonFreeIndex();
|
||||||
|
|
||||||
//Crea un globo nuevo en el vector de globos
|
// Crea un globo nuevo en el vector de globos
|
||||||
Uint8 createNewBalloon(int x, int y, Uint8 kind, float velx, Uint16 stoppedtimer);
|
Uint8 createNewBalloon(int x, int y, Uint8 kind, float velx, Uint16 stoppedtimer);
|
||||||
|
|
||||||
//Establece a cero todos los valores del vector de objetos globo
|
// Establece a cero todos los valores del vector de objetos globo
|
||||||
void resetBalloons();
|
void resetBalloons();
|
||||||
|
|
||||||
//Explosiona un globo. Lo destruye y crea otros dos si es el caso
|
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
|
||||||
void popBalloon(Uint8 index);
|
void popBalloon(Uint8 index);
|
||||||
|
|
||||||
//Detiene todos los globos
|
// Detiene todos los globos
|
||||||
void stopAllBalloons();
|
void stopAllBalloons();
|
||||||
|
|
||||||
//Pone en marcha todos los globos
|
// Pone en marcha todos los globos
|
||||||
void startAllBalloons();
|
void startAllBalloons();
|
||||||
|
|
||||||
//Obtiene el numero de globos activos
|
// Obtiene el numero de globos activos
|
||||||
Uint8 countBalloons();
|
Uint8 countBalloons();
|
||||||
|
|
||||||
//Comprueba la colisión entre el jugador y los globos activos
|
// Comprueba la colisión entre el jugador y los globos activos
|
||||||
bool checkPlayerBallonCollision();
|
bool checkPlayerBallonCollision();
|
||||||
|
|
||||||
//Comprueba y procesa la colisión entre las balas y los globos
|
// Comprueba y procesa la colisión entre las balas y los globos
|
||||||
void processBulletBallonCollision();
|
void processBulletBallonCollision();
|
||||||
|
|
||||||
//Mueve las balas activas
|
// Mueve las balas activas
|
||||||
void moveBullets();
|
void moveBullets();
|
||||||
|
|
||||||
//Pinta las balas activas
|
// Pinta las balas activas
|
||||||
void renderBullets();
|
void renderBullets();
|
||||||
|
|
||||||
//Devuelve el primer indice no activo del vector de balas
|
// Devuelve el primer indice no activo del vector de balas
|
||||||
Uint8 getBulletFreeIndex();
|
Uint8 getBulletFreeIndex();
|
||||||
|
|
||||||
//Establece a cero todos los valores del vector de objetos bala
|
// Establece a cero todos los valores del vector de objetos bala
|
||||||
void resetBullets();
|
void resetBullets();
|
||||||
|
|
||||||
//Crea un objeto bala
|
// Crea un objeto bala
|
||||||
void createBullet(int x, int y, Uint8 kind);
|
void createBullet(int x, int y, Uint8 kind);
|
||||||
|
|
||||||
//Calcula y establece el valor de amenaza en funcion de los globos activos
|
// Calcula y establece el valor de amenaza en funcion de los globos activos
|
||||||
void calculateMenaceLevel();
|
void calculateMenaceLevel();
|
||||||
|
|
||||||
//Obtiene el valor de la variable
|
// Obtiene el valor de la variable
|
||||||
Uint8 getMenaceLevel();
|
Uint8 getMenaceLevel();
|
||||||
|
|
||||||
//Gestiona el nivel de amenaza
|
// Gestiona el nivel de amenaza
|
||||||
void checkMenaceLevel();
|
void checkMenaceLevel();
|
||||||
|
|
||||||
//Gestiona la entrada de teclado y mando durante el juego
|
// Gestiona la entrada de teclado y mando durante el juego
|
||||||
void checkGameInput();
|
void checkGameInput();
|
||||||
|
|
||||||
//Gestiona la entrada de teclado y mando durante el menu
|
// Gestiona la entrada de teclado y mando durante el menu
|
||||||
void checkMenuInput(Menu* menu);
|
void checkMenuInput(Menu *menu);
|
||||||
|
|
||||||
//Obtiene el valor de la variable
|
// Obtiene el valor de la variable
|
||||||
Uint8 getGameStatus();
|
Uint8 getGameStatus();
|
||||||
|
|
||||||
//Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void setGameStatus(Uint8 status);
|
void setGameStatus(Uint8 status);
|
||||||
|
|
||||||
//Pinta una transición en pantalla
|
// Pinta una transición en pantalla
|
||||||
void renderTransition(Uint8 index);
|
void renderTransition(Uint8 index);
|
||||||
|
|
||||||
//Pinta el texto GetReady en pantalla
|
// Pinta el texto GetReady en pantalla
|
||||||
void renderGetReady();
|
void renderGetReady();
|
||||||
|
|
||||||
//Bucle para el titulo del juego
|
// Bucle para el titulo del juego
|
||||||
void runTitle();
|
void runTitle();
|
||||||
|
|
||||||
//Bucle para el juego
|
// Bucle para el juego
|
||||||
void runGame();
|
void runGame();
|
||||||
|
|
||||||
//Bucle para el menu de pausa del juego
|
// Bucle para el menu de pausa del juego
|
||||||
void runPausedGame();
|
void runPausedGame();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//Manejador de eventos
|
// Objetos con la música del juego
|
||||||
|
Mix_Music *gTitleMusic = NULL;
|
||||||
|
Mix_Music *gPlayingMusic = NULL;
|
||||||
|
|
||||||
|
// Objetos con los efectos de sonido del juego
|
||||||
|
Mix_Chunk *gPopBalloonFX = NULL;
|
||||||
|
Mix_Chunk *gBulletFX = NULL;
|
||||||
|
|
||||||
|
// Manejador para el mando 1
|
||||||
|
SDL_Joystick *gGameController = NULL;
|
||||||
|
|
||||||
|
// Manejador de eventos
|
||||||
SDL_Event eventHandler;
|
SDL_Event eventHandler;
|
||||||
|
|
||||||
//El jugador
|
// El jugador
|
||||||
Player player;
|
Player player;
|
||||||
|
|
||||||
//Vector con los objetos globo
|
// Vector con los objetos globo
|
||||||
Balloon balloon[50];
|
Balloon balloon[50];
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
Balloon balloonTest;
|
Balloon balloonTest;
|
||||||
Bullet bulletTest;
|
Bullet bulletTest;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//Vector con los objetos bala
|
// Vector con los objetos bala
|
||||||
Bullet bullet[50];
|
Bullet bullet[50];
|
||||||
|
|
||||||
//Fondo del juego
|
// Fondo del juego
|
||||||
Background gameBackground;
|
Background gameBackground;
|
||||||
|
|
||||||
//Fondo de la pantalla de titulo
|
// Fondo de la pantalla de titulo
|
||||||
Background titleBackground;
|
Background titleBackground;
|
||||||
|
|
||||||
//Texto blanco
|
// Texto blanco
|
||||||
Text whiteText;
|
Text whiteText;
|
||||||
|
|
||||||
//Texto negro
|
// Texto negro
|
||||||
Text blackText;
|
Text blackText;
|
||||||
|
|
||||||
//Menu de la pantalla de título
|
// Menu de la pantalla de título
|
||||||
Menu menuTitle;
|
Menu menuTitle;
|
||||||
|
|
||||||
//Menú de la pantalla de pausa
|
// Menú de la pantalla de pausa
|
||||||
Menu menuPause;
|
Menu menuPause;
|
||||||
|
|
||||||
//Indicador para el bucle principal
|
// Indicador para el bucle principal
|
||||||
Uint8 mGameStatus;
|
Uint8 mGameStatus;
|
||||||
|
|
||||||
//Puntuación actual y puntuación máxima
|
// Puntuación actual y puntuación máxima
|
||||||
Uint32 mScore;
|
Uint32 mScore;
|
||||||
Uint32 mHiScore;
|
Uint32 mHiScore;
|
||||||
|
|
||||||
//Cadena de texto con la puntuación actual y la puntuación máxima de 6 cifras
|
// Cadena de texto con la puntuación actual y la puntuación máxima de 6 cifras
|
||||||
std::string mScoreText;
|
std::string mScoreText;
|
||||||
std::string mHiScoreText;
|
std::string mHiScoreText;
|
||||||
|
|
||||||
//Número máximo de globos y balas que puede almacenar el vector
|
// Número máximo de globos y balas que puede almacenar el vector
|
||||||
Uint8 mMaxBalloons;
|
Uint8 mMaxBalloons;
|
||||||
Uint8 mMaxBullets;
|
Uint8 mMaxBullets;
|
||||||
|
|
||||||
//Contador de ticks para ajustar la velocidad del juego
|
// Contador de ticks para ajustar la velocidad del juego
|
||||||
Uint32 mOldTicks;
|
Uint32 mOldTicks;
|
||||||
|
|
||||||
//Velocidad a la que se repite el bucle de juego
|
// Velocidad a la que se repite el bucle de juego
|
||||||
Uint8 mGameSpeed;
|
Uint8 mGameSpeed;
|
||||||
|
|
||||||
//Nivel de amenaza actual
|
// Nivel de amenaza actual
|
||||||
Uint8 mMenaceLevel;
|
Uint8 mMenaceLevel;
|
||||||
|
|
||||||
//Umbral del nivel de amenaza. Si el nivel de amenaza cae por debajo del umbral,
|
// Umbral del nivel de amenaza. Si el nivel de amenaza cae por debajo del umbral,
|
||||||
//se generan más globos. Si el umbral aumenta, aumenta el numero de globos
|
// se generan más globos. Si el umbral aumenta, aumenta el numero de globos
|
||||||
Uint8 mMenaceLevelThreshold;
|
Uint8 mMenaceLevelThreshold;
|
||||||
|
|
||||||
//Indica si ha de aparecer el texto de GetReady en pantalla
|
// Indica si ha de aparecer el texto de GetReady en pantalla
|
||||||
bool mGetReady;
|
bool mGetReady;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -15,21 +15,9 @@ struct Circle
|
|||||||
Uint8 r;
|
Uint8 r;
|
||||||
};
|
};
|
||||||
|
|
||||||
//La ventana donde dibujamos
|
|
||||||
SDL_Window *gWindow = NULL;
|
|
||||||
|
|
||||||
//El renderizador de la ventana
|
//El renderizador de la ventana
|
||||||
SDL_Renderer *gRenderer = NULL;
|
SDL_Renderer *gRenderer = NULL;
|
||||||
|
|
||||||
//Manejador para el mando 1
|
|
||||||
SDL_Joystick* gGameController = NULL;
|
|
||||||
|
|
||||||
//Objetos con la música del juego
|
|
||||||
Mix_Music *gTitleMusic = NULL;
|
|
||||||
Mix_Music *gPlayingMusic = NULL;
|
|
||||||
|
|
||||||
//Objetos con los efectos de sonido del juego
|
|
||||||
Mix_Chunk *gPopBalloonFX = NULL;
|
|
||||||
Mix_Chunk *gBulletFX = NULL;
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -54,6 +54,9 @@ un tipo asociado diferente a NO_KIND
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
//La ventana donde dibujamos
|
||||||
|
SDL_Window *gWindow = NULL;
|
||||||
|
|
||||||
//Arranca SDL y crea la ventana
|
//Arranca SDL y crea la ventana
|
||||||
bool init();
|
bool init();
|
||||||
|
|
||||||
@@ -90,23 +93,6 @@ bool init()
|
|||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Comprueba los mandos
|
|
||||||
if (SDL_NumJoysticks() < 1)
|
|
||||||
{
|
|
||||||
printf("Warning: No joysticks connected!\n");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//Carga el mando
|
|
||||||
gGameController = SDL_JoystickOpen(0);
|
|
||||||
if (gGameController == NULL)
|
|
||||||
{
|
|
||||||
printf("Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
|
|
||||||
}
|
|
||||||
printf("%i joysticks were found.\n", SDL_NumJoysticks());
|
|
||||||
std::cout << SDL_JoystickNumButtons(gGameController) << " buttons\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
//Crea la ventana
|
//Crea la ventana
|
||||||
gWindow = SDL_CreateWindow("Super Popping (Like Loc) in Jailers World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, VIEW_WIDTH, VIEW_HEIGHT, SDL_WINDOW_SHOWN);
|
gWindow = SDL_CreateWindow("Super Popping (Like Loc) in Jailers World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, VIEW_WIDTH, VIEW_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
if (gWindow == NULL)
|
if (gWindow == NULL)
|
||||||
@@ -214,38 +200,6 @@ bool loadMedia()
|
|||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Carga la música del titulo
|
|
||||||
gTitleMusic = Mix_LoadMUS("media/music/title.ogg");
|
|
||||||
if (gTitleMusic == NULL)
|
|
||||||
{
|
|
||||||
printf("Failed to load title music! SDL_mixer Error: %s\n", Mix_GetError());
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Carga la música del juego
|
|
||||||
gPlayingMusic = Mix_LoadMUS("media/music/playing.ogg");
|
|
||||||
if (gPlayingMusic == NULL)
|
|
||||||
{
|
|
||||||
printf("Failed to load playing music! SDL_mixer Error: %s\n", Mix_GetError());
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Carga los efectos de sonido para la explosión de los globos
|
|
||||||
gPopBalloonFX = Mix_LoadWAV("media/sound/balloon.wav");
|
|
||||||
if (gPopBalloonFX == NULL)
|
|
||||||
{
|
|
||||||
printf("Failed to load balloon sound effect! SDL_mixer Error: %s\n", Mix_GetError());
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Carga los efectos de sonido para los disparos del jugador
|
|
||||||
gBulletFX = Mix_LoadWAV("media/sound/bullet.wav");
|
|
||||||
if (gBulletFX == NULL)
|
|
||||||
{
|
|
||||||
printf("Failed to load bullet sound effect! SDL_mixer Error: %s\n", Mix_GetError());
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,22 +216,6 @@ void close()
|
|||||||
gBalloonTexture.free();
|
gBalloonTexture.free();
|
||||||
gMiscTexture.free();
|
gMiscTexture.free();
|
||||||
|
|
||||||
//Libera los efectos de sonido
|
|
||||||
Mix_FreeChunk(gPopBalloonFX);
|
|
||||||
Mix_FreeChunk(gBulletFX);
|
|
||||||
gPopBalloonFX = NULL;
|
|
||||||
gBulletFX = NULL;
|
|
||||||
|
|
||||||
//Libra la música
|
|
||||||
Mix_FreeMusic(gTitleMusic);
|
|
||||||
gTitleMusic = NULL;
|
|
||||||
Mix_FreeMusic(gPlayingMusic);
|
|
||||||
gPlayingMusic = NULL;
|
|
||||||
|
|
||||||
//Libera el mando
|
|
||||||
SDL_JoystickClose(gGameController);
|
|
||||||
gGameController = NULL;
|
|
||||||
|
|
||||||
//Destruye la ventana
|
//Destruye la ventana
|
||||||
SDL_DestroyRenderer(gRenderer);
|
SDL_DestroyRenderer(gRenderer);
|
||||||
SDL_DestroyWindow(gWindow);
|
SDL_DestroyWindow(gWindow);
|
||||||
|
|||||||
Reference in New Issue
Block a user