- [NEW] Càrrega i pintat d'enemics en l'habitació
This commit is contained in:
137
source/enemies.cpp
Normal file
137
source/enemies.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
#include "enemies.h"
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace enemies
|
||||
{
|
||||
std::unordered_map<std::string, enemy_t> enemies;
|
||||
|
||||
inline void trim(std::string& s) {
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
|
||||
}
|
||||
|
||||
void processKeyValue(const std::string& enemy_name, const std::string& section, const std::string& key, const std::string& value)
|
||||
{
|
||||
enemy_t& enemy = enemies[enemy_name];
|
||||
|
||||
if (section == "global") {
|
||||
if (key == "tileSetFile") {
|
||||
enemy.tileSetFile = images::getImage("enemies/"+value);
|
||||
} else if (key == "frame_width") {
|
||||
enemy.frame_width = std::stoi(value);
|
||||
} else if (key == "frame_height") {
|
||||
enemy.frame_height = std::stoi(value);
|
||||
}
|
||||
} else if (section == "animation") {
|
||||
if (key == "name") {
|
||||
enemy.animations.back().name = value;
|
||||
printf("animacio '%s' frames: ", value.c_str());
|
||||
} else if (key == "loop") {
|
||||
enemy.animations.back().loop = std::stoi(value);
|
||||
} else if (key == "speed") {
|
||||
enemy.animations.back().speed = std::stof(value);
|
||||
} else if (key == "frames") {
|
||||
std::stringstream ss(value);
|
||||
std::string frame;
|
||||
while (std::getline(ss, frame, ',')) {
|
||||
if (!frame.empty()) {
|
||||
enemy.animations.back().frames.emplace_back(std::stoi(frame));
|
||||
printf("%s,", frame.c_str());
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void loadEnemy(fs::path filename)
|
||||
{
|
||||
std::string enemy_name = filename.stem().string();
|
||||
|
||||
std::ifstream file(filename);
|
||||
if (!file) {
|
||||
std::cerr << "Failed to open file.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
std::string current_section = "global";
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
//std::cout << "'" << line << "'\n";
|
||||
// Trim and clean line, ignore empty lines and comments
|
||||
trim(line);
|
||||
if (line.empty() || line[0] == '#') continue;
|
||||
|
||||
// Remove inline comments
|
||||
/*std::size_t comment_pos = line.find('#');
|
||||
if (comment_pos != std::string::npos) {
|
||||
line = line.substr(0, comment_pos);
|
||||
trim(line);
|
||||
}*/
|
||||
|
||||
// Check if entering or exiting a section
|
||||
if (line[0] == '[')
|
||||
{
|
||||
std::size_t closing_pos = line.find(']');
|
||||
if (closing_pos != std::string::npos && closing_pos > 1)
|
||||
{
|
||||
std::string section = line.substr(1,closing_pos-1);
|
||||
if (section[0] == '/') {
|
||||
current_section = "global";
|
||||
} else {
|
||||
current_section = section;
|
||||
if (section == "animation") enemies[enemy_name].animations.emplace_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get and process key/value
|
||||
std::size_t eq_pos = line.find('=');
|
||||
if (eq_pos != std::string::npos) {
|
||||
std::string key = line.substr(0, eq_pos);
|
||||
std::string value = line.substr(eq_pos + 1);
|
||||
trim(key);
|
||||
trim(value);
|
||||
processKeyValue(enemy_name, current_section, key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void load()
|
||||
{
|
||||
// Get all enemies
|
||||
fs::path target_dir = "./data/enemies";
|
||||
std::vector<fs::path> enemy_files;
|
||||
|
||||
try {
|
||||
for (const auto& entry : fs::directory_iterator(target_dir)) {
|
||||
if (entry.is_regular_file() && entry.path().extension() == ".ani") {
|
||||
enemy_files.push_back(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& path : enemy_files) {
|
||||
std::cout << "Processing " << path.filename() << '\n';
|
||||
loadEnemy(path);
|
||||
}
|
||||
|
||||
} catch (const fs::filesystem_error& e) {
|
||||
std::cerr << "Filesystem error: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enemy_t &get(std::string name)
|
||||
{
|
||||
return enemies[name];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user