forked from jaildesigner-jailgames/jaildoctors_dilemma
Creando las superficies del mapa
This commit is contained in:
154
source/room.cpp
154
source/room.cpp
@@ -6,6 +6,12 @@
|
|||||||
// Constructor
|
// Constructor
|
||||||
Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset *asset, ItemTracker *itemTracker, int *items, Debug *debug)
|
Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset *asset, ItemTracker *itemTracker, int *items, Debug *debug)
|
||||||
{
|
{
|
||||||
|
// Inicializa variables
|
||||||
|
tileSize = 8;
|
||||||
|
mapWidth = 32;
|
||||||
|
mapHeight = 16;
|
||||||
|
tilesetWidth = 20;
|
||||||
|
|
||||||
// Copia los punteros a objetos
|
// Copia los punteros a objetos
|
||||||
this->renderer = renderer;
|
this->renderer = renderer;
|
||||||
this->asset = asset;
|
this->asset = asset;
|
||||||
@@ -19,6 +25,12 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
|
|||||||
texture = new LTexture(renderer, asset->get(tileset));
|
texture = new LTexture(renderer, asset->get(tileset));
|
||||||
itemSound = JA_LoadSound(asset->get("item.wav").c_str());
|
itemSound = JA_LoadSound(asset->get("item.wav").c_str());
|
||||||
|
|
||||||
|
// Calcula las superficies
|
||||||
|
setBottomSurfaces();
|
||||||
|
setTopSurfaces();
|
||||||
|
setLeftSurfaces();
|
||||||
|
setRightSurfaces();
|
||||||
|
|
||||||
// Crea la textura para el mapa de tiles de la habitación
|
// Crea la textura para el mapa de tiles de la habitación
|
||||||
mapTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
mapTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||||
if (mapTexture == NULL)
|
if (mapTexture == NULL)
|
||||||
@@ -29,12 +41,6 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
|
|||||||
|
|
||||||
// Establece el color del borde
|
// Establece el color del borde
|
||||||
screen->setBorderColor(borderColor);
|
screen->setBorderColor(borderColor);
|
||||||
|
|
||||||
// Inicializa variables
|
|
||||||
tileSize = 8;
|
|
||||||
mapWidth = 32;
|
|
||||||
mapHeight = 16;
|
|
||||||
tilesetWidth = 20;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -434,6 +440,32 @@ void Room::fillMapTexture()
|
|||||||
texture->render(renderer, x * 8, y * 8, &clip);
|
texture->render(renderer, x * 8, y * 8, &clip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ****
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
|
||||||
|
for (auto l : bottomSurfaces)
|
||||||
|
{
|
||||||
|
SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
|
||||||
|
for (auto l : topSurfaces)
|
||||||
|
{
|
||||||
|
SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
||||||
|
for (auto l : leftSurfaces)
|
||||||
|
{
|
||||||
|
SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||||
|
for (auto l : rightSurfaces)
|
||||||
|
{
|
||||||
|
SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ****
|
||||||
SDL_SetRenderTarget(renderer, nullptr);
|
SDL_SetRenderTarget(renderer, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -542,6 +574,42 @@ tile_e Room::getTile(SDL_Point point)
|
|||||||
return tile;
|
return tile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Devuelve el tipo de tile que hay en ese indice
|
||||||
|
tile_e Room::getTile(int index)
|
||||||
|
{
|
||||||
|
const int maxTile = mapWidth * mapHeight;
|
||||||
|
tile_e tile = t_empty;
|
||||||
|
|
||||||
|
if (index < maxTile)
|
||||||
|
{
|
||||||
|
// Las filas 0-7 son de tiles t_wall
|
||||||
|
if ((tilemap[index] > 0) && (tilemap[index] < 8 * tilesetWidth))
|
||||||
|
{
|
||||||
|
return t_wall;
|
||||||
|
}
|
||||||
|
|
||||||
|
// La fila 8 es de tiles t_slope_r
|
||||||
|
else if ((tilemap[index] >= 8 * tilesetWidth) && (tilemap[index] < 9 * tilesetWidth))
|
||||||
|
{
|
||||||
|
return t_slope_r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// La fila 9 es de tiles t_slope_l
|
||||||
|
else if ((tilemap[index] >= 9 * tilesetWidth) && (tilemap[index] < 10 * tilesetWidth))
|
||||||
|
{
|
||||||
|
return t_slope_l;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Las filas 10-14 son de tiles t_passable
|
||||||
|
if ((tilemap[index] >= 10 * tilesetWidth) && (tilemap[index] < 15 * tilesetWidth))
|
||||||
|
{
|
||||||
|
return t_passable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tile;
|
||||||
|
}
|
||||||
|
|
||||||
// Indica si hay colision con un enemigo a partir de un rectangulo
|
// Indica si hay colision con un enemigo a partir de un rectangulo
|
||||||
bool Room::enemyCollision(SDL_Rect &rect)
|
bool Room::enemyCollision(SDL_Rect &rect)
|
||||||
{
|
{
|
||||||
@@ -626,3 +694,77 @@ int Room::getSlopeHeight(SDL_Point p, tile_e slope)
|
|||||||
|
|
||||||
return base;
|
return base;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calcula las superficies inferiores
|
||||||
|
void Room::setBottomSurfaces()
|
||||||
|
{
|
||||||
|
std::vector<int> tile;
|
||||||
|
|
||||||
|
// Busca todos los tiles de tipo wall (excepto los de la última fila) que debajo
|
||||||
|
// tienen un tile de tipo vacio
|
||||||
|
for (int i = 0; i < tilemap.size() - mapWidth; i++)
|
||||||
|
{
|
||||||
|
if (getTile(i) == t_wall && getTile(i + mapWidth) == t_empty)
|
||||||
|
{
|
||||||
|
tile.push_back(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies
|
||||||
|
int i = 0;
|
||||||
|
while (i < tile.size())
|
||||||
|
{
|
||||||
|
h_line_t line;
|
||||||
|
line.x1 = (tile[i] % mapWidth) * tileSize;
|
||||||
|
line.y = ((tile[i] / mapWidth) * tileSize) + tileSize - 1;
|
||||||
|
while (tile[i] + 1 == tile[i + 1])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
line.x2 = ((tile[i] % mapWidth) * tileSize) + tileSize - 1;
|
||||||
|
bottomSurfaces.push_back(line);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula las superficies superiores
|
||||||
|
void Room::setTopSurfaces()
|
||||||
|
{
|
||||||
|
std::vector<int> tile;
|
||||||
|
|
||||||
|
// Busca todos los tiles de tipo wall (excepto los de la primera fila) que encima
|
||||||
|
// tienen un tile de tipo vacio
|
||||||
|
for (int i = mapWidth; i < tilemap.size(); i++)
|
||||||
|
{
|
||||||
|
if (getTile(i) == t_wall && getTile(i - mapWidth) == t_empty)
|
||||||
|
{
|
||||||
|
tile.push_back(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies
|
||||||
|
int i = 0;
|
||||||
|
while (i < tile.size())
|
||||||
|
{
|
||||||
|
h_line_t line;
|
||||||
|
line.x1 = (tile[i] % mapWidth) * tileSize;
|
||||||
|
line.y = ((tile[i] / mapWidth) * tileSize);
|
||||||
|
while (tile[i] + 1 == tile[i + 1])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
line.x2 = ((tile[i] % mapWidth) * tileSize) + tileSize - 1;
|
||||||
|
topSurfaces.push_back(line);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula las superficies laterales izquierdas
|
||||||
|
void Room::setLeftSurfaces()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcula las superficies laterales derechas
|
||||||
|
void Room::setRightSurfaces()
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -64,6 +64,10 @@ private:
|
|||||||
JA_Sound itemSound; // Sonido producido al coger un objeto
|
JA_Sound itemSound; // Sonido producido al coger un objeto
|
||||||
int *itemsPicked; // Puntero a la cantidad de items recogidos que lleva el juego
|
int *itemsPicked; // Puntero a la cantidad de items recogidos que lleva el juego
|
||||||
Debug *debug; // Objeto para gestionar la información de debug
|
Debug *debug; // Objeto para gestionar la información de debug
|
||||||
|
std::vector<h_line_t> bottomSurfaces; // Lista con las superficies inferiores de la habitación
|
||||||
|
std::vector<h_line_t> topSurfaces; // Lista con las superficies superiores de la habitación
|
||||||
|
std::vector<v_line_t> leftSurfaces; // Lista con las superficies laterales de la parte izquierda de la habitación
|
||||||
|
std::vector<v_line_t> rightSurfaces; // Lista con las superficies laterales de la parte derecha de la habitación
|
||||||
|
|
||||||
int tileSize; // Ancho del tile en pixels
|
int tileSize; // Ancho del tile en pixels
|
||||||
int mapWidth; // Ancho del mapa en tiles
|
int mapWidth; // Ancho del mapa en tiles
|
||||||
@@ -85,6 +89,21 @@ private:
|
|||||||
// Pinta el mapa de la habitación en la textura
|
// Pinta el mapa de la habitación en la textura
|
||||||
void fillMapTexture();
|
void fillMapTexture();
|
||||||
|
|
||||||
|
// Calcula las superficies inferiores
|
||||||
|
void setBottomSurfaces();
|
||||||
|
|
||||||
|
// Calcula las superficies superiores
|
||||||
|
void setTopSurfaces();
|
||||||
|
|
||||||
|
// Calcula las superficies laterales izquierdas
|
||||||
|
void setLeftSurfaces();
|
||||||
|
|
||||||
|
// Calcula las superficies laterales derechas
|
||||||
|
void setRightSurfaces();
|
||||||
|
|
||||||
|
// Devuelve el tipo de tile que hay en ese indice
|
||||||
|
tile_e getTile(int index);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset *asset, ItemTracker *item_tracker, int *items, Debug *debug);
|
Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset *asset, ItemTracker *item_tracker, int *items, Debug *debug);
|
||||||
|
|||||||
@@ -140,6 +140,68 @@ bool checkCollision(SDL_Point &p, SDL_Rect &r)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea horizontal y un rectangulo
|
||||||
|
bool checkCollision(h_line_t &l, SDL_Rect &r)
|
||||||
|
{
|
||||||
|
// Comprueba si la linea esta por encima del rectangulo
|
||||||
|
if (l.y < r.y)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si la linea esta por debajo del rectangulo
|
||||||
|
if (l.y > r.y + r.h)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el inicio de la linea esta a la derecha del rectangulo
|
||||||
|
if (l.x1 > r.x + r.w)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el final de la linea esta a la izquierda del rectangulo
|
||||||
|
if (l.x2 < r.x)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si ha llegado hasta aquí, hay colisión
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea vertical y un rectangulo
|
||||||
|
bool checkCollision(v_line_t &l, SDL_Rect &r)
|
||||||
|
{
|
||||||
|
// Comprueba si la linea esta por la izquierda del rectangulo
|
||||||
|
if (l.x < r.x)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si la linea esta por la derecha del rectangulo
|
||||||
|
if (l.x > r.x + r.w)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el inicio de la linea esta debajo del rectangulo
|
||||||
|
if (l.y1 > r.y + r.h)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba si el final de la linea esta encima del rectangulo
|
||||||
|
if (l.y2 < r.y)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si ha llegado hasta aquí, hay colisión
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Devuelve un color_t a partir de un string
|
// Devuelve un color_t a partir de un string
|
||||||
color_t stringToColor(std::string str)
|
color_t stringToColor(std::string str)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,6 +34,18 @@ struct circle_t
|
|||||||
int r;
|
int r;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Estructura para definir una linea horizontal
|
||||||
|
struct h_line_t
|
||||||
|
{
|
||||||
|
int x1, x2, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para definir una linea vertical
|
||||||
|
struct v_line_t
|
||||||
|
{
|
||||||
|
int x, y1, y2;
|
||||||
|
};
|
||||||
|
|
||||||
// Estructura para definir un color
|
// Estructura para definir un color
|
||||||
struct color_t
|
struct color_t
|
||||||
{
|
{
|
||||||
@@ -74,9 +86,15 @@ bool checkCollision(circle_t &a, SDL_Rect &b);
|
|||||||
// Detector de colisiones entre un dos rectangulos
|
// Detector de colisiones entre un dos rectangulos
|
||||||
bool checkCollision(SDL_Rect &a, SDL_Rect &b);
|
bool checkCollision(SDL_Rect &a, SDL_Rect &b);
|
||||||
|
|
||||||
// Detector de colisiones entre un punto y u rectangulo
|
// Detector de colisiones entre un punto y un rectangulo
|
||||||
bool checkCollision(SDL_Point &p, SDL_Rect &r);
|
bool checkCollision(SDL_Point &p, SDL_Rect &r);
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea horizontal y un rectangulo
|
||||||
|
bool checkCollision(h_line_t &l, SDL_Rect &r);
|
||||||
|
|
||||||
|
// Detector de colisiones entre una linea vertical y un rectangulo
|
||||||
|
bool checkCollision(v_line_t &l, SDL_Rect &r);
|
||||||
|
|
||||||
// Devuelve un color_t a partir de un string
|
// Devuelve un color_t a partir de un string
|
||||||
color_t stringToColor(std::string str);
|
color_t stringToColor(std::string str);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user