197 lines
7.0 KiB
C++
197 lines
7.0 KiB
C++
#include "enemy_engine.h"
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
// Constructor
|
|
EnemyEngine::EnemyEngine(SDL_Renderer *renderer, Asset *asset, Player *player, Map *map)
|
|
{
|
|
this->renderer = renderer;
|
|
this->asset = asset;
|
|
this->player = player;
|
|
this->map = map;
|
|
}
|
|
|
|
// Destructor
|
|
EnemyEngine::~EnemyEngine()
|
|
{
|
|
}
|
|
|
|
// Pinta los enemigos en pantalla
|
|
void EnemyEngine::render()
|
|
{
|
|
for (auto enemy : enemies)
|
|
{
|
|
enemy->render();
|
|
}
|
|
}
|
|
|
|
// Actualiza las variables del objeto
|
|
void EnemyEngine::update()
|
|
{
|
|
for (auto enemy : enemies)
|
|
{
|
|
enemy->update();
|
|
}
|
|
}
|
|
|
|
// Carga las variables desde un fichero
|
|
bool EnemyEngine::load(std::string file_path)
|
|
{
|
|
// Indicador de éxito en la carga
|
|
bool success = true;
|
|
|
|
std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1);
|
|
std::string line;
|
|
std::ifstream file(file_path);
|
|
|
|
// El fichero se puede abrir
|
|
if (file.good())
|
|
{
|
|
// Procesa el fichero linea a linea
|
|
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]")
|
|
{
|
|
do
|
|
{
|
|
std::getline(file, line);
|
|
|
|
if (line == "[moving platform]")
|
|
{
|
|
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));
|
|
}
|
|
|
|
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;
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Cierra el fichero
|
|
printf("Closing file %s\n\n", filename.c_str());
|
|
file.close();
|
|
}
|
|
// El fichero no se puede abrir
|
|
else
|
|
{
|
|
printf("Warning: Unable to open %s file\n", filename.c_str());
|
|
success = false;
|
|
}
|
|
|
|
return success;
|
|
}
|