feat(decals): tiles de decal de sòl compten com a pintats des del load

This commit is contained in:
JailDesigner committed 2026-04-30 19:16:21 +02:00
1 parent 7cd7b4dc4f
commit 7d0aca506a
6 files changed
+74 -12

No files matched your search

-1
View File
@@ -37,5 +37,4 @@ class Hole : public StaticHazard {
};
// TODO(hole-feature): afegir reset de `falling_through_hole_` i `hole_clip_bottom_y_` a `Player::disable()` per simetria.
// TODO(hole-feature): si apareix un quart callsite del càlcul "pixel→tile" o "tileRect", factoritzar a `Map::` (avui: Mummy::reactToCurrentTile, Mummy::isDecalAheadInDir, Player::integrateJump).
// TODO(hole-feature): afegir tests dedicats del comportament de la mòmia amb forats (no requereixen Player real).
+7 -11
View File
@@ -291,13 +291,9 @@ void Mummy::reactToCurrentTile() {
// per a una direcció negativa, el menor (`x`); per a zero, qualsevol
// (no hi ha avenç en aquest eix).
const auto BOX = getCollisionRect();
const auto TS = static_cast<float>(Map::tile_size);
const auto ORIG_X = static_cast<float>(Map::tilemap_origin_x);
const auto ORIG_Y = static_cast<float>(Map::tilemap_origin_y);
const float LEAD_X = (dir_x_ > 0.0F) ? (BOX.x + BOX.w - 1.0F) : BOX.x;
const float LEAD_Y = (dir_y_ > 0.0F) ? (BOX.y + BOX.h - 1.0F) : BOX.y;
const int CUR_COL = static_cast<int>(std::floor((LEAD_X - ORIG_X) / TS));
const int CUR_ROW = static_cast<int>(std::floor((LEAD_Y - ORIG_Y) / TS));
const auto [CUR_COL, CUR_ROW] = Map::pixelToTile(LEAD_X, LEAD_Y);
// Sense canvi de tile no hi ha res a fer — això filtra també tots els
// ticks intra-tile (la majoria, ~16 frames per tile a velocitats típiques).
@@ -318,9 +314,10 @@ void Mummy::reactToCurrentTile() {
// Si el tile nou conté un decal trigger (Hole o futur pinchos plans,
// plaques, etc.), mitja volta immediata. `changeDirection` també
// selecciona l'animació cardinal corresponent.
const auto TS = static_cast<float>(Map::tile_size);
const SDL_FRect TILE_RECT{
.x = (static_cast<float>(CUR_COL) * TS) + ORIG_X,
.y = (static_cast<float>(CUR_ROW) * TS) + ORIG_Y,
.x = (static_cast<float>(CUR_COL) * TS) + static_cast<float>(Map::tilemap_origin_x),
.y = (static_cast<float>(CUR_ROW) * TS) + static_cast<float>(Map::tilemap_origin_y),
.w = TS,
.h = TS,
};
@@ -444,8 +441,6 @@ auto Mummy::isDecalAheadInDir(float dx_signed, float dy_signed) const -> bool {
// tile abans del veritablement adjacent al decal.
const auto BOX = getCollisionRect();
const auto TS = static_cast<float>(Map::tile_size);
const auto ORIG_X = static_cast<float>(Map::tilemap_origin_x);
const auto ORIG_Y = static_cast<float>(Map::tilemap_origin_y);
const auto SIGN_OF = [](float v) -> float {
if (v > 0.0F) { return 1.0F; }
if (v < 0.0F) { return -1.0F; }
@@ -453,8 +448,9 @@ auto Mummy::isDecalAheadInDir(float dx_signed, float dy_signed) const -> bool {
};
const float CENTER_X = BOX.x + (BOX.w * 0.5F);
const float CENTER_Y = BOX.y + (BOX.h * 0.5F);
const float TILE_X = (std::floor((CENTER_X - ORIG_X) / TS) * TS) + ORIG_X;
const float TILE_Y = (std::floor((CENTER_Y - ORIG_Y) / TS) * TS) + ORIG_Y;
const auto [CUR_COL, CUR_ROW] = Map::pixelToTile(CENTER_X, CENTER_Y);
const float TILE_X = (static_cast<float>(CUR_COL) * TS) + static_cast<float>(Map::tilemap_origin_x);
const float TILE_Y = (static_cast<float>(CUR_ROW) * TS) + static_cast<float>(Map::tilemap_origin_y);
const SDL_FRect NEXT{
.x = TILE_X + (SIGN_OF(dx_signed) * TS),
.y = TILE_Y + (SIGN_OF(dy_signed) * TS),
@@ -420,6 +420,32 @@ void EntityManager::loadFromLevel(const std::string& level_yaml_path) {
if (OK) { ++loaded; }
}
// Broadcast inicial: marca com a "pintats" tots els tiles ocupats per
// decals de sòl (Hole, i futurs pinchos plans, plaques, charcos...).
// Imprescindible perquè el jugador no pot trepitjar aquests tiles
// (cauria al forat) i els sarcòfags vecins no podrien completar el
// perímetre — la sala seria incompletable. Usa el canal semàntic
// `tile_painted` (lògica de joc), no `on_tile_entered` (presència
// física del jugador). Ho fem aquí, després del bucle de spawns,
// perquè tots els sarcòfags ja estan registrats al fan-out (la
// subscripció del manager és al ctor, anterior a qualsevol load).
int decal_broadcast = 0;
for (const auto& entity : entities_) {
if (!entity->isFloorDecal()) { continue; }
const auto BOX = entity->getCollisionRect();
const auto [COL, ROW] = Map::pixelToTile(BOX.x, BOX.y);
bus_.publish(Event{
.name = "tile_painted",
.payload = TilePos{.col = COL, .row = ROW},
.player_id = -1,
});
++decal_broadcast;
}
if (decal_broadcast > 0) {
std::cout << "EntityManager: " << decal_broadcast
<< " tile_painted broadcast inicial (decals de sòl)\n";
}
std::cout << "EntityManager: " << loaded << " entitats carregades des de "
<< level_yaml_path << '\n';
}