treballant en el extendedMap per a les colisións fora de pantalla

This commit is contained in:
2026-04-09 21:46:45 +02:00
parent 2120641c3d
commit 4f890586f1
20 changed files with 326 additions and 383 deletions

View File

@@ -66,8 +66,8 @@ void Player::update(float delta_time) {
checkFalling();
// 6. Kill tiles
auto [ktc, kox, koy] = getCollisionContext();
if (ktc.touchesKillTile(x_ + kox, y_ + koy, WIDTH, HEIGHT)) {
const auto& ktc = room_->getTileCollider();
if (ktc.touchesKillTile(x_, y_, WIDTH, HEIGHT)) {
markAsDead();
}
@@ -200,11 +200,11 @@ void Player::handleJumpAndDrop() {
// Drop-through: plataforma passable
if (wanna_down_ && state_ == State::ON_GROUND) {
auto [tc, ox, oy] = getCollisionContext();
float foot_y = (y_ + oy) + HEIGHT;
const auto& tc = room_->getTileCollider();
float foot_y = y_ + HEIGHT;
int foot_row = static_cast<int>(foot_y) / Tile::SIZE;
int left_col = static_cast<int>(x_ + ox) / Tile::SIZE;
int right_col = static_cast<int>((x_ + ox) + WIDTH - 1) / Tile::SIZE;
int left_col = static_cast<int>(x_) / Tile::SIZE;
int right_col = static_cast<int>(x_ + WIDTH - 1) / Tile::SIZE;
for (int col = left_col; col <= right_col; ++col) {
if (tc.getTileAt(col, foot_row) == TileCollider::Tile::PASSABLE) {
@@ -230,43 +230,27 @@ void Player::startJump() {
// ============================================================================
void Player::moveHorizontal(float delta_time) {
if (vx_ == 0.0F) {
// Aunque no haya movimiento horizontal, si estamos en slope hay que seguirla
// (por si la gravedad nos ha movido o algo)
return;
}
auto [tc, ox, oy] = getCollisionContext();
const auto& tc = room_->getTileCollider();
float new_x = x_ + (vx_ * delta_time);
// Colisión con paredes (room actual)
if (vx_ < 0.0F) {
float wall = tc.checkWallLeft(new_x + ox, y_ + oy, WIDTH, HEIGHT);
if (wall != Collision::NONE) {
new_x = wall - ox;
}
} else {
float wall = tc.checkWallRight(new_x + ox, y_ + oy, WIDTH, HEIGHT);
if (wall != Collision::NONE) {
new_x = wall - WIDTH - ox;
}
// Comprobar ambos muros siempre (el tilemap extendido incluye paredes de rooms
// adyacentes; comprobar ambos lados evita solapamiento en zona de borde)
float wall = tc.checkWallLeft(new_x, y_, WIDTH, HEIGHT);
if (wall != Collision::NONE && wall > new_x) {
new_x = wall;
}
wall = tc.checkWallRight(new_x, y_, WIDTH, HEIGHT);
if (wall != Collision::NONE) {
float corrected = wall - WIDTH;
if (corrected < new_x) { new_x = corrected; }
}
// Cross-room: comprobar muros en rooms adyacentes
auto cross = getCrossRoomChecks();
checkCrossRoomWallH(new_x, cross);
x_ = new_x;
// Si estamos en una slope, ajustar Y para seguirla
if (state_ == State::ON_SLOPE) {
followSlope();
}
// Si estamos en suelo plano, detectar entrada a slope
if (state_ == State::ON_GROUND) {
detectSlopeEntry();
}
// Slope following y detección solo cuando hay movimiento horizontal
if (vx_ == 0.0F) { return; }
if (state_ == State::ON_SLOPE) { followSlope(); }
if (state_ == State::ON_GROUND) { detectSlopeEntry(); }
}
// Ajusta Y del jugador para seguir la superficie de la slope mientras camina.
@@ -274,18 +258,18 @@ void Player::moveHorizontal(float delta_time) {
// actual y la inferior (las slopes en escalera bajan una fila por tile).
// Si no encuentra slope, llama a exitSlope().
void Player::followSlope() {
auto [tc, ox, oy] = getCollisionContext();
const auto& tc = room_->getTileCollider();
// SLOPE_L (\): pie izquierdo. SLOPE_R (/): pie derecho.
float foot_x = (slope_type_ == TileCollider::Tile::SLOPE_L) ? (x_ + ox) : (x_ + ox) + WIDTH - 1;
float foot_x = (slope_type_ == TileCollider::Tile::SLOPE_L) ? x_ : x_ + WIDTH - 1;
// Calcular Y en la slope actual
float surface_y = tc.getSlopeY(slope_tile_x_, slope_tile_y_, foot_x);
y_ = surface_y - HEIGHT - oy;
y_ = surface_y - HEIGHT;
// Comprobar si hemos salido del tile actual
int foot_tile_x = static_cast<int>(foot_x) / Tile::SIZE;
int foot_tile_y = static_cast<int>((y_ + oy) + HEIGHT) / Tile::SIZE;
int foot_tile_y = static_cast<int>(y_ + HEIGHT) / Tile::SIZE;
if (foot_tile_x != slope_tile_x_ || foot_tile_y != slope_tile_y_) {
// Buscar slope en el tile calculado y en el de abajo (la escalera de slopes
@@ -297,7 +281,7 @@ void Player::followSlope() {
slope_tile_y_ = row;
slope_type_ = new_tile;
surface_y = tc.getSlopeY(slope_tile_x_, slope_tile_y_, foot_x);
y_ = surface_y - HEIGHT - oy;
y_ = surface_y - HEIGHT;
return;
}
}
@@ -310,16 +294,16 @@ void Player::followSlope() {
// entre filas cuando se sale por el extremo inferior de la slope).
// Si hay suelo, snapea al borde del tile. Si no, empieza a caer.
void Player::exitSlope() {
auto [tc, ox, oy] = getCollisionContext();
float foot_y = (y_ + oy) + HEIGHT;
const auto& tc = room_->getTileCollider();
float foot_y = y_ + HEIGHT;
// Comprobar suelo en la fila actual y la siguiente (al salir por abajo de una slope,
// los pies pueden estar en el último pixel de la fila, justo antes del suelo)
for (int check = 0; check <= 1; ++check) {
float check_y = foot_y + check;
if (tc.hasGroundBelow(x_ + ox, check_y, WIDTH)) {
if (tc.hasGroundBelow(x_, check_y, WIDTH)) {
int row = static_cast<int>(check_y) / Tile::SIZE;
y_ = static_cast<float>(row * Tile::SIZE) - HEIGHT - oy;
y_ = static_cast<float>(row * Tile::SIZE) - HEIGHT;
transitionToState(State::ON_GROUND);
return;
}
@@ -333,12 +317,12 @@ void Player::exitSlope() {
// Las slopes en escalera están una fila arriba del suelo, así que checkSlopeBelow
// también mira la fila superior.
void Player::detectSlopeEntry() {
auto [tc, ox, oy] = getCollisionContext();
float foot_y = (y_ + oy) + HEIGHT;
const auto& tc = room_->getTileCollider();
float foot_y = y_ + HEIGHT;
auto slope = tc.checkSlopeBelow(x_ + ox, foot_y, WIDTH);
auto slope = tc.checkSlopeBelow(x_, foot_y, WIDTH);
if (slope.on_slope) {
y_ = slope.surface_y - HEIGHT - oy;
y_ = slope.surface_y - HEIGHT;
slope_tile_x_ = slope.tile_x;
slope_tile_y_ = slope.tile_y;
slope_type_ = slope.type;
@@ -353,29 +337,27 @@ void Player::detectSlopeEntry() {
void Player::moveVertical(float delta_time) {
if (state_ != State::ON_AIR) { return; }
auto [tc, ox, oy] = getCollisionContext();
const auto& tc = room_->getTileCollider();
float displacement = vy_ * delta_time;
float old_y = y_;
if (vy_ < 0.0F) {
// Subiendo: comprobar techo
float new_y = y_ + displacement;
float ceiling = tc.checkCeiling(x_ + ox, new_y + oy, WIDTH);
float ceiling = tc.checkCeiling(x_, new_y, WIDTH);
if (ceiling != Collision::NONE) {
y_ = ceiling - oy;
y_ = ceiling;
vy_ = 0.0F;
} else {
y_ = new_y;
}
} else if (vy_ > 0.0F) {
// Bajando: comprobar suelo
float foot_y = (y_ + oy) + HEIGHT;
float foot_y = y_ + HEIGHT;
float new_foot_y = foot_y + displacement;
auto hit = tc.checkFloor(x_ + ox, foot_y, WIDTH, new_foot_y);
auto hit = tc.checkFloor(x_, foot_y, WIDTH, new_foot_y);
if (hit.y != Collision::NONE) {
y_ = hit.y - HEIGHT - oy;
y_ = hit.y - HEIGHT;
if (hit.type == TileCollider::Tile::SLOPE_L || hit.type == TileCollider::Tile::SLOPE_R) {
slope_tile_x_ = hit.tile_x;
slope_tile_y_ = hit.tile_y;
@@ -391,10 +373,6 @@ void Player::moveVertical(float delta_time) {
#endif
}
}
// Cross-room: comprobar suelo/techo en rooms adyacentes
auto cross = getCrossRoomChecks();
checkCrossRoomFloor(old_y, cross);
}
// ============================================================================
@@ -404,7 +382,7 @@ void Player::moveVertical(float delta_time) {
void Player::checkFalling() {
if (state_ == State::ON_AIR) { return; }
auto [tc, ox, oy] = getCollisionContext();
const auto& tc = room_->getTileCollider();
if (state_ == State::ON_SLOPE) {
// Verificar que el tile de slope sigue existiendo
@@ -419,14 +397,15 @@ void Player::checkFalling() {
// ON_GROUND: si está sobre una plataforma móvil, no comprobar tiles
if (on_platform_) { return; }
// ON_GROUND: comprobar si sigue habiendo suelo
float foot_y = (y_ + oy) + HEIGHT;
if (!tc.hasGroundBelow(x_ + ox, foot_y, WIDTH)) {
// ON_GROUND: comprobar si sigue habiendo suelo (el tilemap extendido
// incluye tiles de las rooms adyacentes, así que no hace falta cross-room)
float foot_y = y_ + HEIGHT;
if (!tc.hasGroundBelow(x_, foot_y, WIDTH)) {
// Sticking: si no hay suelo pero hay slope debajo, snapear a ella
// para transición suave suelo→slope (bajada de rampas sin caer)
auto slope = tc.checkSlopeBelow(x_ + ox, foot_y, WIDTH);
auto slope = tc.checkSlopeBelow(x_, foot_y, WIDTH);
if (slope.on_slope) {
y_ = slope.surface_y - HEIGHT - oy;
y_ = slope.surface_y - HEIGHT;
slope_tile_x_ = slope.tile_x;
slope_tile_y_ = slope.tile_y;
slope_type_ = slope.type;
@@ -434,11 +413,6 @@ void Player::checkFalling() {
return;
}
// Cross-room: comprobar suelo en rooms adyacentes antes de declarar caída
if (hasCrossRoomGround(getCrossRoomChecks())) {
return;
}
vy_ = 0.0F;
transitionToState(State::ON_AIR);
}
@@ -491,52 +465,6 @@ auto Player::handleBorders() const -> Room::Border {
return Room::Border::NONE;
}
void Player::setAdjacentRoom(std::shared_ptr<Room> room, Room::Border direction) {
adjacent_room_ = std::move(room);
adjacent_direction_ = direction;
}
void Player::clearAdjacentRoom() {
adjacent_room_.reset();
adjacent_direction_ = Room::Border::NONE;
}
auto Player::getCollisionContext() const -> CollisionContext {
if (!adjacent_room_) {
return {room_->getTileCollider(), 0.0F, 0.0F};
}
const float CENTER_X = x_ + (WIDTH / 2.0F);
const float CENTER_Y = y_ + (HEIGHT / 2.0F);
switch (adjacent_direction_) {
case Room::Border::TOP:
if (CENTER_Y < PlayArea::TOP) {
return {adjacent_room_->getTileCollider(), 0.0F, static_cast<float>(PlayArea::HEIGHT)};
}
break;
case Room::Border::BOTTOM:
if (CENTER_Y > PlayArea::BOTTOM) {
return {adjacent_room_->getTileCollider(), 0.0F, -static_cast<float>(PlayArea::HEIGHT)};
}
break;
case Room::Border::LEFT:
if (CENTER_X < PlayArea::LEFT) {
return {adjacent_room_->getTileCollider(), static_cast<float>(PlayArea::WIDTH), 0.0F};
}
break;
case Room::Border::RIGHT:
if (CENTER_X > PlayArea::RIGHT) {
return {adjacent_room_->getTileCollider(), -static_cast<float>(PlayArea::WIDTH), 0.0F};
}
break;
default:
break;
}
return {room_->getTileCollider(), 0.0F, 0.0F};
}
void Player::switchBorders() {
switch (border_) {
case Room::Border::TOP:
@@ -569,109 +497,6 @@ void Player::syncSpriteAndCollider() {
collider_box_ = getRect();
}
// Cross-room collision: asigna una room adyacente por índice
void Player::setBorderRoom(int index, std::shared_ptr<Room> room) {
if (index >= 0 && index < BORDER_ROOM_COUNT) {
border_rooms_[index] = std::move(room);
}
}
// Cross-room collision: limpia todas las rooms adyacentes
void Player::clearBorderRooms() {
for (auto& r : border_rooms_) {
r.reset();
}
}
// Cross-room: construye la lista de rooms adyacentes que solapan con la bbox del jugador
auto Player::getCrossRoomChecks() const -> CrossRoomChecks {
// Offsets por room: TOP, RIGHT, BOTTOM, LEFT, TR, BR, BL, TL
static constexpr float PW = static_cast<float>(PlayArea::WIDTH);
static constexpr float PH = static_cast<float>(PlayArea::HEIGHT);
static constexpr struct { float ox; float oy; } OFFSETS[BORDER_ROOM_COUNT] = {
{0, PH}, {-PW, 0}, {0, -PH}, {PW, 0}, {-PW, PH}, {-PW, -PH}, {PW, -PH}, {PW, PH}
};
bool over_top = y_ < 0.0F;
bool over_right = (x_ + WIDTH) > PlayArea::RIGHT;
bool over_bottom = (y_ + HEIGHT) > PlayArea::BOTTOM;
bool over_left = x_ < 0.0F;
bool needed[BORDER_ROOM_COUNT] = {
over_top, over_right, over_bottom, over_left,
over_top && over_right, over_bottom && over_right,
over_bottom && over_left, over_top && over_left
};
CrossRoomChecks result;
for (int i = 0; i < BORDER_ROOM_COUNT; ++i) {
if (needed[i] && border_rooms_[i]) {
result.entries[result.count++] = {&border_rooms_[i]->getTileCollider(), OFFSETS[i].ox, OFFSETS[i].oy};
}
}
return result;
}
// Cross-room: comprueba muros horizontales en rooms adyacentes
void Player::checkCrossRoomWallH(float& new_x, const CrossRoomChecks& checks) const {
for (int i = 0; i < checks.count; ++i) {
const auto& [tc, ox, oy] = checks.entries[i];
if (vx_ < 0.0F) {
float wall = tc->checkWallLeft(new_x + ox, y_ + oy, WIDTH, HEIGHT);
if (wall != Collision::NONE) {
float corrected = wall - ox;
if (corrected > new_x) { new_x = corrected; }
}
} else if (vx_ > 0.0F) {
float wall = tc->checkWallRight(new_x + ox, y_ + oy, WIDTH, HEIGHT);
if (wall != Collision::NONE) {
float corrected = wall - WIDTH - ox;
if (corrected < new_x) { new_x = corrected; }
}
}
}
}
// Cross-room: comprueba suelo/techo en rooms adyacentes (usa old_y para rango correcto de checkFloor)
void Player::checkCrossRoomFloor(float old_y, const CrossRoomChecks& checks) {
for (int i = 0; i < checks.count; ++i) {
const auto& [tc, ox, oy] = checks.entries[i];
if (vy_ < 0.0F) {
float ceiling = tc->checkCeiling(x_ + ox, y_ + oy, WIDTH);
if (ceiling != Collision::NONE) {
float corrected = ceiling - oy;
if (corrected > y_) {
y_ = corrected;
vy_ = 0.0F;
}
}
} else if (vy_ > 0.0F) {
float old_foot = old_y + oy + HEIGHT;
float new_foot = y_ + oy + HEIGHT;
auto hit = tc->checkFloor(x_ + ox, old_foot, WIDTH, new_foot);
if (hit.y != Collision::NONE) {
float corrected = hit.y - HEIGHT - oy;
if (corrected < y_) {
y_ = corrected;
vy_ = 0.0F;
transitionToState(State::ON_GROUND);
}
}
}
}
}
// Cross-room: comprueba si hay suelo bajo el jugador en alguna room adyacente
auto Player::hasCrossRoomGround(const CrossRoomChecks& checks) const -> bool {
for (int i = 0; i < checks.count; ++i) {
const auto& [tc, ox, oy] = checks.entries[i];
float foot = y_ + oy + HEIGHT;
if (tc->hasGroundBelow(x_ + ox, foot, WIDTH)) {
return true;
}
}
return false;
}
// Aplica el desplazamiento de una plataforma móvil al jugador
void Player::applyPlatformDisplacement(float dx, float surface_y) {
y_ = surface_y - HEIGHT; // Snap vertical al top de la plataforma

View File

@@ -70,18 +70,11 @@ class Player {
auto getSpawnParams() -> SpawnData { return {.x = x_, .y = y_, .vx = vx_, .vy = vy_, .last_grounded_position = last_grounded_position_, .state = state_, .flip = sprite_->getFlip()}; }
static auto skinToAnimationPath(const std::string& skin_name) -> std::string;
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); }
void setAdjacentRoom(std::shared_ptr<Room> room, Room::Border direction);
void clearAdjacentRoom();
[[nodiscard]] auto isAlive() const -> bool { return is_alive_; }
[[nodiscard]] auto getVY() const -> float { return vy_; }
void applyPlatformDisplacement(float dx, float surface_y);
void clearPlatformFlag() { on_platform_ = false; }
// Cross-room collision: rooms adyacentes (TOP=0, RIGHT=1, BOTTOM=2, LEFT=3, TR=4, BR=5, BL=6, TL=7)
static constexpr int BORDER_ROOM_COUNT = 8;
void setBorderRoom(int index, std::shared_ptr<Room> room);
void clearBorderRooms();
void setPaused(bool value) { is_paused_ = value; }
void setIgnoreInput(bool value) { ignore_input_ = value; }
[[nodiscard]] auto getIgnoreInput() const -> bool { return ignore_input_; }
@@ -96,18 +89,8 @@ class Player {
static constexpr int WIDTH = 12;
static constexpr int HEIGHT = 24;
// --- Contexto de colisión (selección de room + traducción de coordenadas) ---
struct CollisionContext {
const TileCollider& tc;
float offset_x;
float offset_y;
};
auto getCollisionContext() const -> CollisionContext;
// --- Objetos y punteros ---
std::shared_ptr<Room> room_;
std::shared_ptr<Room> adjacent_room_;
Room::Border adjacent_direction_{Room::Border::NONE};
std::unique_ptr<AnimatedSprite> sprite_;
// --- Posición y física ---
@@ -142,9 +125,6 @@ class Player {
Room::Border border_ = Room::Border::TOP;
int last_grounded_position_ = 0;
// --- Cross-room collision ---
std::shared_ptr<Room> border_rooms_[BORDER_ROOM_COUNT]{};
// --- Renderizado y sonido ---
JA_Sound_t* jump_sound_ = nullptr;
JA_Sound_t* land_sound_ = nullptr;
@@ -156,21 +136,6 @@ class Player {
void handleJumpAndDrop();
void moveHorizontal(float delta_time);
void moveVertical(float delta_time);
// Cross-room collision helpers
struct CrossRoomEntry {
const TileCollider* tc;
float ox;
float oy;
};
struct CrossRoomChecks {
CrossRoomEntry entries[BORDER_ROOM_COUNT]{};
int count{0};
};
auto getCrossRoomChecks() const -> CrossRoomChecks;
void checkCrossRoomWallH(float& new_x, const CrossRoomChecks& checks) const;
void checkCrossRoomFloor(float old_y, const CrossRoomChecks& checks);
auto hasCrossRoomGround(const CrossRoomChecks& checks) const -> bool;
void followSlope();
void exitSlope();
void detectSlopeEntry();