Files
aee-arcade/source/game/map/map.cpp
T
JailDesigner 4da05b3d76 chore(release): v0.21.0 — empaquetat de recursos (AE26) i neteja de fonts
- Nou format de pack AE26 v1 (XOR + djb2), eina `pack_resources`,
  regeneració automàtica de `build/resources.pack` a cada build.
- `Resource::Helper` amb init/shutdown, fallback a disc només en Debug
  (apunta al source tree); Release exigeix pack present.
- Callers de YAML migrats a `loadFile`+deserialize des de buffer.
- Arrel neta: el pack viu a `build/`, eliminat `sync_data`.
- Placeholder font -> `ari-w9500-condensed-display`; esborrades variants
  bold/condensed no usades a `data/fonts/`.
2026-04-23 08:51:21 +02:00

128 lines
5.0 KiB
C++

#include "game/map/map.hpp"
#include <iostream>
#include <stdexcept>
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp"
namespace {
// AABB amb inequitat estricta: dos rects que es toquen exactament (la
// cantonada o una vora al píxel) NO es consideren intersecció. SDL3
// SDL_HasRectIntersectionFloat tracta el toc com a intersecció, cosa
// que bloqueja el moviment als encreuaments del nostre laberint.
auto overlapStrict(const SDL_FRect& a, const SDL_FRect& b) -> bool {
return (a.x + a.w) > b.x && (b.x + b.w) > a.x && (a.y + a.h) > b.y && (b.y + b.h) > a.y;
}
} // namespace
namespace Map {
void loadFrom(const std::string& yaml_path) {
auto bytes = Resource::Helper::loadFile(yaml_path);
if (bytes.empty()) {
throw std::runtime_error("Map: no s'ha pogut obrir " + yaml_path);
}
const auto* begin = reinterpret_cast<const char*>(bytes.data());
const auto* end = begin + bytes.size();
auto yaml = fkyaml::node::deserialize(begin, end);
if (!yaml.contains("geometry")) {
throw std::runtime_error("Map: manca bloc `geometry:` a " + yaml_path);
}
const auto& g = yaml["geometry"];
grid_cols = g["grid_cols"].get_value<int>();
grid_rows = g["grid_rows"].get_value<int>();
sarc_w = g["sarc_w"].get_value<int>();
sarc_h = g["sarc_h"].get_value<int>();
pitch_x = g["pitch_x"].get_value<int>();
pitch_y = g["pitch_y"].get_value<int>();
hud_h = g["hud_h"].get_value<int>();
back_wall_h = g["back_wall_h"].get_value<int>();
side_wall_w = g["side_wall_w"].get_value<int>();
// Tile size + origen del tilemap (per a consumidors externs que
// converteixen px ↔ tile). Fallbacks conservadors: tile_size per
// defecte 16, origen [0, 0]. Si falten del YAML, només el sistema
// d'entitats es veu afectat; la lògica de col·lisions actual no.
tile_size = yaml.contains("tile_size") ? yaml["tile_size"].get_value<int>() : 16;
tilemap_origin_x = 0;
tilemap_origin_y = 0;
if (yaml.contains("origin")) {
const auto& o = yaml["origin"];
if (o.is_sequence() && o.size() == 2) {
tilemap_origin_x = o[0].get_value<int>();
tilemap_origin_y = o[1].get_value<int>();
}
}
// Derivats: el passadís és el sobrant del pitch sobre el sarcòfag, i
// l'exterior reutilitza la mateixa amplada per mantenir el pas lliure.
corridor_w = pitch_x - sarc_w;
corridor_h = pitch_y - sarc_h;
outer_corridor = corridor_w;
board_w = ((grid_cols - 1) * pitch_x) + sarc_w;
board_h = ((grid_rows - 1) * pitch_y) + sarc_h;
origin_x = side_wall_w + outer_corridor;
origin_y = hud_h + back_wall_h + outer_corridor;
playfield_x = side_wall_w;
playfield_y = hud_h + back_wall_h;
playfield_w = board_w + (2 * outer_corridor);
playfield_h = board_h + (2 * outer_corridor);
std::cout << "Map: geometria " << grid_cols << "x" << grid_rows
<< " sarcs " << sarc_w << "x" << sarc_h
<< " pitch " << pitch_x << "x" << pitch_y
<< " des de " << yaml_path << '\n';
}
auto getSarcRect(int col, int row) -> SDL_FRect {
return {
.x = static_cast<float>(origin_x + (col * pitch_x)),
.y = static_cast<float>(origin_y + (row * pitch_y)),
.w = static_cast<float>(sarc_w),
.h = static_cast<float>(sarc_h),
};
}
auto collides(const SDL_FRect& base_box) -> bool {
// Recorregut trivial dels sarcòfags. Amb una graella petita no val
// la pena indexar per col/row prèviament; si algun dia creix, es pot
// calcular el rang de columnes/files candidates a partir de base_box.
for (int row = 0; row < grid_rows; ++row) {
for (int col = 0; col < grid_cols; ++col) {
if (overlapStrict(base_box, getSarcRect(col, row))) { return true; }
}
}
return false;
}
auto insidePlayfield(const SDL_FRect& box) -> bool {
return box.x >= static_cast<float>(playfield_x) && box.y >= static_cast<float>(playfield_y) && box.x + box.w <= static_cast<float>(playfield_x + playfield_w) && box.y + box.h <= static_cast<float>(playfield_y + playfield_h);
}
auto makeInfo() -> Info {
return {
.playfield_x = playfield_x,
.playfield_y = playfield_y,
.playfield_w = playfield_w,
.playfield_h = playfield_h,
.pitch_x = pitch_x,
.pitch_y = pitch_y,
.grid_cols = grid_cols,
.grid_rows = grid_rows,
.tile_size = tile_size,
.tilemap_origin_x = tilemap_origin_x,
.tilemap_origin_y = tilemap_origin_y,
.collides = &collides,
.inside_playfield = &insidePlayfield,
};
}
} // namespace Map