diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index dcef260..09f71dd 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -90,13 +90,13 @@ void AnimatedSprite::setAnimationLoop(std::string name, bool loop) } // Establece el valor de la variable -void AnimatedSprite::setCompleted(std::string name, bool value) +void AnimatedSprite::setAnimationCompleted(std::string name, bool value) { animation[getIndex(name)].completed = value; } // Comprueba si ha terminado la animación -bool AnimatedSprite::isCompleted(std::string name) +bool AnimatedSprite::animationIsCompleted(std::string name) { return animation[getIndex(name)].completed; } diff --git a/source/animatedsprite.h b/source/animatedsprite.h index ad8881e..55d9981 100644 --- a/source/animatedsprite.h +++ b/source/animatedsprite.h @@ -49,10 +49,10 @@ public: void setAnimationLoop(std::string name, bool loop); // Establece el valor de la variable - void setCompleted(std::string name, bool value); + void setAnimationCompleted(std::string name, bool value); // Comprueba si ha terminado la animación - bool isCompleted(std::string name); + bool animationIsCompleted(std::string name); // Devuelve el rectangulo de una animación y frame concreto SDL_Rect getAnimationClip(std::string name, Uint8 index); diff --git a/source/game.cpp b/source/game.cpp index 3af1ce3..ba743d0 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -115,4 +115,7 @@ void Game::renderDebugInfo() text = std::to_string((int)player->sprite->getPosX()) + "," + std::to_string((int)player->sprite->getPosY()); debugText->write(0, line, text, -1); + + //text = std::to_string(player->checkMapCollisions()); + //debugText->write(0, line+=6, text, -1); } \ No newline at end of file diff --git a/source/map.cpp b/source/map.cpp index fb4376c..70b5a19 100644 --- a/source/map.cpp +++ b/source/map.cpp @@ -224,12 +224,13 @@ void Map::render() t_tile_map Map::getTile(SDL_Point p) { const int tile = tilemap[((p.y / tile_width) * map_width) + (p.x / tile_width)]; + const int png_width = 16; - if (tile > 0 && tile < 4 * map_width * tile_width) + if (tile >= 0 && tile < 4 * png_width) { return nothing; } - else if (tile > (4 * map_width * tile_width) && tile < 8 * map_width * tile_width) + else if (tile >= (4 * png_width) && tile < 8 * png_width) { return wall; } diff --git a/source/map.h b/source/map.h index a8f0807..0ab2064 100644 --- a/source/map.h +++ b/source/map.h @@ -37,8 +37,8 @@ private: SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación int tile_width; // Ancho del tile en pixels - int map_width; // Alto del mapa en tiles - int map_height; // Ancho del mapa en tiles + int map_width; // Ancho del mapa en tiles + int map_height; // Alto del mapa en tiles // Carga las variables desde un fichero bool load(std::string file); diff --git a/source/movingsprite.cpp b/source/movingsprite.cpp index f2d7a67..14fc078 100644 --- a/source/movingsprite.cpp +++ b/source/movingsprite.cpp @@ -158,6 +158,13 @@ double MovingSprite::getAngle() return mAngle; } +// Establece la posición del objeto +void MovingSprite::setPos(SDL_Rect rect) +{ + mPosX = (float)rect.x; + mPosY = (float)rect.y; +} + // Establece el valor de la variable void MovingSprite::setPosX(float x) { @@ -314,4 +321,22 @@ void MovingSprite::undoMove() { mPosX = mPosXPrev; mPosY = mPosYPrev; +} + +// Deshace el último movimiento en el eje X +void MovingSprite::undoMoveX() +{ + mPosX = mPosXPrev; +} + +// Deshace el último movimiento en el eje Y +void MovingSprite::undoMoveY() +{ + mPosY = mPosYPrev; +} + +// Pone a cero las velocidades de desplacamiento +void MovingSprite::clearVel() +{ + mVelX = mVelY = 0; } \ No newline at end of file diff --git a/source/movingsprite.h b/source/movingsprite.h index 055fa13..709f95f 100644 --- a/source/movingsprite.h +++ b/source/movingsprite.h @@ -87,6 +87,9 @@ public: // Obtiene el valor de la variable Uint16 getRotateSpeed(); + // Establece la posición del objeto + void setPos(SDL_Rect rect); + // Establece el valor de la variable void setPosX(float x); @@ -146,6 +149,15 @@ public: // Deshace el último movimiento void undoMove(); + + // Deshace el último movimiento en el eje X + void undoMoveX(); + + // Deshace el último movimiento en el eje Y + void undoMoveY(); + + // Pone a cero las velocidades de desplacamiento + void clearVel(); }; #endif diff --git a/source/player.cpp b/source/player.cpp index 041c42b..20d3220 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -17,9 +17,14 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map) sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani")); - sprite->setPosX(16); - sprite->setPosY(0); + x = 16; + y = 40; + vx = 0; + vy = 0; + const SDL_Rect rect = {(int)x, (int)y, 16, 24}; + sprite->setPos(rect); sprite->setCurrentAnimation("stand"); + sprite->setFlip(SDL_FLIP_HORIZONTAL); gravity = 0.5f; can_jump = true; @@ -53,8 +58,7 @@ void Player::update() { checkInput(); addGravity(); - sprite->update(); - updateColliders(); + move(); } // Dibuja el objeto @@ -70,19 +74,19 @@ void Player::checkInput() // Solo comprueba las entradas de dirección cuando está de pie if (input->checkInput(INPUT_LEFT, REPEAT_TRUE)) { - sprite->setVelX(-speed); + vx = -speed; sprite->setFlip(SDL_FLIP_NONE); sprite->setCurrentAnimation("walk"); } else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) { - sprite->setVelX(speed); + vx = speed; sprite->setFlip(SDL_FLIP_HORIZONTAL); sprite->setCurrentAnimation("walk"); } else { - sprite->setVelX(0); + vx = 0; sprite->setCurrentAnimation("stand"); } } @@ -90,13 +94,13 @@ void Player::checkInput() // Aplica la gravedad void Player::addGravity() { - sprite->setVelY(gravity); + vy = gravity; } // Actualiza los puntos de colisión void Player::updateColliders() { - const SDL_Point p = {(int)sprite->getPosX(), (int)sprite->getPosY()}; + const SDL_Point p = {(int)x, (int)y}; collider[0] = p; collider[1] = {p.x, p.y + 12}; @@ -107,7 +111,7 @@ void Player::updateColliders() } // Compruena las colisiones con el mapa -void Player::checkMapCollisions() +bool Player::checkMapCollisions() { bool collision = false; @@ -116,14 +120,29 @@ void Player::checkMapCollisions() collision |= (map->getTile(c) == wall); } - if (collision) - { - undoMove(); - } + return collision; } -// Deshace el último movimiento -void Player::undoMove() +// Mueve al jugador en función de la velocidad/desplazamiento +void Player::move() { - sprite->undoMove(); + const float old_x = x; + x += vx; + updateColliders(); + if (checkMapCollisions()) + { + x = old_x; + } + + const float old_y = y; + y += vy; + updateColliders(); + if (checkMapCollisions()) + { + y = old_y; + } + + sprite->setPosX(x); + sprite->setPosY(y); + sprite->update(); } \ No newline at end of file diff --git a/source/player.h b/source/player.h index 52b000b..ad21266 100644 --- a/source/player.h +++ b/source/player.h @@ -21,20 +21,24 @@ public: LTexture *texture; // Textura con los graficos del jugador Map *map; // Objeto con el mapa - bool can_jump; // Si puede saltar - bool enabled; // Si está habilitado - bool standing; // Si esta de pie (o quieto?) - bool invulnerable; // Si es invulnerable - int coins; // Cantidad de monedas - int cooldown; // Tiempo de inhabilitación - int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar - int lifes; // Cantidad de vidas - float gravity; // Gravedad - std::vector key; // Indica las llaves que posee el jugador + float x; // Posición del jugador en el eje X + float y; // Posición del jugador en el eje Y + float vx; // Velocidad/desplazamiento del jugador en el eje X + float vy; // Velocidad/desplazamiento del jugador en el eje Y + bool can_jump; // Si puede saltar + bool enabled; // Si está habilitado + bool standing; // Si esta de pie (o quieto?) + bool invulnerable; // Si es invulnerable + int coins; // Cantidad de monedas + int cooldown; // Tiempo de inhabilitación + int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar + int lifes; // Cantidad de vidas + float gravity; // Gravedad + std::vector key; // Indica las llaves que posee el jugador std::vector collider; // Contiene los puntos de colisión del jugador con el mapa - JA_Sound sound_coin; // Sonido al coger monedas - JA_Sound sound_death; // Sonido al morir - JA_Sound sound_jump; // Sonido al saltar + JA_Sound sound_coin; // Sonido al coger monedas + JA_Sound sound_death; // Sonido al morir + JA_Sound sound_jump; // Sonido al saltar // Comprueba las entradas y modifica variables void checkInput(); @@ -46,10 +50,10 @@ public: void updateColliders(); // Compruena las colisiones con el mapa - void checkMapCollisions(); + bool checkMapCollisions(); - // Deshace el último movimiento - void undoMove(); + // Mueve al jugador en función de la velocidad/desplazamiento + void move(); public: // Constructor diff --git a/source/sprite.cpp b/source/sprite.cpp index 8a31493..e907e57 100644 --- a/source/sprite.cpp +++ b/source/sprite.cpp @@ -87,6 +87,13 @@ int Sprite::getHeight() return mHeight; } +// Establece la posición del objeto +void Sprite::setPos(SDL_Rect rect) +{ + mPosX = rect.x; + mPosY = rect.y; +} + // Establece el valor de la variable void Sprite::setPosX(int x) { diff --git a/source/sprite.h b/source/sprite.h index b8f08f2..dc5e689 100644 --- a/source/sprite.h +++ b/source/sprite.h @@ -43,6 +43,9 @@ public: // Obten el valor de la variable int getHeight(); + // Establece la posición del objeto + void setPos(SDL_Rect rect); + // Establece el valor de la variable void setPosX(int x);