From 48f84d28bd0604ba04dca91760896eafd9c5d6ab Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 23 Sep 2022 19:50:40 +0200 Subject: [PATCH] Trabajando en las colisiones con los enemigos --- source/animatedsprite.cpp | 8 +- source/asset.cpp | 49 ++++++--- source/asset.h | 4 +- source/easing.cpp | 214 -------------------------------------- source/easing.h | 40 ------- source/enemy_engine.cpp | 27 +++++ source/enemy_engine.h | 3 + source/game.cpp | 25 ++--- source/game.h | 4 +- source/input.cpp | 12 +-- source/intro.h | 4 +- source/item_tracker.cpp | 32 ++++-- source/logo.h | 4 +- source/map.cpp | 20 ++-- source/menu.cpp | 8 +- source/player.cpp | 60 ++++++++--- source/player.h | 13 ++- source/scoreboard.cpp | 2 +- source/scoreboard.h | 1 - source/text.cpp | 6 +- source/title.h | 4 +- 21 files changed, 196 insertions(+), 344 deletions(-) delete mode 100644 source/easing.cpp delete mode 100644 source/easing.h diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index 51cca79..adcdd7a 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -57,7 +57,7 @@ void AnimatedSprite::animate() // Si alcanza el final de la animación, reinicia el contador de la animación // en función de la variable loop y coloca el nuevo frame - if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size()) + if (animation[currentAnimation].currentFrame >= (int)animation[currentAnimation].frames.size()) { if (animation[currentAnimation].loop == -1) { // Si no hay loop, deja el último frame @@ -85,7 +85,7 @@ void AnimatedSprite::animate() void AnimatedSprite::setCurrentFrame(int num) { // Descarta valores fuera de rango - if (num >= animation[currentAnimation].frames.size()) + if (num >= (int)animation[currentAnimation].frames.size()) { num = 0; } @@ -170,7 +170,7 @@ bool AnimatedSprite::load(std::string filePath) int pos = line.find("="); // Procesa las dos subcadenas - if (pos != line.npos) + if (pos != (int)line.npos) { if (line.substr(0, pos) == "name") { @@ -217,7 +217,7 @@ bool AnimatedSprite::load(std::string filePath) int pos = line.find("="); // Procesa las dos subcadenas - if (pos != line.npos) + if (pos != (int)line.npos) { if (line.substr(0, pos) == "frames_per_row") { diff --git a/source/asset.cpp b/source/asset.cpp index 761337f..ad0c69c 100644 --- a/source/asset.cpp +++ b/source/asset.cpp @@ -3,7 +3,7 @@ // Constructor Asset::Asset(std::string path) { - mExecutablePath = path; + executablePath = path; longest_name = 0; } @@ -16,10 +16,10 @@ Asset::~Asset() void Asset::add(std::string file, enum assetType type, bool required) { item_t temp; - temp.file = mExecutablePath + "/.." + file; + temp.file = executablePath + "/.." + file; temp.type = type; temp.required = required; - mFileList.push_back(temp); + fileList.push_back(temp); const std::string filename = file.substr(file.find_last_of("\\/") + 1); longest_name = SDL_max(longest_name, filename.size()); @@ -28,9 +28,13 @@ void Asset::add(std::string file, enum assetType type, bool required) // Devuelve el fichero de un elemento de la lista a partir de una cadena std::string Asset::get(std::string text) { - for (int i = 0; i < mFileList.size(); i++) - if (mFileList[i].file.find(text) != std::string::npos) - return mFileList[i].file; + for (auto f : fileList) + { + if (f.file.find(text) != std::string::npos) + { + return f.file; + } + } printf("Warning: file %s not found\n", text.c_str()); return ""; @@ -44,31 +48,43 @@ bool Asset::check() printf("\n** Checking files.\n"); // Comprueba la lista de ficheros clasificandolos por tipo - for (int type = 0; type < maxAssetType; type++) + for (int type = 0; type < maxAssetType; ++type) { // Comprueba si hay ficheros de ese tipo bool any = false; - for (int i = 0; i < mFileList.size(); i++) - if ((mFileList[i].required) && (mFileList[i].type == type)) + for (auto f : fileList) + { + if ((f.required) && (f.type == type)) + { any = true; + } + } // Si hay ficheros de ese tipo, comprueba si existen if (any) { printf("\n>> %s FILES\n", getTypeName(type).c_str()); - for (int i = 0; i < mFileList.size(); i++) - if ((mFileList[i].required) && (mFileList[i].type == type)) - success &= checkFile(mFileList[i].file); + for (auto f : fileList) + { + if ((f.required) && (f.type == type)) + { + success &= checkFile(f.file); + } + } } } // Resultado if (success) + { printf("\n** All files OK.\n\n"); + } else + { printf("\n** A file is missing. Exiting.\n\n"); + } return success; } @@ -104,30 +120,39 @@ std::string Asset::getTypeName(int type) case bitmap: return "BITMAP"; break; + case music: return "MUSIC"; break; + case sound: return "SOUND"; break; + case font: return "FONT"; break; + case lang: return "LANG"; break; + case data: return "DATA"; break; + case room: return "ROOM"; break; + case enemy: return "ENEMY"; break; + case item: return "ITEM"; break; + default: return "ERROR"; break; diff --git a/source/asset.h b/source/asset.h index ba8cb6c..242231b 100644 --- a/source/asset.h +++ b/source/asset.h @@ -35,8 +35,8 @@ private: int longest_name; // Contiene la longitud del nombre de fichero mas largo - std::vector mFileList; - std::string mExecutablePath; + std::vector fileList; + std::string executablePath; // Comprueba que existe un fichero bool checkFile(std::string path); diff --git a/source/easing.cpp b/source/easing.cpp deleted file mode 100644 index b8e3a31..0000000 --- a/source/easing.cpp +++ /dev/null @@ -1,214 +0,0 @@ -#include -#include - -#include "easing.h" - -#ifndef PI -#define PI 3.1415926545 -#endif - -double easeInSine( double t ) { - return sin( 1.5707963 * t ); -} - -double easeOutSine( double t ) { - return 1 + sin( 1.5707963 * (--t) ); -} - -double easeInOutSine( double t ) { - return 0.5 * (1 + sin( 3.1415926 * (t - 0.5) ) ); -} - -double easeInQuad( double t ) { - return t * t; -} - -double easeOutQuad( double t ) { - return t * (2 - t); -} - -double easeInOutQuad( double t ) { - return t < 0.5 ? 2 * t * t : t * (4 - 2 * t) - 1; -} - -double easeInCubic( double t ) { - return t * t * t; -} - -double easeOutCubic( double t ) { - return 1 + (--t) * t * t; -} - -double easeInOutCubic( double t ) { - return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t); -} - -double easeInQuart( double t ) { - t *= t; - return t * t; -} - -double easeOutQuart( double t ) { - t = (--t) * t; - return 1 - t * t; -} - -double easeInOutQuart( double t ) { - if( t < 0.5 ) { - t *= t; - return 8 * t * t; - } else { - t = (--t) * t; - return 1 - 8 * t * t; - } -} - -double easeInQuint( double t ) { - double t2 = t * t; - return t * t2 * t2; -} - -double easeOutQuint( double t ) { - double t2 = (--t) * t; - return 1 + t * t2 * t2; -} - -double easeInOutQuint( double t ) { - double t2; - if( t < 0.5 ) { - t2 = t * t; - return 16 * t * t2 * t2; - } else { - t2 = (--t) * t; - return 1 + 16 * t * t2 * t2; - } -} - -double easeInExpo( double t ) { - return (pow( 2, 8 * t ) - 1) / 255; -} - -double easeOutExpo( double t ) { - return 1 - pow( 2, -8 * t ); -} - -double easeInOutExpo( double t ) { - if( t < 0.5 ) { - return (pow( 2, 16 * t ) - 1) / 510; - } else { - return 1 - 0.5 * pow( 2, -16 * (t - 0.5) ); - } -} - -double easeInCirc( double t ) { - return 1 - sqrt( 1 - t ); -} - -double easeOutCirc( double t ) { - return sqrt( t ); -} - -double easeInOutCirc( double t ) { - if( t < 0.5 ) { - return (1 - sqrt( 1 - 2 * t )) * 0.5; - } else { - return (1 + sqrt( 2 * t - 1 )) * 0.5; - } -} - -double easeInBack( double t ) { - return t * t * (2.70158 * t - 1.70158); -} - -double easeOutBack( double t ) { - return 1 + (--t) * t * (2.70158 * t + 1.70158); -} - -double easeInOutBack( double t ) { - if( t < 0.5 ) { - return t * t * (7 * t - 2.5) * 2; - } else { - return 1 + (--t) * t * 2 * (7 * t + 2.5); - } -} - -double easeInElastic( double t ) { - double t2 = t * t; - return t2 * t2 * sin( t * PI * 4.5 ); -} - -double easeOutElastic( double t ) { - double t2 = (t - 1) * (t - 1); - return 1 - t2 * t2 * cos( t * PI * 4.5 ); -} - -double easeInOutElastic( double t ) { - double t2; - if( t < 0.45 ) { - t2 = t * t; - return 8 * t2 * t2 * sin( t * PI * 9 ); - } else if( t < 0.55 ) { - return 0.5 + 0.75 * sin( t * PI * 4 ); - } else { - t2 = (t - 1) * (t - 1); - return 1 - 8 * t2 * t2 * sin( t * PI * 9 ); - } -} - -double easeInBounce( double t ) { - return pow( 2, 6 * (t - 1) ) * abs( sin( t * PI * 3.5 ) ); -} - -double easeOutBounce( double t ) { - return 1 - pow( 2, -6 * t ) * abs( cos( t * PI * 3.5 ) ); -} - -double easeInOutBounce( double t ) { - if( t < 0.5 ) { - return 8 * pow( 2, 8 * (t - 1) ) * abs( sin( t * PI * 7 ) ); - } else { - return 1 - 8 * pow( 2, -8 * t ) * abs( sin( t * PI * 7 ) ); - } -} - -easingFunction getEasingFunction( easing_functions function ) -{ - static std::map< easing_functions, easingFunction > easingFunctions; - if( easingFunctions.empty() ) - { - easingFunctions.insert( std::make_pair( EaseInSine, easeInSine ) ); - easingFunctions.insert( std::make_pair( EaseOutSine, easeOutSine ) ); - easingFunctions.insert( std::make_pair( EaseInOutSine, easeInOutSine ) ); - easingFunctions.insert( std::make_pair( EaseInQuad, easeInQuad ) ); - easingFunctions.insert( std::make_pair( EaseOutQuad, easeOutQuad ) ); - easingFunctions.insert( std::make_pair( EaseInOutQuad, easeInOutQuad ) ); - easingFunctions.insert( std::make_pair( EaseInCubic, easeInCubic ) ); - easingFunctions.insert( std::make_pair( EaseOutCubic, easeOutCubic ) ); - easingFunctions.insert( std::make_pair( EaseInOutCubic, easeInOutCubic ) ); - easingFunctions.insert( std::make_pair( EaseInQuart, easeInQuart ) ); - easingFunctions.insert( std::make_pair( EaseOutQuart, easeOutQuart ) ); - easingFunctions.insert( std::make_pair( EaseInOutQuart, easeInOutQuart) ); - easingFunctions.insert( std::make_pair( EaseInQuint, easeInQuint ) ); - easingFunctions.insert( std::make_pair( EaseOutQuint, easeOutQuint ) ); - easingFunctions.insert( std::make_pair( EaseInOutQuint, easeInOutQuint ) ); - easingFunctions.insert( std::make_pair( EaseInExpo, easeInExpo ) ); - easingFunctions.insert( std::make_pair( EaseOutExpo, easeOutExpo ) ); - easingFunctions.insert( std::make_pair( EaseInOutExpo, easeInOutExpo ) ); - easingFunctions.insert( std::make_pair( EaseInCirc, easeInCirc ) ); - easingFunctions.insert( std::make_pair( EaseOutCirc, easeOutCirc ) ); - easingFunctions.insert( std::make_pair( EaseInOutCirc, easeInOutCirc ) ); - easingFunctions.insert( std::make_pair( EaseInBack, easeInBack ) ); - easingFunctions.insert( std::make_pair( EaseOutBack, easeOutBack ) ); - easingFunctions.insert( std::make_pair( EaseInOutBack, easeInOutBack ) ); - easingFunctions.insert( std::make_pair( EaseInElastic, easeInElastic ) ); - easingFunctions.insert( std::make_pair( EaseOutElastic, easeOutElastic ) ); - easingFunctions.insert( std::make_pair( EaseInOutElastic, easeInOutElastic ) ); - easingFunctions.insert( std::make_pair( EaseInBounce, easeInBounce ) ); - easingFunctions.insert( std::make_pair( EaseOutBounce, easeOutBounce ) ); - easingFunctions.insert( std::make_pair( EaseInOutBounce, easeInOutBounce ) ); - - } - - auto it = easingFunctions.find( function ); - return it == easingFunctions.end() ? nullptr : it->second; -} diff --git a/source/easing.h b/source/easing.h deleted file mode 100644 index 1a4938c..0000000 --- a/source/easing.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -enum easing_functions -{ - EaseInSine, - EaseOutSine, - EaseInOutSine, - EaseInQuad, - EaseOutQuad, - EaseInOutQuad, - EaseInCubic, - EaseOutCubic, - EaseInOutCubic, - EaseInQuart, - EaseOutQuart, - EaseInOutQuart, - EaseInQuint, - EaseOutQuint, - EaseInOutQuint, - EaseInExpo, - EaseOutExpo, - EaseInOutExpo, - EaseInCirc, - EaseOutCirc, - EaseInOutCirc, - EaseInBack, - EaseOutBack, - EaseInOutBack, - EaseInElastic, - EaseOutElastic, - EaseInOutElastic, - EaseInBounce, - EaseOutBounce, - EaseInOutBounce -}; - -typedef double(*easingFunction)(double); - -easingFunction getEasingFunction( easing_functions function ); - diff --git a/source/enemy_engine.cpp b/source/enemy_engine.cpp index 0f4e731..fbe230d 100644 --- a/source/enemy_engine.cpp +++ b/source/enemy_engine.cpp @@ -114,57 +114,84 @@ bool EnemyEngine::setEnemy(enemy_t *enemy, SDL_Point *p1, SDL_Point *p2, std::st { enemy->tileset = value; } + else if (var == "animation") { enemy->animation = value; } + else if (var == "width") { enemy->w = std::stof(value); } + else if (var == "height") { enemy->h = std::stof(value); } + else if (var == "x") { enemy->x = std::stof(value) * tile_size; } + else if (var == "y") { enemy->y = std::stof(value) * tile_size; } + else if (var == "vx") { enemy->vx = std::stof(value); } + else if (var == "vy") { enemy->vy = std::stof(value); } + else if (var == "x1") { p1->x = std::stoi(value) * tile_size; } + else if (var == "x2") { p2->x = std::stoi(value) * tile_size; } + else if (var == "y1") { p1->y = std::stoi(value) * tile_size; } + else if (var == "y2") { p2->y = std::stoi(value) * tile_size; } + else if (var == "[/path]") { } + else { success = false; } return success; +} + +// Comprueba las colisiones con los enemigos +bool EnemyEngine::checkEnemyCollision(SDL_Rect &rect) +{ + for (auto enemy : enemies) + { + if (checkCollision(rect, enemy->getCollider())) + { + return true; + } + } + + return false; } \ No newline at end of file diff --git a/source/enemy_engine.h b/source/enemy_engine.h index c1b05f4..ed1c989 100644 --- a/source/enemy_engine.h +++ b/source/enemy_engine.h @@ -41,6 +41,9 @@ public: // Actualiza las variables del objeto void update(); + + // Comprueba las colisiones con los enemigos + bool checkEnemyCollision(SDL_Rect &rect); }; #endif diff --git a/source/game.cpp b/source/game.cpp index 9733705..75d1b5e 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -9,14 +9,14 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D this->screen = screen; this->input = input; this->debug = debug; - //this->debug->setEnabled(true); + this->debug->setEnabled(true); // Reserva memoria para los objetos scoreboard = new ScoreBoard(renderer, asset, &board); eventHandler = new SDL_Event(); itemTracker = new ItemTracker(); map = new Map(asset->get("01.map"), renderer, asset, itemTracker); - player = new Player(renderer, asset, input, map); + player = new Player(renderer, asset, input, map, debug); enemyEngine = new EnemyEngine(renderer, asset, player, map, asset->get(map->getEnemyFile())); music = JA_LoadMusic(asset->get("music_surface.ogg").c_str()); @@ -93,14 +93,17 @@ void Game::update() if (input->checkAnyInput()) scoreboard->reset(); - board.diamonds = player->diamonds; - // Actualiza los objetos debug->clear(); scoreboard->update(); map->update(); enemyEngine->update(); player->update(); + if (enemyEngine->checkEnemyCollision(player->getCollider())) + { + debug->add("COLLISION"); + } + checkScreenBorders(); updateDebugInfo(); @@ -197,7 +200,7 @@ void Game::checkEventHandler() delete map; map = new Map(asset->get("01.map"), renderer, asset, itemTracker); delete player; - player = new Player(renderer, asset, input, map); + player = new Player(renderer, asset, input, map, debug); break; case SDL_SCANCODE_F: @@ -237,16 +240,6 @@ void Game::updateDebugInfo() { debug->add("R - Reload player and map"); debug->add("D - Toggle debug mode"); - debug->add(std::to_string((int)player->sprite->getPosX()) + "," + std::to_string((int)player->sprite->getPosY()) + "," + std::to_string((int)player->sprite->getWidth()) + "," + std::to_string((int)player->sprite->getHeight())); - debug->add("VY " + std::to_string(player->vy) + " " + std::to_string(player->jumpStrenght)); - debug->add("VX " + std::to_string(player->vx)); - debug->add("jump_pressed " + std::to_string(player->jumpPressed)); - debug->add("isOnFloor " + std::to_string(player->isOnFloor())); - debug->add("getTile(" + std::to_string(player->underFeet[0].x) + "," + std::to_string(player->underFeet[0].y) + ") = " + std::to_string(player->map->getTile(player->underFeet[0]))); - debug->add("state " + std::to_string(player->state)); - debug->add(map->getName() + " (" + map->getRoomFileName(b_top) + ", " + map->getRoomFileName(b_right) + ", " + map->getRoomFileName(b_bottom) + ", " + map->getRoomFileName(b_left) + ")"); - debug->add("hookedOn = " + std::to_string(player->hookedOnMovingPlatform)); - debug->add("DIAMONDS = " + std::to_string(player->diamonds)); } // Pone la información de debug en pantalla @@ -270,7 +263,7 @@ void Game::renderDebugInfo() // Pinta mascaras SDL_SetRenderDrawColor(renderer, 0, 255, 0, 192); - SDL_Rect rect = player->sprite->getRect(); + SDL_Rect rect = player->getRect(); SDL_RenderFillRect(renderer, &rect); // Pinta el texto diff --git a/source/game.h b/source/game.h index b2fd40f..bb23a4a 100644 --- a/source/game.h +++ b/source/game.h @@ -34,8 +34,8 @@ private: Debug *debug; // Objeto para gestionar la información de debug SDL_Texture *spriteLayer; // Textura para dibujar los sprites section_t section; // Seccion actual dentro del programa - int ticks; // Contador de ticks para ajustar la velocidad del programa - int ticksSpeed; // Velocidad a la que se repiten los bucles del programa + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa bool musicEnabled; // Indica si la musica puede sonar o no // Actualiza el juego, las variables, comprueba la entrada, etc. diff --git a/source/input.cpp b/source/input.cpp index 912c309..a479fb2 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -8,7 +8,7 @@ Input::Input(std::string file) dbPath = file; // Inicializa las variables - for (int i = 0; i < 17; i++) + for (int i = 0; i < 17; ++i) { keyBindings[i].scancode = 0; keyBindings[i].active = false; @@ -23,7 +23,7 @@ Input::Input(std::string file) // Destructor Input::~Input() { - for (int i = 0; i < numGamepads; i++) + for (int i = 0; i < numGamepads; ++i) connectedControllers[i] = nullptr; } @@ -143,7 +143,7 @@ bool Input::checkAnyInput(int device, int index) { const Uint8 *mKeystates = SDL_GetKeyboardState(NULL); - for (int i = 0; i < 17; i++) + for (int i = 0; i < 17; ++i) { if (mKeystates[keyBindings[i].scancode] != 0) { @@ -155,7 +155,7 @@ bool Input::checkAnyInput(int device, int index) if (gameControllerFound()) if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) { - for (int i = 0; i < 17; i++) + for (int i = 0; i < 17; ++i) { if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0) { @@ -182,7 +182,7 @@ bool Input::discoverGameController() numGamepads = 0; // Cuenta el numero de mandos - for (int i = 0; i < nJoysticks; i++) + for (int i = 0; i < nJoysticks; ++i) if (SDL_IsGameController(i)) numGamepads++; @@ -193,7 +193,7 @@ bool Input::discoverGameController() { found = true; - for (int i = 0; i < numGamepads; i++) + for (int i = 0; i < numGamepads; ++i) { // Abre el mando y lo añade a la lista SDL_GameController *pad = SDL_GameControllerOpen(i); diff --git a/source/intro.h b/source/intro.h index 643244d..c359730 100644 --- a/source/intro.h +++ b/source/intro.h @@ -22,8 +22,8 @@ private: Asset *asset; // Objeto con los ficheros de recurso AnimatedSprite *sprite; // Sprite para dibujar los graficos de la intro section_t section; // Estado del bucle principal para saber si continua o se sale - int ticks; // Contador de ticks para ajustar la velocidad del programa - int ticksSpeed; // Velocidad a la que se repiten los bucles del programa + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa // Actualiza las variables void update(); diff --git a/source/item_tracker.cpp b/source/item_tracker.cpp index ba4072f..3760825 100644 --- a/source/item_tracker.cpp +++ b/source/item_tracker.cpp @@ -57,31 +57,47 @@ void ItemTracker::addItem(std::string name, SDL_Point pos) // Busca una entrada en la lista por nombre int ItemTracker::findByName(std::string name) { - const int c = -1; - - for (int i = 0; i < list.size(); i++) + /*for (int i = 0; i < list.size(); ++i) { if (list[i].name == name) { return i; } + }*/ + + int i = 0; + for (auto l : list) + { + if (l.name == name) + { + return i; + } + i++; } - return c; + return -1; } // Busca una entrada en la lista por posición int ItemTracker::findByPos(int index, SDL_Point pos) { - const int c = -1; - - for (int i = 0; i < list[index].pos.size(); i++) + /*for (int i = 0; i < list[index].pos.size(); ++i) { if ((list[index].pos[i].x == pos.x) && (list[index].pos[i].y == pos.y)) { return i; } + }*/ + + int i = 0; + for (auto l : list[index].pos) + { + if ((l.x == pos.x) && (l.y == pos.y)) + { + return i; + } + i++; } - return c; + return -1; } \ No newline at end of file diff --git a/source/logo.h b/source/logo.h index 372f5ee..32d75a4 100644 --- a/source/logo.h +++ b/source/logo.h @@ -25,8 +25,8 @@ private: Sprite *sprite; // Sprite con la textura del logo int counter; // Contador section_t section; // Estado del bucle principal para saber si continua o se sale - int ticks; // Contador de ticks para ajustar la velocidad del programa - int ticksSpeed; // Velocidad a la que se repiten los bucles del programa + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa int initFade; // Tiempo del contador cuando inicia el fade a negro int endLogo; // Tiempo del contador para terminar el logo int postLogo; // Tiempo que dura el logo con el fade al maximo diff --git a/source/map.cpp b/source/map.cpp index a6cd24e..25b9b23 100644 --- a/source/map.cpp +++ b/source/map.cpp @@ -92,7 +92,7 @@ bool Map::load(std::string file_path) { std::getline(file2, line); pos = line.find("data encoding"); - } while (pos == std::string::npos); + } while (pos == (int)std::string::npos); do { // Se introducen los valores separados por comas en un vector @@ -117,7 +117,7 @@ bool Map::load(std::string file_path) { std::getline(file2, line); pos = line.find("data encoding"); - } while (pos == std::string::npos); + } while (pos == (int)std::string::npos); do { // Se introducen los valores separados por comas en un vector @@ -142,7 +142,7 @@ bool Map::load(std::string file_path) { std::getline(file2, line); pos = line.find("data encoding"); - } while (pos == std::string::npos); + } while (pos == (int)std::string::npos); do { // Se introducen los valores separados por comas en un vector @@ -438,7 +438,7 @@ void Map::fillMapTexture() // Dibuja el degradado de fondo const float num_lines = PLAY_AREA_BOTTOM - PLAY_AREA_TOP; - for (int i = PLAY_AREA_TOP; i < PLAY_AREA_BOTTOM; i++) + for (int i = PLAY_AREA_TOP; i < PLAY_AREA_BOTTOM; ++i) { float step = ((float)i / num_lines); int r = bgColor1.r + ((bgColor2.r - bgColor1.r) * step); @@ -449,8 +449,8 @@ void Map::fillMapTexture() } // Dibuja el mapeado de tiles - for (int y = 0; y < map_height; y++) - for (int x = 0; x < map_width; x++) + for (int y = 0; y < map_height; ++y) + for (int x = 0; x < map_width; ++x) { // Resta uno porque Tiled almacena los indices empezando de 1 en vez de 0. // El problema es que los tiles vacios los pone como 0 y aqui pasan a ser -1 @@ -469,8 +469,8 @@ void Map::fillMapTexture() SDL_RenderClear(renderer); // Dibuja el mapeado de tiles - for (int y = 0; y < map_height; y++) - for (int x = 0; x < map_width; x++) + for (int y = 0; y < map_height; ++y) + for (int x = 0; x < map_width; ++x) { // Resta uno porque Tiled almacena los indices empezando de 1 en vez de 0. // El problema es que los tiles vacios los pone como 0 y aqui pasan a ser -1 @@ -511,8 +511,8 @@ void Map::renderLayer0() } else { - SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT}; - SDL_RenderCopy(renderer, map_layer0, nullptr, nullptr); + //SDL_Rect rect = {PLAY_AREA_X, PLAY_AREA_Y, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT}; + SDL_RenderCopy(renderer, map_layer0, NULL, NULL); } } diff --git a/source/menu.cpp b/source/menu.cpp index 5037bf7..78a2627 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -451,7 +451,7 @@ void Menu::updateSelector() // Coloca el selector en una posición específica void Menu::setSelectorPos(int index) { - if (index < item.size()) + if (index < (int)item.size()) { selector.index = index; selector.rect.y = selector.y = selector.originY = selector.targetY = item[selector.index].rect.y; @@ -615,8 +615,8 @@ void Menu::render() SDL_RenderDrawRect(renderer, &rectBG.rect); } - // Renderitza el texto - for (int i = 0; i < item.size(); i++) + // Renderiza el texto + for (int i = 0; i < (int)item.size(); ++i) { if (i == selector.index) { @@ -852,7 +852,7 @@ void Menu::replaceElementsOnY() { item[0].rect.y = y; - for (int i = 1; i < item.size(); i++) + for (int i = 1; i < (int)item.size(); i++) { item[i].rect.y = item[i - 1].rect.y + item[i - 1].rect.h + item[i - 1].hPaddingDown; } diff --git a/source/player.cpp b/source/player.cpp index a005092..68f95d0 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -1,12 +1,13 @@ #include "player.h" // Constructor -Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map) +Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map, Debug *debug) { this->asset = asset; this->renderer = renderer; this->input = input; this->map = map; + this->debug = debug; sound_jump = JA_LoadSound(asset->get("sound_player_jump.wav").c_str()); sound_death = JA_LoadSound(asset->get("sound_player_death.wav").c_str()); @@ -40,6 +41,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map) underFeet.insert(underFeet.end(), {p, p, p}); hookedOnMovingPlatform = -1; diamonds = 0; + colliderBox = getRect(); } // Destructor @@ -62,6 +64,18 @@ void Player::update() move(); animate(); checkActors(); + colliderBox = getRect(); + + debug->add(std::to_string((int)sprite->getPosX()) + "," + std::to_string((int)sprite->getPosY()) + "," + std::to_string((int)sprite->getWidth()) + "," + std::to_string((int)sprite->getHeight())); + debug->add("VY " + std::to_string(vy) + " " + std::to_string(jumpStrenght)); + debug->add("VX " + std::to_string(vx)); + debug->add("jump_pressed " + std::to_string(jumpPressed)); + debug->add("isOnFloor " + std::to_string(isOnFloor())); + debug->add("getTile(" + std::to_string(underFeet[0].x) + "," + std::to_string(underFeet[0].y) + ") = " + std::to_string(map->getTile(underFeet[0]))); + debug->add("state " + std::to_string(state)); + debug->add(map->getName() + " (" + map->getRoomFileName(b_top) + ", " + map->getRoomFileName(b_right) + ", " + map->getRoomFileName(b_bottom) + ", " + map->getRoomFileName(b_left) + ")"); + debug->add("hookedOn = " + std::to_string(hookedOnMovingPlatform)); + debug->add("DIAMONDS = " + std::to_string(diamonds)); } // Dibuja el objeto @@ -75,23 +89,27 @@ void Player::checkInput() { if (input->checkInput(INPUT_LEFT, REPEAT_TRUE)) { - vx = std::max(vx -= accelX, -maxVX); + vx -= accelX; + vx = std::max(vx, -maxVX); sprite->setFlip(SDL_FLIP_HORIZONTAL); } else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) { - vx = std::min(vx += accelX, maxVX); + vx += accelX; + vx = std::min(vx, maxVX); sprite->setFlip(SDL_FLIP_NONE); } else { if (vx > 0.0f) { - vx = std::max(vx -= accelX, 0.0f); + vx -= accelX; + vx = std::max(vx, 0.0f); } else { - vx = std::min(vx += accelX, 0.0f); + vx += accelX; + vx = std::min(vx, 0.0f); } } @@ -113,7 +131,8 @@ void Player::checkInput() { if (jumpPressed) { - jumpStrenght = std::max(jumpStrenght -= 0.4f, 0.0f); + jumpStrenght -= 0.4f; + jumpStrenght = std::max(jumpStrenght, 0.0f); vy -= jumpStrenght; } } @@ -139,7 +158,8 @@ void Player::addGravity() // *** Falta ver pq la gravedad empuja al muñeco hacia abajo en los tiles atravesables if (state != s_standing) { - vy = std::min(vy += gravity, maxVY); + vy += gravity; + vy = std::min(vy, maxVY); } } @@ -397,31 +417,33 @@ bool Player::isOnMovingPlatform() // Comprueba si está situado en alguno de los cuatro bordes de la habitación bool Player::isOnScreenBorder() { - bool success = false; border = b_none; if (x < map->getPlayArea(b_left)) { border = b_left; - success = true; + return true; } + else if (x > map->getPlayArea(b_right) - w) { border = b_right; - success = true; + return true; } + else if (y < map->getPlayArea(b_top)) { border = b_top; - success = true; + return true; } + else if (y > map->getPlayArea(b_bottom) - h) { border = b_bottom; - success = true; + return true; } - return success; + return false; } // Devuelve el valor de la variable @@ -486,4 +508,16 @@ int Player::checkActors() void Player::reLoadTextures() { texture->reLoad(); +} + +// Devuelve el rectangulo que contiene al enemigo +SDL_Rect Player::getRect() +{ + return sprite->getRect(); +} + +// Obtiene el rectangulo de colision del enemigo +SDL_Rect &Player::getCollider() +{ + return colliderBox; } \ No newline at end of file diff --git a/source/player.h b/source/player.h index 172efa9..284202f 100644 --- a/source/player.h +++ b/source/player.h @@ -8,6 +8,7 @@ #include "asset.h" #include "map.h" #include "actor.h" +#include "debug.h" #ifndef PLAYER_H #define PLAYER_H @@ -29,13 +30,14 @@ enum e_floor // The player class Player { -public: +private: Asset *asset; // Objeto con la ruta a todos los ficheros de recursos SDL_Renderer *renderer; // El renderizador de la ventana Input *input; // Objeto Input para gestionar las entradas AnimatedSprite *sprite; // Objeto con los graficos, animaciones y posición del jugador LTexture *texture; // Textura con los graficos del jugador Map *map; // Objeto con el mapa + Debug *debug; // Objeto para gestionar la información de debug float x; // Posición del jugador en el eje X float y; // Posición del jugador en el eje Y @@ -48,6 +50,7 @@ public: SDL_Point lastPosition; // Posición anterior int hookedOnMovingPlatform; // Índice de la plataforma movil a la que está enganchado e_floor isOn; // Indica sobre que tipo de suelo se encuentra + SDL_Rect colliderBox; // Caja de colisión // Variables que afectan a la inercia del movimiento bool jumpPressed; // Indica si esta pulsada la tecla de salto @@ -97,7 +100,7 @@ public: public: // Constructor - Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map); + Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map, Debug *debug); // Destructor ~Player(); @@ -122,6 +125,12 @@ public: // Recarga las texturas void reLoadTextures(); + + // Devuelve el rectangulo que contiene al jugador + SDL_Rect getRect(); + + // Obtiene el rectangulo de colision del jugador + SDL_Rect &getCollider(); }; #endif diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index fc5555d..2d179a1 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -54,7 +54,7 @@ void ScoreBoard::fillTexture() // Pinta el degradado const float num_lines = (SCOREBOARD_BOTTOM / 2) - SCOREBOARD_TOP; - for (int i = SCOREBOARD_TOP; i < SCOREBOARD_TOP + num_lines; i++) + for (int i = SCOREBOARD_TOP; i < SCOREBOARD_TOP + num_lines; ++i) { float step = ((float)i / num_lines); int alpha = 64 + ((0 - 64) * step); diff --git a/source/scoreboard.h b/source/scoreboard.h index cbc560e..a8ede12 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -6,7 +6,6 @@ #include "asset.h" #include "sprite.h" #include "const.h" -#include "easing.h" #include #ifndef SCOREBOARD_H diff --git a/source/text.cpp b/source/text.cpp index d7ed473..653a156 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -26,7 +26,7 @@ Text::~Text() void Text::init() { // Inicializa a cero el vector con las coordenadas - for (int i = 0; i < 128; i++) + for (int i = 0; i < 128; ++i) { offset[i].x = 0; offset[i].y = 0; @@ -44,7 +44,7 @@ void Text::init() sprite->setSpriteClip(0, 0, sprite->getWidth(), sprite->getHeight()); // Establece las coordenadas para cada caracter ascii de la cadena y su ancho - for (int i = 32; i < 128; i++) + for (int i = 32; i < 128; ++i) { offset[i].x = ((i - 32) % 15) * boxWidth; offset[i].y = ((i - 32) / 15) * boxHeight; @@ -131,7 +131,7 @@ int Text::lenght(std::string text, int kerning) { int shift = 0; - for (int i = 0; i < (int)text.length(); i++) + for (int i = 0; i < (int)text.length(); ++i) shift += (offset[int(text[i])].w + kerning); // Descuenta el kerning del último caracter diff --git a/source/title.h b/source/title.h index d9cccc9..9e92423 100644 --- a/source/title.h +++ b/source/title.h @@ -29,8 +29,8 @@ private: AnimatedSprite *sprite; // Sprite para dibujar los graficos de la intro JA_Music music; // Musica del titulo del juego section_t section; // Estado del bucle principal para saber si continua o se sale - int ticks; // Contador de ticks para ajustar la velocidad del programa - int ticksSpeed; // Velocidad a la que se repiten los bucles del programa + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa // Actualiza las variables void update();