Debugando la clase enemy
This commit is contained in:
@@ -3,17 +3,27 @@
|
||||
#include <sstream>
|
||||
|
||||
// Constructor
|
||||
EnemyEngine::EnemyEngine(SDL_Renderer *renderer, Asset *asset, Player *player, Map *map)
|
||||
EnemyEngine::EnemyEngine(SDL_Renderer *renderer, Asset *asset, Player *player, Map *map, std::string file)
|
||||
{
|
||||
this->renderer = renderer;
|
||||
this->asset = asset;
|
||||
this->player = player;
|
||||
this->map = map;
|
||||
|
||||
load(file);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
EnemyEngine::~EnemyEngine()
|
||||
{
|
||||
// Elimina todos los enemigos anteriores
|
||||
for (auto enemy : enemies)
|
||||
{
|
||||
delete enemy;
|
||||
}
|
||||
enemies.clear();
|
||||
|
||||
delete enemy;
|
||||
}
|
||||
|
||||
// Pinta los enemigos en pantalla
|
||||
@@ -23,6 +33,7 @@ void EnemyEngine::render()
|
||||
{
|
||||
enemy->render();
|
||||
}
|
||||
enemy->render();
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
@@ -32,6 +43,7 @@ void EnemyEngine::update()
|
||||
{
|
||||
enemy->update();
|
||||
}
|
||||
enemy->update();
|
||||
}
|
||||
|
||||
// Carga las variables desde un fichero
|
||||
@@ -51,133 +63,33 @@ bool EnemyEngine::load(std::string file_path)
|
||||
printf("Reading file %s\n", filename.c_str());
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
// Si la linea contiene el texto [tilemap] se realiza el proceso de carga del fichero tmx
|
||||
if (line == "[tilemap]")
|
||||
{
|
||||
do
|
||||
{
|
||||
std::getline(file, line);
|
||||
if (line.find(".tmx") != std::string::npos)
|
||||
{
|
||||
std::ifstream file2(asset->get(line)); // Abre el fichero tmx
|
||||
if (file2.good())
|
||||
{
|
||||
bool data_read = false;
|
||||
while (std::getline(file2, line)) // Lee el fichero linea a linea
|
||||
{
|
||||
if (!data_read)
|
||||
{ // Lee lineas hasta que encuentre donde empiezan los datos del mapa
|
||||
int pos = 0;
|
||||
do
|
||||
{
|
||||
std::getline(file2, line);
|
||||
pos = line.find("data encoding");
|
||||
} while (pos == std::string::npos);
|
||||
|
||||
do
|
||||
{ // Se introducen los valores separados por comas en un vector
|
||||
data_read = true;
|
||||
std::getline(file2, line);
|
||||
if (line != "</data>")
|
||||
{
|
||||
std::stringstream ss(line);
|
||||
std::string tmp;
|
||||
while (getline(ss, tmp, ','))
|
||||
{
|
||||
tilemap.push_back(std::stoi(tmp));
|
||||
}
|
||||
}
|
||||
} while (line != "</data>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (line != "[/tilemap]");
|
||||
}
|
||||
|
||||
// Si la linea contiene el texto [actor] se realiza el proceso de carga de los actores
|
||||
else if (line == "[actors]")
|
||||
if (line == "[path]")
|
||||
{
|
||||
enemy_t enemy;
|
||||
enemy.asset = asset;
|
||||
enemy.renderer = renderer;
|
||||
SDL_Point p1, p2;
|
||||
|
||||
do
|
||||
{
|
||||
std::getline(file, line);
|
||||
|
||||
if (line == "[moving platform]")
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
|
||||
// Procesa las dos subcadenas
|
||||
if (!setEnemy(&enemy, &p1, &p2, line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
actor_t actor;
|
||||
actor.asset = asset;
|
||||
actor.renderer = renderer;
|
||||
actor.name = a_moving_platform;
|
||||
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 moving platform loaded\n\n");
|
||||
actors.push_back(new ActorMovingPlatform(actor, p1, p2));
|
||||
printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (line == "[diamond]")
|
||||
{
|
||||
actor_t actor;
|
||||
actor.asset = asset;
|
||||
actor.renderer = renderer;
|
||||
actor.name = a_diamond;
|
||||
actor.vx = 0.0f;
|
||||
actor.vy = 0.0f;
|
||||
SDL_Point p1, p2;
|
||||
} while (line != "[/path]");
|
||||
|
||||
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 != "[/diamond]");
|
||||
|
||||
// Comprueba si el actor no ha sido recogido previamente
|
||||
if (!itemTracker->hasBeenPicked(name, {(int)actor.x, (int)actor.y}))
|
||||
{
|
||||
printf("** actor diamond loaded\n\n");
|
||||
actors.push_back(new ActorDiamond(actor));
|
||||
}
|
||||
}
|
||||
|
||||
} while (line != "[/actors]");
|
||||
}
|
||||
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
else
|
||||
{
|
||||
// Encuentra la posición del caracter '='
|
||||
int pos = line.find("=");
|
||||
// Procesa las dos subcadenas
|
||||
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length())))
|
||||
{
|
||||
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
|
||||
success = false;
|
||||
}
|
||||
printf("** enemy path loaded\n\n");
|
||||
//enemies.push_back(new EnemyPath(enemy, p1, p2));
|
||||
enemy = new EnemyPath(enemy, p1, p2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,3 +106,86 @@ bool EnemyEngine::load(std::string file_path)
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura enemy_t
|
||||
bool EnemyEngine::setEnemy(enemy_t *enemy, SDL_Point *p1, SDL_Point *p2, std::string var, std::string value)
|
||||
{
|
||||
const int tile_size = 8;
|
||||
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
if (var == "tileset")
|
||||
{
|
||||
enemy->tileset = value;
|
||||
}
|
||||
else if (var == "animation")
|
||||
{
|
||||
enemy->animation = value;
|
||||
}
|
||||
else if (var == "width")
|
||||
{
|
||||
enemy->w = std::stof(value);
|
||||
}
|
||||
else if (var == "height")
|
||||
{
|
||||
enemy->h = std::stof(value);
|
||||
}
|
||||
else if (var == "x")
|
||||
{
|
||||
enemy->x = std::stof(value) * tile_size;
|
||||
}
|
||||
else if (var == "y")
|
||||
{
|
||||
enemy->y = std::stof(value) * tile_size;
|
||||
}
|
||||
else if (var == "vx")
|
||||
{
|
||||
enemy->vx = std::stof(value);
|
||||
}
|
||||
else if (var == "vy")
|
||||
{
|
||||
enemy->vy = std::stof(value);
|
||||
}
|
||||
else if (var == "x1")
|
||||
{
|
||||
p1->x = std::stoi(value) * tile_size;
|
||||
}
|
||||
else if (var == "x2")
|
||||
{
|
||||
p2->x = std::stoi(value) * tile_size;
|
||||
}
|
||||
else if (var == "y1")
|
||||
{
|
||||
p1->y = std::stoi(value) * tile_size;
|
||||
}
|
||||
else if (var == "y2")
|
||||
{
|
||||
p2->y = std::stoi(value) * tile_size;
|
||||
}
|
||||
else if (var == "[/path]")
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Carga el vector de enemigos
|
||||
//bool EnemyEngine::setEnemies(std::string file)
|
||||
//{
|
||||
// // Elimina todos los enemigos anteriores
|
||||
// for (auto enemy : enemies)
|
||||
// {
|
||||
// delete enemy;
|
||||
// }
|
||||
// enemies.clear();
|
||||
//
|
||||
// // Carga el vector de enemigos desde un fichero
|
||||
// const bool success = load(file);
|
||||
//
|
||||
// return success;
|
||||
//}
|
||||
Reference in New Issue
Block a user