Ya sube cuestas pero no las baja

This commit is contained in:
2022-09-05 23:22:49 +02:00
parent b6cfe45872
commit ce8a4a8050
7 changed files with 156 additions and 43 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@@ -164,11 +164,13 @@ bool Director::setFileList()
asset->add("/data/room/03.room", room);
asset->add("/data/room/04.room", room);
asset->add("/data/room/05.room", room);
asset->add("/data/room/06.room", room);
asset->add("/data/room/01.tmx", room);
asset->add("/data/room/02.tmx", room);
asset->add("/data/room/03.tmx", room);
asset->add("/data/room/04.tmx", room);
asset->add("/data/room/05.tmx", room);
asset->add("/data/room/06.tmx", room);
asset->add("/media/tilesets/standard.png", bitmap);

View File

@@ -3,10 +3,13 @@
// Constructor
Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input)
{
// Inicia variables
// Inicia algunas variables
clock = SDL_GetTicks();
currentRoom = "01.room";
spawnPoint = {2 * 8, 12 * 8, 0, 0, 0, s_standing, SDL_FLIP_NONE};
spawnPoint = {16, 96, 0, 0, 0, s_standing, SDL_FLIP_NONE};
currentRoom = "06.room";
spawnPoint = {240, 96, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
// Copia los punteros
this->renderer = renderer;
@@ -24,7 +27,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input)
debugText = new Text(asset->get("debug.png"), asset->get("debug.txt"), renderer);
music = JA_LoadMusic(asset->get("game.ogg").c_str());
// Inicializa variables
// Inicializa el resto de variables
ticks = 0;
ticksSpeed = 15;
playerLives = 9;

View File

@@ -2,8 +2,6 @@
#include <fstream>
#include <sstream>
// CAUTION!!!!! si no se gasta al final, quitar la referencia a la habitación
// Constructor
Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Renderer *renderer, Asset *asset, Input *input, Room *room)
{
@@ -40,12 +38,15 @@ Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Ren
sprite->setHeight(16);
sprite->setFlip(ini.flip);
sprite->setCurrentAnimation("walk");
sprite->animate();
lastPosition = getRect();
colliderBox = getRect();
const SDL_Point p = {0, 0};
colliderPoints.insert(colliderPoints.end(), {p, p, p, p, p, p, p, p});
underFeet.insert(underFeet.end(), {p, p});
feet.insert(feet.end(), {p, p});
}
// Destructor
@@ -236,6 +237,23 @@ void Player::move()
}
}
// Comprueba colisiones con rampas
else
{
const tile_e slope = checkSlopes();
// Se mueve hacia la derecha y cuesta hacia la derecha
if (sprite->getFlip() == SDL_FLIP_NONE && slope == t_slope_r)
{ // Recoloca
y = -h + room->getSlopeHeight(feet[1], t_slope_r);
}
// Se mueve hacia la izquierda y cuesta hacia la izquierda
if (sprite->getFlip() == SDL_FLIP_HORIZONTAL && slope == t_slope_l)
{ // Recoloca
y = -h + room->getSlopeHeight(feet[0], t_slope_l);
}
}
y += vy;
if (checkWalls())
{
@@ -309,6 +327,12 @@ void Player::move()
vy = 0.0f;
}
}
// EXPERIMENTAL
else if (checkSlopes())
{
state = s_standing;
vy = 0.0f;
}
}
}
@@ -320,16 +344,10 @@ void Player::move()
// Establece la animación del jugador
void Player::animate()
{
// Establece la animación
if (vx != 0)
{
sprite->setCurrentAnimation("walk");
sprite->animate();
}
else
{
sprite->setCurrentAnimation("stand");
}
sprite->animate();
}
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
@@ -353,7 +371,8 @@ bool Player::isOnFloor()
for (auto f : underFeet)
{
onFloor |= ((room->getTile(f) == TILE_SOLID) || (room->getTile(f) == TILE_TRAVESSABLE));
const tile_e tile = (room->getTile(f));
onFloor |= (tile == t_wall || tile == t_passable || tile == t_slope_l || tile == t_slope_r);
}
return onFloor;
@@ -370,12 +389,41 @@ bool Player::checkWalls()
for (auto c : colliderPoints)
{
wall |= (room->getTile(c) == TILE_SOLID);
wall |= (room->getTile(c) == t_wall);
}
return wall;
}
// Comprueba si el jugador está en una rampa
tile_e Player::checkSlopes()
{
// Actualiza los puntos de colisión
updateFeet();
// Comprueba si ha colisionado con una rampa
bool slope_l = false;
bool slope_r = false;
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;
}
// Obtiene algunos parametros del jugador
player_t Player::getSpawnParams()
{
@@ -425,6 +473,9 @@ void Player::updateFeet()
underFeet[0] = {p.x, p.y + h};
underFeet[1] = {p.x + 7, p.y + h};
feet[0] = {p.x, p.y + h - 1};
feet[1] = {p.x + 7, p.y + h - 1};
}
// Obtiene el valor de la variable

View File

@@ -51,6 +51,7 @@ public:
SDL_Rect colliderBox; // Caja de colisión con los enemigos u objetos
std::vector<SDL_Point> colliderPoints; // Puntos de colisión con el mapa
std::vector<SDL_Point> underFeet; // Contiene los puntos que hay bajo cada pie del jugador
std::vector<SDL_Point> feet; // Contiene los puntos que hay en el pie del jugador
state_e state; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
bool onBorder; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
int border; // Indica en cual de los cuatro bordes se encuentra
@@ -89,6 +90,9 @@ public:
// Comprueba que el jugador no atraviese ninguna pared
bool checkWalls();
// Comprueba si el jugador está en una rampa
tile_e checkSlopes();
// Actualiza los puntos de colisión
void updateColliderPoints();

