polimorfise d'enemics
moving platforms
This commit is contained in:
@@ -204,6 +204,11 @@ void RoomLoader::parseEnemyBoundaries(const fkyaml::node& bounds_node, Enemy::Da
|
||||
auto RoomLoader::parseEnemyData(const fkyaml::node& enemy_node) -> Enemy::Data { // NOLINT(readability-convert-member-functions-to-static)
|
||||
Enemy::Data enemy;
|
||||
|
||||
// Enemy type (default: "path")
|
||||
if (enemy_node.contains("type")) {
|
||||
enemy.type = enemy_node["type"].get_value<std::string>();
|
||||
}
|
||||
|
||||
// Animation path
|
||||
if (enemy_node.contains("animation")) {
|
||||
enemy.animation_path = enemy_node["animation"].get_value<std::string>();
|
||||
@@ -323,6 +328,90 @@ void RoomLoader::parseItems(const fkyaml::node& yaml, Room::Data& room, bool ver
|
||||
}
|
||||
}
|
||||
|
||||
// Parsea los límites de movimiento de una plataforma
|
||||
void RoomLoader::parsePlatformBoundaries(const fkyaml::node& bounds_node, MovingPlatform::Data& platform) {
|
||||
// Formato: position1 y position2
|
||||
if (bounds_node.contains("position1")) {
|
||||
const auto& pos1 = bounds_node["position1"];
|
||||
if (pos1.contains("x")) {
|
||||
platform.x1 = pos1["x"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
if (pos1.contains("y")) {
|
||||
platform.y1 = pos1["y"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
}
|
||||
if (bounds_node.contains("position2")) {
|
||||
const auto& pos2 = bounds_node["position2"];
|
||||
if (pos2.contains("x")) {
|
||||
platform.x2 = pos2["x"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
if (pos2.contains("y")) {
|
||||
platform.y2 = pos2["y"].get_value<int>() * Tile::SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parsea los datos de una plataforma individual
|
||||
auto RoomLoader::parsePlatformData(const fkyaml::node& platform_node) -> MovingPlatform::Data {
|
||||
MovingPlatform::Data platform;
|
||||
|
||||
// Animation path
|
||||
if (platform_node.contains("animation")) {
|
||||
platform.animation_path = platform_node["animation"].get_value<std::string>();
|
||||
}
|
||||
|
||||
// Position (in tiles, convert to pixels)
|
||||
if (platform_node.contains("position")) {
|
||||
const auto& pos = platform_node["position"];
|
||||
if (pos.contains("x")) {
|
||||
platform.x = pos["x"].get_value<float>() * Tile::SIZE;
|
||||
}
|
||||
if (pos.contains("y")) {
|
||||
platform.y = pos["y"].get_value<float>() * Tile::SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
// Velocity (already in pixels/second)
|
||||
if (platform_node.contains("velocity")) {
|
||||
const auto& vel = platform_node["velocity"];
|
||||
if (vel.contains("x")) {
|
||||
platform.vx = vel["x"].get_value<float>();
|
||||
}
|
||||
if (vel.contains("y")) {
|
||||
platform.vy = vel["y"].get_value<float>();
|
||||
}
|
||||
}
|
||||
|
||||
// Boundaries (in tiles, convert to pixels)
|
||||
if (platform_node.contains("boundaries")) {
|
||||
parsePlatformBoundaries(platform_node["boundaries"], platform);
|
||||
}
|
||||
|
||||
// Optional frame
|
||||
platform.frame = platform_node.contains("frame")
|
||||
? platform_node["frame"].get_value_or<int>(-1)
|
||||
: -1;
|
||||
|
||||
return platform;
|
||||
}
|
||||
|
||||
// Parsea la lista de plataformas de la habitación
|
||||
void RoomLoader::parsePlatforms(const fkyaml::node& yaml, Room::Data& room, bool verbose) {
|
||||
if (!yaml.contains("platforms") || yaml["platforms"].is_null()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& platforms_node = yaml["platforms"];
|
||||
|
||||
for (const auto& platform_node : platforms_node) {
|
||||
room.platforms.push_back(parsePlatformData(platform_node));
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Loaded " << room.platforms.size() << " platforms\n";
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Carga una habitación desde un string YAML (para el editor de mapas, evita el resource pack)
|
||||
auto RoomLoader::loadFromString(const std::string& yaml_content, const std::string& file_name) -> Room::Data {
|
||||
@@ -333,6 +422,7 @@ auto RoomLoader::loadFromString(const std::string& yaml_content, const std::stri
|
||||
parseTilemap(yaml, room, file_name, false);
|
||||
parseEnemies(yaml, room, false);
|
||||
parseItems(yaml, room, false);
|
||||
parsePlatforms(yaml, room, false);
|
||||
} catch (const fkyaml::exception& e) {
|
||||
std::cerr << "YAML parsing error in " << file_name << ": " << e.what() << '\n';
|
||||
} catch (const std::exception& e) {
|
||||
@@ -367,6 +457,7 @@ auto RoomLoader::loadYAML(const std::string& file_path, bool verbose) -> Room::D
|
||||
parseTilemap(yaml, room, FILE_NAME, verbose);
|
||||
parseEnemies(yaml, room, verbose);
|
||||
parseItems(yaml, room, verbose);
|
||||
parsePlatforms(yaml, room, verbose);
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Room loaded successfully: " << FILE_NAME << '\n';
|
||||
|
||||
Reference in New Issue
Block a user