diff --git a/.gitignore b/.gitignore index b5152b7..1b4172d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .vscode *.DS_Store bin -data/config.bin +data/config.txt data/score.bin \ No newline at end of file diff --git a/source/balloon.cpp b/source/balloon.cpp index 7a3045e..4142eb2 100644 --- a/source/balloon.cpp +++ b/source/balloon.cpp @@ -7,7 +7,7 @@ Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 c mSprite = new AnimatedSprite(texture, renderer, file); disable(); - mEnabled = true; + mEnabled = true; switch (kind) { @@ -225,7 +225,7 @@ Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 c // Valores para el efecto de rebote mBouncing.enabled = false; mBouncing.counter = 0; - mBouncing.speed = 0; + mBouncing.speed = 2; mBouncing.zoomW = 1.0f; mBouncing.zoomH = 1.0f; mBouncing.despX = 0.0f; @@ -252,18 +252,12 @@ Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 c mStoppedCounter = 0; mBlinking = false; mVisible = true; - mInvulnerable = false; + mInvulnerable = true; mBeingCreated = true; - mCreationCounter = creationtimer; - mCreationCounterIni = creationtimer; + mCreationCounter = creationtimer; + mCreationCounterIni = creationtimer; mPopping = false; - mBouncing.enabled = false; - mBouncing.counter = 0; - mBouncing.speed = 2; - mBouncing.zoomW = 1; - mBouncing.zoomH = 1; - mBouncing.despX = 0; - mBouncing.despY = 0; + mCounter = 0; mTravelY = 1.0f; mSpeed = speed; @@ -438,7 +432,7 @@ void Balloon::disable() mSpeed = 0; mStopped = false; mStoppedCounter = 0; - //mTimeToLive = 0; + // mTimeToLive = 0; mTravelY = 0; mVelX = 0.0f; mVelY = 0.0f; @@ -483,8 +477,8 @@ void Balloon::updateState() setStop(true); if (mSprite->animationIsCompleted()) { - //mSprite->setAnimationCompleted(BALLOON_POP_ANIMATION, false); - //mTimeToLive = 0; + // mSprite->setAnimationCompleted(BALLOON_POP_ANIMATION, false); + // mTimeToLive = 0; disable(); } /*else if (mTimeToLive > 0) @@ -571,6 +565,20 @@ void Balloon::updateState() // Establece la animación correspondiente al estado void Balloon::updateAnimation() { + std::string creatingAnimation = "blue"; + std::string normalAnimation = "orange"; + + if (mKind == POWER_BALL) + { + creatingAnimation = "powerball"; + normalAnimation = "powerball"; + } + else if (getClass() == HEXAGON_CLASS) + { + creatingAnimation = "red"; + normalAnimation = "green"; + } + // Establece el frame de animación if (isPopping()) { @@ -578,11 +586,11 @@ void Balloon::updateAnimation() } else if (isBeingCreated()) { - mSprite->setCurrentAnimation("blue"); + mSprite->setCurrentAnimation(creatingAnimation); } else { - mSprite->setCurrentAnimation("orange"); + mSprite->setCurrentAnimation(normalAnimation); } mSprite->animate(); diff --git a/source/game.cpp b/source/game.cpp index 37f2fe5..754c78e 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -2462,6 +2462,22 @@ void Game::createItemScoreSprite(int x, int y, SmartSprite *sprite) ss->setEnabledCounter(100); } +// Vacia el vector de smartsprites +void Game::freeSmartSprites() +{ + if (smartSprites.empty() == false) + { + for (int i = smartSprites.size() - 1; i >= 0; --i) + { + if (smartSprites.at(i)->hasFinished()) + { + delete smartSprites.at(i); + smartSprites.erase(smartSprites.begin() + i); + } + } + } +} + // Dibuja el efecto de flash void Game::renderFlashEffect() { @@ -2662,63 +2678,77 @@ void Game::updateEnemyDeployCounter() } } -// Actualiza el campo de juego -void Game::updatePlayField() +// Actualiza el juego +void Game::update() { - // Comprueba el teclado/mando - checkGameInput(); + // Comprueba los eventos que hay en cola + checkEventHandler(); - // Actualiza las variables del jugador - updatePlayer(); - - // Actualiza el fondo - updateBackground(); - - // Mueve los globos - updateBalloons(); - - // Mueve las balas - moveBullets(); - - // Actualiza los items - updateItems(); - - // Actualiza el valor de mCurrentStage - updateStage(); - - // Actualiza el estado de muerte - updateDeath(); - - // Actualiza los SmartSprites - updateSmartSprites(); - - // Actualiza los contadores de estado y efectos - updateTimeStoppedCounter(); - updateEnemyDeployCounter(); - updateShakeEffect(); - - // Actualiza el ayudante - updateHelper(); - - // Comprueba las colisiones entre globos y balas - checkBulletBalloonCollision(); - - // Comprueba el nivel de amenaza para ver si se han de crear nuevos enemigos - if (!mGameCompleted) + // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego + if (SDL_GetTicks() - mTicks > mTicksSpeed) { - updateMenace(); + // Actualiza el contador de ticks + mTicks = SDL_GetTicks(); + + // Actualiza el contador de juego + mCounter++; + + // Comprueba el teclado/mando + checkGameInput(); + + // Actualiza las variables del jugador + updatePlayer(); + + // Actualiza el fondo + updateBackground(); + + // Mueve los globos + updateBalloons(); + + // Mueve las balas + moveBullets(); + + // Actualiza los items + updateItems(); + + // Actualiza el valor de mCurrentStage + updateStage(); + + // Actualiza el estado de muerte + updateDeath(); + + // Actualiza los SmartSprites + updateSmartSprites(); + + // Actualiza los contadores de estado y efectos + updateTimeStoppedCounter(); + updateEnemyDeployCounter(); + updateShakeEffect(); + + // Actualiza el ayudante + updateHelper(); + + // Comprueba las colisiones entre globos y balas + checkBulletBalloonCollision(); + + // Comprueba el nivel de amenaza para ver si se han de crear nuevos enemigos + if (!mGameCompleted) + { + updateMenace(); + } + + // Actualiza la velocidad de los enemigos + updateBalloonSpeed(); + + // Actualiza el tramo final de juego, una vez completado + updateGameCompleted(); + + // Vacia los vectores + freeBullets(); + freeBalloons(); + freeItems(); + freeSmartSprites(); } - - // Actualiza la velocidad de los enemigos - updateBalloonSpeed(); - - // Actualiza el tramo final de juego, una vez completado - updateGameCompleted(); - - // Vacia los vectores - freeBullets(); - freeBalloons(); - freeItems(); } // Actualiza el fondo @@ -2788,9 +2818,16 @@ void Game::renderBackground() mSpriteGrass->render(); } -// Dibuja el campo de juego -void Game::renderPlayField() +// Dibuja el juego +void Game::render() { + // Prepara para empezar a dibujar en la textura de juego + mScreen->start(); + + // Limpia la pantalla + mScreen->clean(bgColor); + + // Dibuja los objetos renderBackground(); renderBalloons(); renderBullets(); @@ -2817,6 +2854,12 @@ void Game::renderPlayField() renderFlashEffect(); mText->write(0, 0, std::to_string(balloons.size())); + + // Pinta la informacion de debug + renderDebugInfo(); + + // Vuelca el contenido del renderizador en pantalla + mScreen->blit(); } // Gestiona el nivel de amenaza @@ -3193,44 +3236,11 @@ section_t Game::run() } } - // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego - if (SDL_GetTicks() - mTicks > mTicksSpeed) - { - // Actualiza el contador de ticks - mTicks = SDL_GetTicks(); - - // Actualiza el contador de juego - mCounter++; - - // Comprueba los eventos que hay en la cola - while (SDL_PollEvent(mEventHandler) != 0) - { - // Evento de salida de la aplicación - if (mEventHandler->type == SDL_QUIT) - { - mSection.name = PROG_SECTION_QUIT; - break; - } - } - - // Actualiza la lógica del juego - updatePlayField(); - } - - // Prepara para empezar a dibujar en la textura de juego - mScreen->start(); - - // Limpia la pantalla - mScreen->clean(bgColor); + // Actualiza la lógica del juego + update(); // Dibuja los objetos - renderPlayField(); - - // Pinta la informacion de debug - renderDebugInfo(); - - // Vuelca el contenido del renderizador en pantalla - mScreen->blit(); + render(); } } @@ -3246,15 +3256,7 @@ void Game::runPausedGame() while ((mSection.subsection == GAME_SECTION_PAUSE) && (mSection.name == PROG_SECTION_GAME)) { // Comprueba los eventos que hay en la cola - while (SDL_PollEvent(mEventHandler) != 0) - { - // Evento de salida de la aplicación - if (mEventHandler->type == SDL_QUIT) - { - mSection.name = PROG_SECTION_QUIT; - break; - } - } + checkEventHandler(); // Calcula la lógica de los objetos if (SDL_GetTicks() - mTicks > mTicksSpeed) @@ -3281,7 +3283,37 @@ void Game::runPausedGame() mScreen->clean(bgColor); // Pinta el escenario - renderPlayField(); + { + renderBackground(); + renderBalloons(); + renderBullets(); + renderMessages(); + renderItems(); + renderSmartSprites(); + renderScoreBoard(); + + for (auto player : players) + { + player->render(); + } + + if ((mDeathCounter <= 150) && !players.at(0)->isAlive()) + { + renderDeathFade(150 - mDeathCounter); + } + + if ((mGameCompleted) && (mGameCompletedCounter >= 300)) + { + renderDeathFade(mGameCompletedCounter - 300); + } + + renderFlashEffect(); + + mText->write(0, 0, std::to_string(balloons.size())); + + // Pinta la informacion de debug + renderDebugInfo(); + } mMenuPause->render(); mFade->render(); @@ -3599,4 +3631,30 @@ bool Game::allPlayersAreDead() } return success; +} + +// Comprueba los eventos que hay en cola +void Game::checkEventHandler() +{ + while (SDL_PollEvent(mEventHandler) != 0) + { + // Evento de salida de la aplicación + if (mEventHandler->type == SDL_QUIT) + { + mSection.name = PROG_SECTION_QUIT; + break; + } + else if (mEventHandler->type == SDL_KEYDOWN && mEventHandler->key.repeat == 0) + { + switch (mEventHandler->key.keysym.scancode) + { + case SDL_SCANCODE_P: + createPowerBall(); + break; + + default: + break; + } + } + } } \ No newline at end of file diff --git a/source/game.h b/source/game.h index f243fbf..e68199d 100644 --- a/source/game.h +++ b/source/game.h @@ -239,6 +239,15 @@ private: demo_t mDemo; // Variable con todas las variables relacionadas con el modo demo debug_t mDebug; // Variable con las opciones de debug + // Actualiza el juego + void update(); + + // Dibuja el juego + void render(); + + // Comprueba los eventos que hay en cola + void checkEventHandler(); + // Inicializa el vector con los valores del seno void initSin(); @@ -392,6 +401,9 @@ private: // Crea un objeto SmartSprite void createItemScoreSprite(int x, int y, SmartSprite *sprite); + // Vacia el vector de smartsprites + void freeSmartSprites(); + // Dibuja el efecto de flash void renderFlashEffect(); @@ -443,17 +455,12 @@ private: // Gestiona el nivel de amenaza void updateMenace(); - // Actualiza el campo de juego - void updatePlayField(); - // Actualiza el fondo void updateBackground(); // Dibuja el fondo void renderBackground(); - // Dibuja el campo de juego - void renderPlayField(); // Gestiona la entrada durante el juego void checkGameInput(); diff --git a/source/ltexture.cpp b/source/ltexture.cpp index 1d01275..94a88fa 100644 --- a/source/ltexture.cpp +++ b/source/ltexture.cpp @@ -149,7 +149,7 @@ void LTexture::setAlpha(Uint8 alpha) // Renderiza la textura en un punto específico void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip) { - // Establece el destini de renderizado en la pantalla + // Establece el destino de renderizado en la pantalla SDL_Rect renderQuad = {x, y, width, height}; // Obtiene las dimesiones del clip de renderizado diff --git a/source/movingsprite.cpp b/source/movingsprite.cpp index 8500651..291eca2 100644 --- a/source/movingsprite.cpp +++ b/source/movingsprite.cpp @@ -45,7 +45,7 @@ MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vel spriteClip = {0, 0, w, h}; // Establece el centro de rotación - center = {0, 0}; + center = nullptr; // Establece el tipo de volteado currentFlip = SDL_FLIP_NONE; @@ -73,7 +73,7 @@ void MovingSprite::clear() angle = 0.0; // Angulo para dibujarlo rotateEnabled = false; // Indica si ha de rotar - center = {0, 0}; // Centro de rotación + center = nullptr; // Centro de rotación rotateSpeed = 0; // Velocidad de giro rotateAmount = 0.0; // Cantidad de grados a girar en cada iteración counter = 0; // Contador interno @@ -101,7 +101,9 @@ void MovingSprite::move() void MovingSprite::render() { if (enabled) - texture->render(renderer, (int)x, (int)y, &spriteClip, zoomW, zoomH, angle, ¢er, currentFlip); + { + texture->render(renderer, (int)x, (int)y, &spriteClip, zoomW, zoomH, angle, center, currentFlip); + } } // Obtiene el valor de la variable diff --git a/source/movingsprite.h b/source/movingsprite.h index aeedcab..d94f8ed 100644 --- a/source/movingsprite.h +++ b/source/movingsprite.h @@ -30,7 +30,7 @@ protected: int rotateSpeed; // Velocidad de giro double rotateAmount; // Cantidad de grados a girar en cada iteración int counter; // Contador interno - SDL_Point center; // Centro de rotación + SDL_Point *center; // Centro de rotación SDL_RendererFlip currentFlip; // Indica como se voltea el sprite public: