Trabajando en las colisiones con los enemigos
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user