forked from jaildesigner-jailgames/jaildoctors_dilemma
Empezando a trabajar con tiles animados
This commit is contained in:
@@ -5,8 +5,8 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
|
|||||||
{
|
{
|
||||||
// Inicia algunas variables
|
// Inicia algunas variables
|
||||||
board.iniClock = SDL_GetTicks();
|
board.iniClock = SDL_GetTicks();
|
||||||
currentRoom = "01.room";
|
currentRoom = "03.room";
|
||||||
spawnPoint = {16, 96, 0, 0, 0, s_standing, SDL_FLIP_NONE};
|
spawnPoint = {15, 96, 0, 0, 0, s_standing, SDL_FLIP_NONE};
|
||||||
|
|
||||||
// Copia los punteros
|
// Copia los punteros
|
||||||
this->renderer = renderer;
|
this->renderer = renderer;
|
||||||
@@ -24,10 +24,10 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
|
|||||||
//currentRoom = "06.room";
|
//currentRoom = "06.room";
|
||||||
//spawnPoint = {14 * 8, 9 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
|
//spawnPoint = {14 * 8, 9 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
|
||||||
|
|
||||||
currentRoom = "60.room";
|
//currentRoom = "60.room";
|
||||||
const int x = 16;
|
//const int x = 16;
|
||||||
const int y = 13;
|
//const int y = 13;
|
||||||
spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
|
//spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
|
||||||
// ****
|
// ****
|
||||||
|
|
||||||
// Crea los objetos
|
// Crea los objetos
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Ren
|
|||||||
color = stringToColor("white");
|
color = stringToColor("white");
|
||||||
onBorder = false;
|
onBorder = false;
|
||||||
border = BORDER_TOP;
|
border = BORDER_TOP;
|
||||||
invincible = false;
|
invincible = true;
|
||||||
alive = true;
|
alive = true;
|
||||||
maxFallHeight = BLOCK * 4;
|
maxFallHeight = BLOCK * 4;
|
||||||
paused = false;
|
paused = false;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ Room::Room(std::string file, SDL_Renderer *renderer, Screen *screen, Asset *asse
|
|||||||
paused = false;
|
paused = false;
|
||||||
itemColor1 = stringToColor("magenta");
|
itemColor1 = stringToColor("magenta");
|
||||||
itemColor2 = stringToColor("yellow");
|
itemColor2 = stringToColor("yellow");
|
||||||
|
counter = 0;
|
||||||
|
|
||||||
// Copia los punteros a objetos
|
// Copia los punteros a objetos
|
||||||
this->renderer = renderer;
|
this->renderer = renderer;
|
||||||
@@ -38,6 +39,9 @@ Room::Room(std::string file, SDL_Renderer *renderer, Screen *screen, Asset *asse
|
|||||||
setLeftSlopes();
|
setLeftSlopes();
|
||||||
setRightSlopes();
|
setRightSlopes();
|
||||||
|
|
||||||
|
// Busca los tiles animados
|
||||||
|
setAnimatedTiles();
|
||||||
|
|
||||||
// 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 == nullptr)
|
if (mapTexture == nullptr)
|
||||||
@@ -70,6 +74,11 @@ Room::~Room()
|
|||||||
{
|
{
|
||||||
delete item;
|
delete item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto a: animatedTiles)
|
||||||
|
{
|
||||||
|
delete a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carga las variables desde un fichero de mapa
|
// Carga las variables desde un fichero de mapa
|
||||||
@@ -522,7 +531,7 @@ void Room::fillMapTexture()
|
|||||||
clip.x = (tilemap[index] % tilesetWidth) * tileSize;
|
clip.x = (tilemap[index] % tilesetWidth) * tileSize;
|
||||||
clip.y = (tilemap[index] / tilesetWidth) * tileSize;
|
clip.y = (tilemap[index] / tilesetWidth) * tileSize;
|
||||||
texture->render(renderer, x * tileSize, y * tileSize, &clip);
|
texture->render(renderer, x * tileSize, y * tileSize, &clip);
|
||||||
|
|
||||||
// ****
|
// ****
|
||||||
if (debug->getEnabled())
|
if (debug->getEnabled())
|
||||||
{
|
{
|
||||||
@@ -611,10 +620,14 @@ void Room::fillMapTexture()
|
|||||||
// Dibuja el mapa en pantalla
|
// Dibuja el mapa en pantalla
|
||||||
void Room::renderMap()
|
void Room::renderMap()
|
||||||
{
|
{
|
||||||
SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
|
|
||||||
|
|
||||||
// Dibuja la textura con el mapa en pantalla
|
// Dibuja la textura con el mapa en pantalla
|
||||||
SDL_RenderCopy(renderer, mapTexture, &rect, nullptr);
|
SDL_RenderCopy(renderer, mapTexture, nullptr, nullptr);
|
||||||
|
|
||||||
|
// Dibuja los tiles animados
|
||||||
|
for (auto a:animatedTiles)
|
||||||
|
{
|
||||||
|
a->render();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dibuja los enemigos en pantalla
|
// Dibuja los enemigos en pantalla
|
||||||
@@ -643,6 +656,12 @@ void Room::update()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actualiza el contador
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
// Actualiza los tiles animados
|
||||||
|
updateAnimatedTiles();
|
||||||
|
|
||||||
for (auto enemy : enemies)
|
for (auto enemy : enemies)
|
||||||
{ // Actualiza los enemigos
|
{ // Actualiza los enemigos
|
||||||
enemy->update();
|
enemy->update();
|
||||||
@@ -713,12 +732,18 @@ tile_e Room::getTile(int index)
|
|||||||
return t_slope_l;
|
return t_slope_l;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Las filas 10-18 son de tiles t_passable
|
// Las filas 10-17 son de tiles t_passable
|
||||||
if ((tilemap[index] >= 10 * tilesetWidth) && (tilemap[index] < 19 * tilesetWidth))
|
if ((tilemap[index] >= 10 * tilesetWidth) && (tilemap[index] < 18 * tilesetWidth))
|
||||||
{
|
{
|
||||||
return t_passable;
|
return t_passable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// La fila 18 es de tiles t_animated
|
||||||
|
if ((tilemap[index] >= 18 * tilesetWidth) && (tilemap[index] < 29 * tilesetWidth))
|
||||||
|
{
|
||||||
|
return t_animated;
|
||||||
|
}
|
||||||
|
|
||||||
// Las fila 19 es de tiles t_kill
|
// Las fila 19 es de tiles t_kill
|
||||||
if ((tilemap[index] >= 19 * tilesetWidth) && (tilemap[index] < 20 * tilesetWidth))
|
if ((tilemap[index] >= 19 * tilesetWidth) && (tilemap[index] < 20 * tilesetWidth))
|
||||||
{
|
{
|
||||||
@@ -1061,6 +1086,53 @@ void Room::setRightSlopes()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Localiza todos los tiles animados de la habitación
|
||||||
|
void Room::setAnimatedTiles()
|
||||||
|
{
|
||||||
|
// Recorre la habitación entera por filas buscando tiles de tipo t_animated
|
||||||
|
for (int i = 0; i < (int)tilemap.size(); ++i)
|
||||||
|
{
|
||||||
|
if (getTile(i) == t_animated)
|
||||||
|
{
|
||||||
|
// la i me da la ubicación
|
||||||
|
int x = (i % mapWidth) * tileSize;
|
||||||
|
int y = (i / mapWidth) * tileSize;
|
||||||
|
|
||||||
|
// tilemap[i] me da el tile a poner
|
||||||
|
int xc = (tilemap[i] % tilesetWidth) * tileSize;
|
||||||
|
int yc = (tilemap[i] / tilesetWidth) * tileSize;
|
||||||
|
|
||||||
|
Sprite *sp = new Sprite(x, y, 8, 8, texture, renderer);
|
||||||
|
sp->setSpriteClip(xc, yc, 8, 8);
|
||||||
|
animatedTiles.push_back(sp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza los tiles animados
|
||||||
|
void Room::updateAnimatedTiles()
|
||||||
|
{
|
||||||
|
const int frame = (counter / 8) % 4;
|
||||||
|
for (auto &a : animatedTiles)
|
||||||
|
{ // Frame vale 0, 1, 2 o 3. La x se aumenta de 8 en 8, salvo cuando frame vale 0 que ha de volver 3*8
|
||||||
|
SDL_Rect rect = a->getSpriteClip();
|
||||||
|
if (frame == 0)
|
||||||
|
rect.x -= 24;
|
||||||
|
else
|
||||||
|
rect.x += 8;
|
||||||
|
a->setSpriteClip(rect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pinta los tiles animados en pantalla
|
||||||
|
void Room::renderAnimatedTiles()
|
||||||
|
{
|
||||||
|
for (auto a : animatedTiles)
|
||||||
|
{
|
||||||
|
a->render();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Comprueba las colisiones
|
// Comprueba las colisiones
|
||||||
int Room::checkRightSurfaces(SDL_Rect *rect)
|
int Room::checkRightSurfaces(SDL_Rect *rect)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
#include "enemy.h"
|
#include "enemy.h"
|
||||||
#include "item.h"
|
#include "item.h"
|
||||||
|
#include "sprite.h"
|
||||||
#include "item_tracker.h"
|
#include "item_tracker.h"
|
||||||
#include "const.h"
|
#include "const.h"
|
||||||
#include "jail_audio.h"
|
#include "jail_audio.h"
|
||||||
@@ -36,7 +37,8 @@ enum tile_e
|
|||||||
t_passable,
|
t_passable,
|
||||||
t_slope_l,
|
t_slope_l,
|
||||||
t_slope_r,
|
t_slope_r,
|
||||||
t_kill
|
t_kill,
|
||||||
|
t_animated
|
||||||
};
|
};
|
||||||
|
|
||||||
// Clase Room
|
// Clase Room
|
||||||
@@ -73,6 +75,8 @@ private:
|
|||||||
std::vector<v_line_t> rightSurfaces; // Lista con las superficies laterales de la parte derecha de la habitación
|
std::vector<v_line_t> rightSurfaces; // Lista con las superficies laterales de la parte derecha de la habitación
|
||||||
std::vector<d_line_t> leftSlopes; // Lista con todas las rampas que suben hacia la izquierda
|
std::vector<d_line_t> leftSlopes; // Lista con todas las rampas que suben hacia la izquierda
|
||||||
std::vector<d_line_t> rightSlopes; // Lista con todas las rampas que suben hacia la derecha
|
std::vector<d_line_t> rightSlopes; // Lista con todas las rampas que suben hacia la derecha
|
||||||
|
int counter; // Contador para lo que haga falta
|
||||||
|
std::vector<Sprite *> animatedTiles; // Vector con los indices de tiles animados
|
||||||
|
|
||||||
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
|
||||||
@@ -115,6 +119,15 @@ private:
|
|||||||
// Encuentra todas las rampas que suben hacia la derecha
|
// Encuentra todas las rampas que suben hacia la derecha
|
||||||
void setRightSlopes();
|
void setRightSlopes();
|
||||||
|
|
||||||
|
// Localiza todos los tiles animados de la habitación
|
||||||
|
void setAnimatedTiles();
|
||||||
|
|
||||||
|
// Actualiza los tiles animados
|
||||||
|
void updateAnimatedTiles();
|
||||||
|
|
||||||
|
// Pinta los tiles animados en pantalla
|
||||||
|
void renderAnimatedTiles();
|
||||||
|
|
||||||
// Devuelve el tipo de tile que hay en ese indice
|
// Devuelve el tipo de tile que hay en ese indice
|
||||||
tile_e getTile(int index);
|
tile_e getTile(int index);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user