Colisiones básicas completadas

This commit is contained in:
2022-08-17 13:26:22 +02:00
parent a6d6f2854e
commit 9bd1e9b936
11 changed files with 115 additions and 41 deletions

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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)
{
@@ -315,3 +322,21 @@ 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;
}

View File

@@ -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

View File

@@ -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();
}

View File

@@ -21,6 +21,10 @@ public:
LTexture *texture; // Textura con los graficos del jugador
Map *map; // Objeto con el mapa
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?)
@@ -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

View File

@@ -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)
{

View File

@@ -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);