View File

@@ -28,6 +28,12 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
// Establece el color del borde
screen->setBorderColor(borderColor);
// Inicializa variables
tileSize = 8;
mapWidth = 32;
mapHeight = 16;
tilesetWidth = 20;
}
// Destructor
@@ -499,29 +505,36 @@ std::string Room::getRoom(int border)
}
// Devuelve el tipo de tile que hay en ese pixel
int Room::getTile(SDL_Point point)
tile_e Room::getTile(SDL_Point point)
{
int pos = ((point.y / 8) * 32) + (point.x / 8);
int tile = TILE_EMPTY;
const int maxTile = mapWidth * mapHeight;
const int pos = ((point.y / tileSize) * mapWidth) + (point.x / tileSize);
tile_e tile = t_empty;
if (pos < 512)
if (pos < maxTile)
{
// Los tiles entre el 1 y el 80 son solidos
if ((tilemap[pos] > 0) && (tilemap[pos] < 201))
// Las filas 0-7 son de tiles t_wall
if ((tilemap[pos] > 0) && (tilemap[pos] < 8 * tilesetWidth))
{
return TILE_SOLID;
return t_wall;
}
// Los tiles mayores de 80 son atravesables
if ((tilemap[pos] > 200) && (tilemap[pos] < 381))
// La fila 8 es de tiles t_slope_r
else if ((tilemap[pos] >= 8 * tilesetWidth) && (tilemap[pos] < 9 * tilesetWidth))
{
return TILE_TRAVESSABLE;
return t_slope_r;
}
// Los tiles mayores de 80 son atravesables
if ((tilemap[pos] > 380) && (tilemap[pos] < 400))
// La fila 9 es de tiles t_slope_l
else if ((tilemap[pos] >= 9 * tilesetWidth) && (tilemap[pos] < 10 * tilesetWidth))
{
return TILE_KILL;
return t_slope_l;
}
// Las filas 10-14 son de tiles t_passable
if ((tilemap[pos] >= 10 * tilesetWidth) && (tilemap[pos] < 15 * tilesetWidth))
{
return t_passable;
}
}
@@ -582,3 +595,29 @@ int Room::getTileSize()
{
return 8;
}
// Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
int Room::getSlopeHeight(SDL_Point p, tile_e slope)
{
// Calcula la base del tile
int base = ((p.y / tileSize) * tileSize) + tileSize;
printf("base %i\n", base);
// Calcula cuanto se ha entrado en el tile horizontalmente
const int pos = (p.x % tileSize); // esto da un valor entre 0 y 7
printf("pos %i\n", base);
// Se resta a la base la cantidad de pixeles pos en funcion de la rampa
if (slope == t_slope_r)
{
base -= pos+1;
printf("base_R %i\n", base);
}
else
{
base -= (tileSize - pos);
printf("base_L %i\n", base);
}
return base;
}

View File

@@ -15,24 +15,29 @@
#ifndef ROOM_H
#define ROOM_H
#define TILE_EMPTY 0
#define TILE_SOLID 1
#define TILE_TRAVESSABLE 2
#define TILE_KILL 3
/*
Cada habitación se crea y destruye cada vez que se entra o sale de la misma
Cada habitacion si que tendra lo siguiente:
ID (numerico)
NOMBRE (texto)
COLOR DE FONDO (texto)
SET DE TILES (texto, hace referencia a un png de la colección)
LIMITE SUPERIOR (ID de la habitación superior), INFERIOR, IZQUIERDO y DERECHO
MAPA DE TILES (array con los indices de los tiles a utilizar) <-- hay que decidir si cada tile del set ya
tierne propiedades o se ponen en un mapa aparte
LISTADO DE ENEMIGOS (tipo, posicion, dx, dy)
LISTADO DE ITEMS (tipo, posicion)
Cada habitacion tiene lo siguiente:
- ID (numerico)
- NOMBRE (texto)
- COLOR DE FONDO (texto)
- COLOR DEL BORDE (texto)
- SET DE TILES (texto, hace referencia a un png de la colección)
- LIMITE SUPERIOR (ID de la habitación superior), INFERIOR, IZQUIERDO y DERECHO
- MAPA DE TILES (array con los indices de los tiles a utilizar) <-- hay que decidir si cada tile del set ya
tierne propiedades o se ponen en un mapa aparte
- LISTADO DE ENEMIGOS (tipo, posicion, dx, dy)
- LISTADO DE ITEMS (tipo, posicion)
*/
enum tile_e
{
t_empty,
t_wall,
t_passable,
t_slope_l,
t_slope_r,
t_death
};
// Clase Room
class Room
@@ -58,6 +63,11 @@ private:
JA_Sound itemSound; // Sonido producido al coger un objeto
int *itemsPicked; // Puntero a la cantidad de items recogidos que lleva el juego
int tileSize; // Ancho del tile en pixels
int mapWidth; // Ancho del mapa en tiles
int mapHeight; // Alto del mapa en tiles
int tilesetWidth; // Ancho del tileset en tiles
// Carga las variables desde un fichero
bool load(std::string file_path);
@@ -102,7 +112,7 @@ public:
std::string getRoom(int border);
// Devuelve el tipo de tile que hay en ese pixel
int getTile(SDL_Point point);
tile_e getTile(SDL_Point point);
// Indica si hay colision con un enemigo a partir de un rectangulo
bool enemyCollision(SDL_Rect &rect);
@@ -115,6 +125,10 @@ public:
// Obten el tamaño del tile
int getTileSize();
// Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
int getSlopeHeight(SDL_Point p, tile_e slope);
};
#endif