Empezando con los actores y polimorfismo. Ya pinta la plataforma movil
This commit is contained in:
10
data/actors/moving_platform.ani
Normal file
10
data/actors/moving_platform.ani
Normal file
@@ -0,0 +1,10 @@
|
||||
frames_per_row=1
|
||||
frame_width=16
|
||||
frame_height=8
|
||||
|
||||
[animation]
|
||||
name=default
|
||||
speed=8
|
||||
loop=0
|
||||
frames=0
|
||||
[/animation]
|
||||
BIN
data/actors/moving_platform.png
Normal file
BIN
data/actors/moving_platform.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 151 B |
@@ -11,32 +11,21 @@ room_right=02.map
|
||||
01.tmx
|
||||
[/tilemap]
|
||||
|
||||
[enemy]
|
||||
tileset=walking_eye.png
|
||||
animation=walking_eye.ani
|
||||
[actors]
|
||||
|
||||
[moving platform]
|
||||
tileset=moving_platform.png
|
||||
animation=moving_platform.ani
|
||||
width=16
|
||||
height=16
|
||||
x=11
|
||||
y=17
|
||||
height=8
|
||||
x=9
|
||||
y=11
|
||||
vx=0.3
|
||||
vy=0
|
||||
x1=11
|
||||
y1=17
|
||||
x1=9
|
||||
y1=11
|
||||
x2=15
|
||||
y2=17
|
||||
[/enemy]
|
||||
y2=11
|
||||
[/moving platform]
|
||||
|
||||
[enemy]
|
||||
tileset=flying_eye_horn.png
|
||||
animation=flying_eye_horn.ani
|
||||
width=16
|
||||
height=16
|
||||
x=20
|
||||
y=8
|
||||
vx=0.7
|
||||
vy=0
|
||||
x1=5
|
||||
y1=8
|
||||
x2=33
|
||||
y2=8
|
||||
[/enemy]
|
||||
[/actors]
|
||||
@@ -10,33 +10,3 @@ room_right=0
|
||||
[tilemap]
|
||||
02.tmx
|
||||
[/tilemap]
|
||||
|
||||
[enemy]
|
||||
tileset=flying_eye.png
|
||||
animation=flying_eye.ani
|
||||
width=16
|
||||
height=16
|
||||
x=18
|
||||
y=11
|
||||
vx=0
|
||||
vy=0.5
|
||||
x1=18
|
||||
y1=11
|
||||
x2=18
|
||||
y2=17
|
||||
[/enemy]
|
||||
|
||||
[enemy]
|
||||
tileset=flying_eye_horn.png
|
||||
animation=flying_eye_horn.ani
|
||||
width=16
|
||||
height=16
|
||||
x=21
|
||||
y=11
|
||||
vx=0
|
||||
vy=0.7
|
||||
x1=21
|
||||
y1=11
|
||||
x2=21
|
||||
y2=17
|
||||
[/enemy]
|
||||
@@ -2,6 +2,11 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
// Constructor
|
||||
Actor::Actor()
|
||||
{
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Actor::Actor(actor_t actor)
|
||||
{
|
||||
@@ -23,7 +28,6 @@ Actor::Actor(actor_t actor)
|
||||
// Inicializa el sprite con el resto de parametros comunes
|
||||
sprite->setWidth(actor.w);
|
||||
sprite->setHeight(actor.h);
|
||||
sprite->setCurrentAnimation("walk");
|
||||
sprite->setFlip(actor.vx>0?SDL_FLIP_NONE:SDL_FLIP_HORIZONTAL);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,16 +36,17 @@ protected:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Actor();
|
||||
Actor(actor_t actor);
|
||||
|
||||
// Destructor
|
||||
~Actor();
|
||||
virtual ~Actor();
|
||||
|
||||
// Pinta el enemigo en pantalla
|
||||
void render();
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void update();
|
||||
virtual void update();
|
||||
|
||||
// Obtiene el rectangulo de colision del enemigo
|
||||
SDL_Rect &getCollider();
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
#include <sstream>
|
||||
|
||||
// Constructor
|
||||
ActorMovingPlatform::ActorMovingPlatform()
|
||||
ActorMovingPlatform::ActorMovingPlatform(actor_t actor, SDL_Point p1, SDL_Point p2) : Actor(actor)
|
||||
{
|
||||
this->p1 = p1;
|
||||
this->p2 = p2;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -19,14 +21,14 @@ void ActorMovingPlatform::checkPath()
|
||||
if (sprite->getPosX() > p2.x || sprite->getPosX() < p1.x)
|
||||
{
|
||||
sprite->setVelX(sprite->getVelX() * (-1));
|
||||
sprite->flip();
|
||||
//sprite->flip();
|
||||
}
|
||||
|
||||
// Comprueba los límites verticales
|
||||
if (sprite->getPosY() > p2.y || sprite->getPosY() < p1.y)
|
||||
{
|
||||
sprite->setVelY(sprite->getVelY() * (-1));
|
||||
sprite->flip();
|
||||
//sprite->flip();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
ActorMovingPlatform();
|
||||
ActorMovingPlatform(actor_t actor, SDL_Point p1, SDL_Point p2);
|
||||
|
||||
// Destructor
|
||||
~ActorMovingPlatform();
|
||||
|
||||
@@ -65,7 +65,7 @@ public:
|
||||
bool load(std::string filePath);
|
||||
|
||||
// Establece la animacion actual
|
||||
void setCurrentAnimation(std::string name);
|
||||
void setCurrentAnimation(std::string name = "default");
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void update();
|
||||
|
||||
@@ -36,11 +36,11 @@ Map::~Map()
|
||||
|
||||
SDL_DestroyTexture(map_texture);
|
||||
|
||||
for (auto enemy : enemy_list)
|
||||
for (auto actor : actors)
|
||||
{
|
||||
delete enemy;
|
||||
delete actor;
|
||||
}
|
||||
enemy_list.clear();
|
||||
actors.clear();
|
||||
}
|
||||
|
||||
// Carga las variables desde un fichero
|
||||
@@ -104,12 +104,44 @@ bool Map::load(std::string file_path)
|
||||
} while (line != "[/tilemap]");
|
||||
}
|
||||
|
||||
// Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo
|
||||
else if (line == "[enemy]")
|
||||
// Si la linea contiene el texto [actor] se realiza el proceso de carga de los actores
|
||||
else if (line == "[actors]")
|
||||
{
|
||||
enemy_t enemy;
|
||||
enemy.asset = asset;
|
||||
enemy.renderer = renderer;
|
||||
do
|
||||
{
|
||||
std::getline(file, line);
|
||||
|
||||
if (line == "[moving platform]")
|
||||
{
|
||||
actor_t actor;
|
||||
actor.asset = asset;
|
||||
actor.renderer = renderer;
|
||||
SDL_Point p1, p2;
|
||||
|
||||
do
|
||||
{
|
||||
std::getline(file, line);
|
||||
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (!setActor(&actor, &p1, &p2, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
|
||||
} while (line != "[/moving platform]");
|
||||
|
||||
printf("actor loaded\n");
|
||||
actors.push_back(new ActorMovingPlatform(actor, p1, p2));
|
||||
}
|
||||
|
||||
} while (line != "[/actors]");
|
||||
/*actor_t actor;
|
||||
actor.asset = asset;
|
||||
actor.renderer = renderer;
|
||||
|
||||
do
|
||||
{
|
||||
@@ -118,15 +150,15 @@ bool Map::load(std::string file_path)
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
// Procesa las dos subcadenas
|
||||
if (!setEnemy(&enemy, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
if (!setActor(&actor, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
} while (line != "[/enemy]");
|
||||
} while (line != "[/actor]");
|
||||
|
||||
// Añade el enemigo al vector de enemigos
|
||||
enemy_list.push_back(new Enemy(enemy));
|
||||
actors.push_back(new ActorMovingPlatform(actor));*/
|
||||
}
|
||||
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
@@ -219,60 +251,60 @@ bool Map::setVars(std::string var, std::string value)
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura enemy_t
|
||||
bool Map::setEnemy(enemy_t *enemy, std::string var, std::string value)
|
||||
bool Map::setActor(actor_t *actor, SDL_Point *p1, SDL_Point *p2, std::string var, std::string value)
|
||||
{
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "tileset")
|
||||
{
|
||||
enemy->tileset = value;
|
||||
actor->tileset = value;
|
||||
}
|
||||
else if (var == "animation")
|
||||
{
|
||||
enemy->animation = value;
|
||||
actor->animation = value;
|
||||
}
|
||||
else if (var == "width")
|
||||
{
|
||||
enemy->w = std::stof(value);
|
||||
actor->w = std::stof(value);
|
||||
}
|
||||
else if (var == "height")
|
||||
{
|
||||
enemy->h = std::stof(value);
|
||||
actor->h = std::stof(value);
|
||||
}
|
||||
else if (var == "x")
|
||||
{
|
||||
enemy->x = std::stof(value) * tile_size;
|
||||
actor->x = std::stof(value) * tile_size;
|
||||
}
|
||||
else if (var == "y")
|
||||
{
|
||||
enemy->y = std::stof(value) * tile_size;
|
||||
actor->y = std::stof(value) * tile_size;
|
||||
}
|
||||
else if (var == "vx")
|
||||
{
|
||||
enemy->vx = std::stof(value);
|
||||
actor->vx = std::stof(value);
|
||||
}
|
||||
else if (var == "vy")
|
||||
{
|
||||
enemy->vy = std::stof(value);
|
||||
actor->vy = std::stof(value);
|
||||
}
|
||||
else if (var == "x1")
|
||||
{
|
||||
enemy->p1.x = std::stoi(value) * tile_size;
|
||||
p1->x = std::stoi(value) * tile_size;
|
||||
}
|
||||
else if (var == "x2")
|
||||
{
|
||||
enemy->p2.x = std::stoi(value) * tile_size;
|
||||
p2->x = std::stoi(value) * tile_size;
|
||||
}
|
||||
else if (var == "y1")
|
||||
{
|
||||
enemy->p1.y = std::stoi(value) * tile_size;
|
||||
p1->y = std::stoi(value) * tile_size;
|
||||
}
|
||||
else if (var == "y2")
|
||||
{
|
||||
enemy->p2.y = std::stoi(value) * tile_size;
|
||||
p2->y = std::stoi(value) * tile_size;
|
||||
}
|
||||
else if (var == "[/enemy]")
|
||||
else if (var == "[/moving platform]")
|
||||
{
|
||||
}
|
||||
else
|
||||
@@ -343,18 +375,18 @@ void Map::render()
|
||||
SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
|
||||
SDL_RenderCopy(renderer, map_texture, &rect, NULL);
|
||||
|
||||
for (auto enemy : enemy_list)
|
||||
for (auto actor : actors)
|
||||
{
|
||||
enemy->render();
|
||||
actor->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza todas las variables
|
||||
void Map::update()
|
||||
{
|
||||
for (auto enemy : enemy_list)
|
||||
for (auto actor : actors)
|
||||
{
|
||||
enemy->update();
|
||||
actor->update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
32
source/map.h
32
source/map.h
@@ -4,7 +4,7 @@
|
||||
#include "utils.h"
|
||||
#include "asset.h"
|
||||
#include "const.h"
|
||||
#include "enemy.h"
|
||||
#include "actor_moving_platform.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
@@ -33,19 +33,19 @@ enum e_border
|
||||
class Map
|
||||
{
|
||||
private:
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
std::string room_up; // Identificador de la habitación que se encuentra arriba
|
||||
std::string room_down; // Identificador de la habitación que se encuentra abajp
|
||||
std::string room_left; // Identificador de la habitación que se encuentra a la izquierda
|
||||
std::string room_right; // Identificador de la habitación que se encuentra a la derecha
|
||||
std::string tileset_img; // Imagen con los graficos para la habitación
|
||||
std::vector<int> tilemap; // Indice de los tiles a dibujar en la habitación
|
||||
LTexture *texture_tile; // Textura con los graficos de los tiles habitación
|
||||
SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación
|
||||
std::vector<Enemy *> enemy_list; // Listado con los enemigos de la habitación
|
||||
color_t bgColor1; // Color superior del degradado de fondo
|
||||
color_t bgColor2; // Color inferior del degradado de fondo
|
||||
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||
std::string room_up; // Identificador de la habitación que se encuentra arriba
|
||||
std::string room_down; // Identificador de la habitación que se encuentra abajp
|
||||
std::string room_left; // Identificador de la habitación que se encuentra a la izquierda
|
||||
std::string room_right; // Identificador de la habitación que se encuentra a la derecha
|
||||
std::string tileset_img; // Imagen con los graficos para la habitación
|
||||
std::vector<int> tilemap; // Indice de los tiles a dibujar en la habitación
|
||||
LTexture *texture_tile; // Textura con los graficos de los tiles habitación
|
||||
SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación
|
||||
std::vector<Actor *> actors; // Listado con los actores de la habitación
|
||||
color_t bgColor1; // Color superior del degradado de fondo
|
||||
color_t bgColor2; // Color inferior del degradado de fondo
|
||||
|
||||
int tile_size; // Ancho del tile en pixels
|
||||
int map_width; // Ancho del mapa en tiles
|
||||
@@ -58,8 +58,8 @@ private:
|
||||
// Asigna variables a partir de dos cadenas
|
||||
bool setVars(std::string var, std::string value);
|
||||
|
||||
// Asigna variables a una estructura enemy_t
|
||||
bool setEnemy(enemy_t *enemy, std::string var, std::string value);
|
||||
// Asigna variables a una estructura actor_t
|
||||
bool setActor(actor_t *actor, SDL_Point *p1, SDL_Point *p2, std::string var, std::string value);
|
||||
|
||||
// Pinta el mapa de la habitación en la textura
|
||||
void fillMapTexture();
|
||||
|
||||
@@ -187,6 +187,10 @@ bool Prog::setFileList()
|
||||
asset->add("/data/actors/enemies/flying_eye_horn.png", bitmap);
|
||||
asset->add("/data/actors/enemies/flying_eye_horn.ani", data);
|
||||
|
||||
// Ficheros de actores
|
||||
asset->add("/data/actors/moving_platform.png", bitmap);
|
||||
asset->add("/data/actors/moving_platform.ani", data);
|
||||
|
||||
// Ficheros del logo
|
||||
asset->add("/data/logo/logo.png", bitmap);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user