change y again, add StateScoped to level and player, optimize level sel.

This commit is contained in:
Roberto Maurizzi 2025-07-08 17:30:32 +08:00
parent dffaadf7ec
commit 56dd566af8
Signed by: robm
GPG key ID: F26E59AFAAADEA55
2 changed files with 25 additions and 8 deletions

View file

@ -88,12 +88,15 @@ fn pan_camera(
}
fn translate_grid_coords_entities(
mut grid_coords_entities: Query<(&mut Transform, &GridCoords), Changed<GridCoords>>,
mut grid_coords_entities: Query<
(&mut Transform, &GridCoords),
(With<Player>, Changed<GridCoords>),
>,
pan: Res<PanLevel>,
) {
// TODO: what is this used for? Why it doesn't work for a moving Player?
for (mut transform, grid_coords) in grid_coords_entities.iter_mut() {
// info!("translated pan offset: {}", pan.offset);
info!("Changed GridCoords: {grid_coords:?}");
transform.translation = (bevy_ecs_ldtk::utils::grid_coords_to_translation(
*grid_coords,
IVec2::splat(GRID_SIZE),
@ -117,6 +120,7 @@ pub enum Direction {
E,
S,
W,
// we ignore the diagonals NE SE NW SW
}
impl TryFrom<&str> for Direction {
@ -218,6 +222,7 @@ pub fn spawn_level(
commands.spawn((
Name::new("Ldtk level"),
StateScoped(Screen::Gameplay),
LdtkWorldBundle {
ldtk_handle: level_assets.world.clone(),
transform: Transform::from_xyz(-half_size.x, half_size.y, 0.0),
@ -374,6 +379,11 @@ fn level_selection_follow_player(
};
if level_bounds.contains(player_transform.translation().truncate()) {
if *level_selection == LevelSelection::Iid(level_iid.clone()) {
// Player is already in the current level, no need to change
return Ok(());
}
info!("Setting current level to {level_iid}");
*level_selection = LevelSelection::Iid(level_iid.clone());
}
}