Colisiones básicas completadas
This commit is contained in:
@@ -90,13 +90,13 @@ void AnimatedSprite::setAnimationLoop(std::string name, bool loop)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Establece el valor de la variable
|
// 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;
|
animation[getIndex(name)].completed = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba si ha terminado la animación
|
// Comprueba si ha terminado la animación
|
||||||
bool AnimatedSprite::isCompleted(std::string name)
|
bool AnimatedSprite::animationIsCompleted(std::string name)
|
||||||
{
|
{
|
||||||
return animation[getIndex(name)].completed;
|
return animation[getIndex(name)].completed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ public:
|
|||||||
void setAnimationLoop(std::string name, bool loop);
|
void setAnimationLoop(std::string name, bool loop);
|
||||||
|
|
||||||
// Establece el valor de la variable
|
// 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
|
// 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
|
// Devuelve el rectangulo de una animación y frame concreto
|
||||||
SDL_Rect getAnimationClip(std::string name, Uint8 index);
|
SDL_Rect getAnimationClip(std::string name, Uint8 index);
|
||||||
|
|||||||
@@ -115,4 +115,7 @@ void Game::renderDebugInfo()
|
|||||||
|
|
||||||
text = std::to_string((int)player->sprite->getPosX()) + "," + std::to_string((int)player->sprite->getPosY());
|
text = std::to_string((int)player->sprite->getPosX()) + "," + std::to_string((int)player->sprite->getPosY());
|
||||||
debugText->write(0, line, text, -1);
|
debugText->write(0, line, text, -1);
|
||||||
|
|
||||||
|
//text = std::to_string(player->checkMapCollisions());
|
||||||
|
//debugText->write(0, line+=6, text, -1);
|
||||||
}
|
}
|
||||||
@@ -224,12 +224,13 @@ void Map::render()
|
|||||||
t_tile_map Map::getTile(SDL_Point p)
|
t_tile_map Map::getTile(SDL_Point p)
|
||||||
{
|
{
|
||||||
const int tile = tilemap[((p.y / tile_width) * map_width) + (p.x / tile_width)];
|
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;
|
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;
|
return wall;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ private:
|
|||||||
SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación
|
SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación
|
||||||
|
|
||||||
int tile_width; // Ancho del tile en pixels
|
int tile_width; // Ancho del tile en pixels
|
||||||
int map_width; // Alto del mapa en tiles
|
int map_width; // Ancho del mapa en tiles
|
||||||
int map_height; // Ancho del mapa en tiles
|
int map_height; // Alto del mapa en tiles
|
||||||
|
|
||||||
// Carga las variables desde un fichero
|
// Carga las variables desde un fichero
|
||||||
bool load(std::string file);
|
bool load(std::string file);
|
||||||
|
|||||||
@@ -158,6 +158,13 @@ double MovingSprite::getAngle()
|
|||||||
return mAngle;
|
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
|
// Establece el valor de la variable
|
||||||
void MovingSprite::setPosX(float x)
|
void MovingSprite::setPosX(float x)
|
||||||
{
|
{
|
||||||
@@ -315,3 +322,21 @@ void MovingSprite::undoMove()
|
|||||||
mPosX = mPosXPrev;
|
mPosX = mPosXPrev;
|
||||||
mPosY = mPosYPrev;
|
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;
|
||||||
|
}
|
||||||
@@ -87,6 +87,9 @@ public:
|
|||||||
// Obtiene el valor de la variable
|
// Obtiene el valor de la variable
|
||||||
Uint16 getRotateSpeed();
|
Uint16 getRotateSpeed();
|
||||||
|
|
||||||
|
// Establece la posición del objeto
|
||||||
|
void setPos(SDL_Rect rect);
|
||||||
|
|
||||||
// Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void setPosX(float x);
|
void setPosX(float x);
|
||||||
|
|
||||||
@@ -146,6 +149,15 @@ public:
|
|||||||
|
|
||||||
// Deshace el último movimiento
|
// Deshace el último movimiento
|
||||||
void undoMove();
|
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
|
#endif
|
||||||
|
|||||||
@@ -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 = new AnimatedSprite(texture, renderer, asset->get("player.ani"));
|
||||||
|
|
||||||
sprite->setPosX(16);
|
x = 16;
|
||||||
sprite->setPosY(0);
|
y = 40;
|
||||||
|
vx = 0;
|
||||||
|
vy = 0;
|
||||||
|
const SDL_Rect rect = {(int)x, (int)y, 16, 24};
|
||||||
|
sprite->setPos(rect);
|
||||||
sprite->setCurrentAnimation("stand");
|
sprite->setCurrentAnimation("stand");
|
||||||
|
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
||||||
|
|
||||||
gravity = 0.5f;
|
gravity = 0.5f;
|
||||||
can_jump = true;
|
can_jump = true;
|
||||||
@@ -53,8 +58,7 @@ void Player::update()
|
|||||||
{
|
{
|
||||||
checkInput();
|
checkInput();
|
||||||
addGravity();
|
addGravity();
|
||||||
sprite->update();
|
move();
|
||||||
updateColliders();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dibuja el objeto
|
// Dibuja el objeto
|
||||||
@@ -70,19 +74,19 @@ void Player::checkInput()
|
|||||||
// Solo comprueba las entradas de dirección cuando está de pie
|
// Solo comprueba las entradas de dirección cuando está de pie
|
||||||
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
|
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
|
||||||
{
|
{
|
||||||
sprite->setVelX(-speed);
|
vx = -speed;
|
||||||
sprite->setFlip(SDL_FLIP_NONE);
|
sprite->setFlip(SDL_FLIP_NONE);
|
||||||
sprite->setCurrentAnimation("walk");
|
sprite->setCurrentAnimation("walk");
|
||||||
}
|
}
|
||||||
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
|
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
|
||||||
{
|
{
|
||||||
sprite->setVelX(speed);
|
vx = speed;
|
||||||
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
||||||
sprite->setCurrentAnimation("walk");
|
sprite->setCurrentAnimation("walk");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sprite->setVelX(0);
|
vx = 0;
|
||||||
sprite->setCurrentAnimation("stand");
|
sprite->setCurrentAnimation("stand");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,13 +94,13 @@ void Player::checkInput()
|
|||||||
// Aplica la gravedad
|
// Aplica la gravedad
|
||||||
void Player::addGravity()
|
void Player::addGravity()
|
||||||
{
|
{
|
||||||
sprite->setVelY(gravity);
|
vy = gravity;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza los puntos de colisión
|
// Actualiza los puntos de colisión
|
||||||
void Player::updateColliders()
|
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[0] = p;
|
||||||
collider[1] = {p.x, p.y + 12};
|
collider[1] = {p.x, p.y + 12};
|
||||||
@@ -107,7 +111,7 @@ void Player::updateColliders()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compruena las colisiones con el mapa
|
// Compruena las colisiones con el mapa
|
||||||
void Player::checkMapCollisions()
|
bool Player::checkMapCollisions()
|
||||||
{
|
{
|
||||||
bool collision = false;
|
bool collision = false;
|
||||||
|
|
||||||
@@ -116,14 +120,29 @@ void Player::checkMapCollisions()
|
|||||||
collision |= (map->getTile(c) == wall);
|
collision |= (map->getTile(c) == wall);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (collision)
|
return collision;
|
||||||
{
|
|
||||||
undoMove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deshace el último movimiento
|
// Mueve al jugador en función de la velocidad/desplazamiento
|
||||||
void Player::undoMove()
|
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();
|
||||||
}
|
}
|
||||||
@@ -21,20 +21,24 @@ public:
|
|||||||
LTexture *texture; // Textura con los graficos del jugador
|
LTexture *texture; // Textura con los graficos del jugador
|
||||||
Map *map; // Objeto con el mapa
|
Map *map; // Objeto con el mapa
|
||||||
|
|
||||||
bool can_jump; // Si puede saltar
|
float x; // Posición del jugador en el eje X
|
||||||
bool enabled; // Si está habilitado
|
float y; // Posición del jugador en el eje Y
|
||||||
bool standing; // Si esta de pie (o quieto?)
|
float vx; // Velocidad/desplazamiento del jugador en el eje X
|
||||||
bool invulnerable; // Si es invulnerable
|
float vy; // Velocidad/desplazamiento del jugador en el eje Y
|
||||||
int coins; // Cantidad de monedas
|
bool can_jump; // Si puede saltar
|
||||||
int cooldown; // Tiempo de inhabilitación
|
bool enabled; // Si está habilitado
|
||||||
int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
|
bool standing; // Si esta de pie (o quieto?)
|
||||||
int lifes; // Cantidad de vidas
|
bool invulnerable; // Si es invulnerable
|
||||||
float gravity; // Gravedad
|
int coins; // Cantidad de monedas
|
||||||
std::vector<bool> key; // Indica las llaves que posee el jugador
|
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<bool> key; // Indica las llaves que posee el jugador
|
||||||
std::vector<SDL_Point> collider; // Contiene los puntos de colisión del jugador con el mapa
|
std::vector<SDL_Point> collider; // Contiene los puntos de colisión del jugador con el mapa
|
||||||
JA_Sound sound_coin; // Sonido al coger monedas
|
JA_Sound sound_coin; // Sonido al coger monedas
|
||||||
JA_Sound sound_death; // Sonido al morir
|
JA_Sound sound_death; // Sonido al morir
|
||||||
JA_Sound sound_jump; // Sonido al saltar
|
JA_Sound sound_jump; // Sonido al saltar
|
||||||
|
|
||||||
// Comprueba las entradas y modifica variables
|
// Comprueba las entradas y modifica variables
|
||||||
void checkInput();
|
void checkInput();
|
||||||
@@ -46,10 +50,10 @@ public:
|
|||||||
void updateColliders();
|
void updateColliders();
|
||||||
|
|
||||||
// Compruena las colisiones con el mapa
|
// Compruena las colisiones con el mapa
|
||||||
void checkMapCollisions();
|
bool checkMapCollisions();
|
||||||
|
|
||||||
// Deshace el último movimiento
|
// Mueve al jugador en función de la velocidad/desplazamiento
|
||||||
void undoMove();
|
void move();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
|
|||||||
@@ -87,6 +87,13 @@ int Sprite::getHeight()
|
|||||||
return mHeight;
|
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
|
// Establece el valor de la variable
|
||||||
void Sprite::setPosX(int x)
|
void Sprite::setPosX(int x)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -43,6 +43,9 @@ public:
|
|||||||
// Obten el valor de la variable
|
// Obten el valor de la variable
|
||||||
int getHeight();
|
int getHeight();
|
||||||
|
|
||||||
|
// Establece la posición del objeto
|
||||||
|
void setPos(SDL_Rect rect);
|
||||||
|
|
||||||
// Establece el valor de la variable
|
// Establece el valor de la variable
|
||||||
void setPosX(int x);
|
void setPosX(int x);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user