Sonidos de saltar, caer y morir

This commit is contained in:
2022-09-11 18:20:08 +02:00
parent 37daa9a8a0
commit 3eab857267
9 changed files with 117 additions and 235 deletions

View File

@@ -184,6 +184,8 @@ bool Director::setFileList()
asset->add("/media/enemies/wave.ani", data);
asset->add("/media/enemies/sigmasua.png", bitmap);
asset->add("/media/enemies/sigmasua.ani", data);
asset->add("/media/enemies/diskette.png", bitmap);
asset->add("/media/enemies/diskette.ani", data);
asset->add("/media/player/player.png", bitmap);
asset->add("/media/player/player.ani", data);

View File

@@ -79,8 +79,6 @@ void Game::checkEventHandler()
}
else if ((eventHandler->type == SDL_KEYDOWN) and (eventHandler->key.repeat == 0))
{
//test->update();
switch (eventHandler->key.keysym.scancode)
{
case SDL_SCANCODE_ESCAPE:

View File

@@ -22,7 +22,7 @@ Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Ren
border = BORDER_TOP;
invincible = false;
jump_ini = ini.jump_ini;
jumpIni = ini.jumpIni;
state = ini.state;
prevState = state;
@@ -74,7 +74,24 @@ Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Ren
jumpSound.push_back(JA_LoadSound(asset->get("jump23.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset->get("jump24.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump11.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump12.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump13.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump14.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump15.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump16.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump17.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump18.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump19.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump20.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump21.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump22.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump23.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset->get("jump24.wav").c_str()));
r = {0, 0, 0, 0};
jumpCounter = 0;
fallCounter = 0;
}
// Destructor
@@ -153,8 +170,8 @@ void Player::checkInput()
{
setState(s_jumping);
vy = -maxVY;
jump_ini = y;
JA_PlaySound(jumpSound[0]);
jumpIni = y;
jumpCounter = 0;
}
}
@@ -207,11 +224,21 @@ void Player::checkState()
{
vx = 0.0f;
vy = maxVY;
fallCounter++;
playFallSound();
}
else if (state == s_standing)
{
vy = 0.0f;
jumpCounter = 0;
fallCounter = 0;
}
else if (state == s_jumping)
{
jumpCounter++;
playJumpSound();
}
}
@@ -221,7 +248,7 @@ void Player::switchBorders()
if (border == BORDER_TOP)
{
y = PLAY_AREA_BOTTOM - h - 1;
jump_ini += 128;
jumpIni += 128;
}
else if (border == BORDER_BOTTOM)
{
@@ -424,147 +451,6 @@ void Player::move()
}
}
//--------------------------------------------------------------
// Calcula la nueva posición del jugador y compensa en caso de colisión
/*x += vx;
// Comprueba colisiones con muros
if (checkWalls())
{
// Recoloca
if (vx > 0.0f)
{
x = (int)x - ((int)x + w) % tileSize;
}
else
{
x = (int)x + tileSize - ((int)x % tileSize);
}
}
// Comprueba colisiones con rampas solo si esta quieto o cayendo
else if (vy >= 0.0f)
{
tile_e slope = checkSlopes();
tile_e slope2 = checkSlopes2();
debug->add("SLOPE = " + std::to_string(slope));
debug->add("SLOPE2 = " + std::to_string(slope2));
if (slope != t_empty)
{ // Cuesta hacia la derecha
if (slope == t_slope_r)
{ // Recoloca
y = -h + room->getSlopeHeight(feet[1], t_slope_r);
debug->add("feet[1] = " + std::to_string(feet[1].y));
}
// Cuesta hacia la izquierda
if (slope == t_slope_l)
{ // Recoloca
y = -h + room->getSlopeHeight(feet[0], t_slope_l);
debug->add("feet[0] = " + std::to_string(feet[0].y));
}
}
else if (slope2 != t_empty)
{ // Cuesta hacia la derecha
if (slope2 == t_slope_r)
{ // Recoloca
y = -h + room->getSlopeHeight(underFeet[1], t_slope_r);
debug->add("ufeet[1] = " + std::to_string(underFeet[1].y));
}
// Cuesta hacia la izquierda
if (slope2 == t_slope_l)
{ // Recoloca
y = -h + room->getSlopeHeight(underFeet[0], t_slope_l);
debug->add("ufeet[0] = " + std::to_string(underFeet[0].y));
}
}
}
y += vy;
if (checkWalls())
{
// Recoloca
if (vy > 0.0f)
{ // Bajando
y -= ((int)y + h) % tileSize;
setState(s_standing);
vy = 0.0f;
}
else
{ // Subiendo
y += tileSize - ((int)y % tileSize);
setState(s_falling);
vy = maxVY;
}
}
else
// Si no colisiona con los muros, haz comprobaciones extra
{
const int a = (lastPosition.y + h - 1) / tileSize;
const int b = ((int)y + h - 1) / tileSize;
const bool tile_change = a != b;
bool going_down = vy >= 0.0f;
bool tile_aligned = ((int)y + h) % tileSize == 0;
// Si está cayendo y hay cambio de tile o está justo sobre uno
if (going_down && (tile_aligned || tile_change))
{
// Comprueba si tiene uno de los pies sobre una superficie
if (isOnFloor())
{ // Y deja al jugador de pie
setState(s_standing);
vy = 0.0f;
// Si ademas ha habido un cambio de tile recoloca al jugador
if (tile_change && !checkSlopes2())
{
y = ((int)y - ((int)y % tileSize));
}
}
// Si tiene ambos pies sobre el vacío y no está saltando
else if (state != s_jumping)
{
setState(s_falling);
vy = maxVY;
}
}
going_down = vy >= 0.0f;
tile_aligned = ((int)y + h) % tileSize == 0;
// Si simplemente está cayendo (sin mirar si hay cambio de tile o si está justo sobre uno)
if (going_down)
{
if (state != s_jumping)
{
setState(s_falling);
vy = maxVY;
}
// Si está alineado con el tile mira el suelo (para que no lo mire si está
// dentro de un tile atravesable y lo deje a medias)
if (tile_aligned)
{
if (isOnFloor())
{
setState(s_standing);
vy = 0.0f;
}
}
// EXPERIMENTAL
else if (checkSlopes())
{
setState(s_standing);
vy = 0.0f;
}
}
}*/
// Actualiza la posición del sprite
sprite->setPosX(x);
sprite->setPosY(y);
@@ -583,12 +469,37 @@ void Player::animate()
void Player::checkJumpEnd()
{
if (state == s_jumping)
{
if (vy > 0)
if (y >= jump_ini)
{
if (y >= jumpIni)
{ // Si alcanza la altura de salto inicial, pasa al estado de caída
setState(s_falling);
vy = maxVY;
jumpCounter = 0;
}
}
}
}
// Calcula y reproduce el sonido de salto
void Player::playJumpSound()
{
if (jumpCounter % 4 == 0)
{
JA_PlaySound(jumpSound[jumpCounter / 4]);
}
debug->add("JUMP: " + std::to_string(jumpCounter / 4));
}
// Calcula y reproduce el sonido de caer
void Player::playFallSound()
{
if (fallCounter % 4 == 0)
{
JA_PlaySound(fallSound[std::min((fallCounter / 4), (int)fallSound.size() - 1)]);
}
debug->add("FALL: " + std::to_string(fallCounter / 4));
}
// Comprueba si el jugador tiene suelo debajo de los pies
@@ -657,73 +568,6 @@ bool Player::checkWalls()
return wall;
}
// Comprueba si el jugador está en una rampa
tile_e Player::checkSlopes()
{
// Actualiza los puntos de colisión
updateFeet();
bool slope_l = false;
bool slope_r = false;
// Comprueba si ha colisionado con una rampa
for (auto f : feet)
{
slope_l |= (room->getTile(f) == t_slope_l);
slope_r |= (room->getTile(f) == t_slope_r);
}
if (slope_l)
{
return t_slope_l;
}
if (slope_r)
{
return t_slope_r;
}
return t_empty;
}
// Comprueba si el jugador está en una rampa
tile_e Player::checkSlopes2()
{
// Actualiza los puntos de colisión
updateFeet();
bool slope_l = false;
bool slope_r = false;
bool wall = false;
bool passable = false;
// Comprueba si ha colisionado con una rampa
for (auto f : underFeet)
{
slope_l |= (room->getTile(f) == t_slope_l);
slope_r |= (room->getTile(f) == t_slope_r);
wall |= (room->getTile(f) == t_wall);
passable |= (room->getTile(f) == t_passable);
}
if (wall || passable)
{
return t_empty;
}
if (slope_l)
{
return t_slope_l;
}
if (slope_r)
{
return t_slope_r;
}
return t_empty;
}
// Obtiene algunos parametros del jugador
player_t Player::getSpawnParams()
{
@@ -733,7 +577,7 @@ player_t Player::getSpawnParams()
params.y = y;
params.vx = vx;
params.vy = vy;
params.jump_ini = jump_ini;
params.jumpIni = jumpIni;
params.state = state;
params.flip = sprite->getFlip();

View File

@@ -27,7 +27,7 @@ struct player_t
float y;
float vx;
float vy;
int jump_ini;
int jumpIni;
state_e state;
SDL_RendererFlip flip;
};
@@ -60,9 +60,12 @@ public:
int border; // Indica en cual de los cuatro bordes se encuentra
bool invincible; // Si es invencible, no puede morir
SDL_Rect lastPosition; // Contiene la ultima posición del jugador, por si hay que deshacer algun movimiento
int jump_ini; // Valor del eje Y en el que se inicia el salto
int jumpIni; // Valor del eje Y en el que se inicia el salto
float maxVY; // Velocidad máxima que puede alcanzar al desplazarse en vertical
std::vector<JA_Sound> jumpSound; // Vecor con todos los sonidos del salto
std::vector<JA_Sound> fallSound; // Vecor con todos los sonidos de la caída
int jumpCounter; // Cuenta el tiempo de salto
int fallCounter; // Cuenta el tiempo de caida
SDL_Rect r;
// Comprueba las entradas y modifica variables
@@ -89,6 +92,12 @@ public:
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
void checkJumpEnd();
// Calcula y reproduce el sonido de salto
void playJumpSound();
// Calcula y reproduce el sonido de caer
void playFallSound();
// Comprueba si el jugador tiene suelo debajo de los pies
bool isOnFloor();
@@ -98,12 +107,6 @@ public:
// Comprueba que el jugador no atraviese ninguna pared
bool checkWalls();
// Comprueba si el jugador está en una rampa
tile_e checkSlopes();
// Comprueba si el jugador está en una rampa
tile_e checkSlopes2();
// Actualiza los puntos de colisión
void updateColliderPoints();

View File

@@ -73,6 +73,31 @@ void Title::checkEventHandler()
section.subsection = 0;
break;
case SDL_SCANCODE_F:
screen->switchVideoMode();
texture->reLoad();
break;
case SDL_SCANCODE_F1:
screen->setWindowSize(1);
texture->reLoad();
break;
case SDL_SCANCODE_F2:
screen->setWindowSize(2);
texture->reLoad();
break;
case SDL_SCANCODE_F3:
screen->setWindowSize(3);
texture->reLoad();
break;
case SDL_SCANCODE_F4:
screen->setWindowSize(4);
texture->reLoad();
break;
default:
break;
}