units
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
#include "unitconfig.h"
|
||||
#include "yamlparser.h"
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QDebug>
|
||||
|
||||
UnitConfigManager::UnitConfigManager()
|
||||
{
|
||||
}
|
||||
|
||||
bool UnitConfigManager::loadFromDirectory(const QString &dirPath)
|
||||
{
|
||||
QDir dir(dirPath);
|
||||
if (!dir.exists()) {
|
||||
qWarning() << "Unit config directory does not exist:" << dirPath;
|
||||
return false;
|
||||
}
|
||||
|
||||
QStringList filters;
|
||||
filters << "*.yaml" << "*.yml";
|
||||
QFileInfoList fileList = dir.entryInfoList(filters, QDir::Files);
|
||||
|
||||
for (const QFileInfo &fileInfo : fileList) {
|
||||
loadConfigFile(fileInfo.absoluteFilePath());
|
||||
}
|
||||
|
||||
return !m_configs.isEmpty();
|
||||
}
|
||||
|
||||
bool UnitConfigManager::loadConfigFile(const QString &filePath)
|
||||
{
|
||||
YamlNode root = YamlParser::parseFile(filePath);
|
||||
if (root.children.isEmpty()) {
|
||||
qWarning() << "Failed to parse unit config YAML:" << filePath;
|
||||
return false;
|
||||
}
|
||||
|
||||
UnitConfig cfg;
|
||||
cfg.id = root.hasChild("id") ? root["id"].asString() : QFileInfo(filePath).baseName();
|
||||
cfg.name = root.hasChild("name") ? root["name"].asString() : cfg.id;
|
||||
cfg.category = stringToUnitCategory(root.hasChild("category") ? root["category"].asString() : "bouwen");
|
||||
cfg.health = root.hasChild("health") ? root["health"].asInt(100) : 100;
|
||||
cfg.movementSpeed = root.hasChild("movement_speed") ? root["movement_speed"].asFloat(2.0f) : 2.0f;
|
||||
cfg.cost = root.hasChild("cost") ? root["cost"].asInt(20) : 20;
|
||||
cfg.attackDamage = root.hasChild("attack_damage") ? root["attack_damage"].asInt(0) : 0;
|
||||
cfg.attackRange = root.hasChild("attack_range") ? root["attack_range"].asInt(1) : 1;
|
||||
cfg.iconPath = root.hasChild("icon") ? root["icon"].asString() : "";
|
||||
|
||||
if (root.hasChild("actions")) {
|
||||
cfg.actions = root["actions"].listValues;
|
||||
}
|
||||
// Always ensure verplaatsen action is present
|
||||
if (!cfg.actions.contains("verplaatsen")) {
|
||||
cfg.actions.prepend("verplaatsen");
|
||||
}
|
||||
|
||||
m_configs[cfg.id] = cfg;
|
||||
return true;
|
||||
}
|
||||
|
||||
const UnitConfig* UnitConfigManager::getConfig(const QString &id) const
|
||||
{
|
||||
auto it = m_configs.find(id);
|
||||
if (it != m_configs.end()) {
|
||||
return &it.value();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
Reference in New Issue
Block a user