From 56dd566af88690c214d257b2bf6c76af3e0a71e9 Mon Sep 17 00:00:00 2001 From: RobertoMaurizzi Date: Tue, 8 Jul 2025 17:30:32 +0800 Subject: [PATCH] change y again, add StateScoped to level and player, optimize level sel. --- src/demo/level.rs | 14 ++++++++++++-- src/demo/player.rs | 19 +++++++++++++------ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/demo/level.rs b/src/demo/level.rs index 0ca5670..0a37418 100644 --- a/src/demo/level.rs +++ b/src/demo/level.rs @@ -88,12 +88,15 @@ fn pan_camera( } fn translate_grid_coords_entities( - mut grid_coords_entities: Query<(&mut Transform, &GridCoords), Changed>, + mut grid_coords_entities: Query< + (&mut Transform, &GridCoords), + (With, Changed), + >, pan: Res, ) { // 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()); } } diff --git a/src/demo/player.rs b/src/demo/player.rs index d306340..92bff45 100644 --- a/src/demo/player.rs +++ b/src/demo/player.rs @@ -10,9 +10,10 @@ use crate::{ AppSystems, PausableSystems, asset_tracking::LoadResource, demo::{ - animation::PlayerAnimation, - movement::{MovementController, ScreenWrap}, + // animation::PlayerAnimation, + movement::MovementController, }, + screens::Screen, }; pub(super) fn plugin(app: &mut App) { @@ -89,12 +90,15 @@ fn record_player_directional_input( ) { // Collect directional input. let mut intent = Vec2::ZERO; - // TODO: check axes for gridcoords! + // TODO: example ldkt has down -1 and up +1... but here the opposite happens?! + // additionally: why the heck the Player is offsetted by 25 y? + // why the player is displayed at its set location but then is teleported the first time I + // update its GridCoords? if input.just_pressed(KeyCode::KeyW) || input.pressed(KeyCode::ArrowUp) { - intent.y -= 1.0; + intent.y += 1.0; } if input.just_pressed(KeyCode::KeyS) || input.pressed(KeyCode::ArrowDown) { - intent.y += 1.0; + intent.y -= 1.0; } if input.just_pressed(KeyCode::KeyA) || input.pressed(KeyCode::ArrowLeft) { intent.x -= 1.0; @@ -144,7 +148,7 @@ impl FromWorld for PlayerAssets { } fn process_player( - mut _commands: Commands, + mut commands: Commands, new_players: Query<(Entity, &GridCoords), Added>, ) { for (player_entity, player_coords) in new_players.iter() { @@ -152,5 +156,8 @@ fn process_player( "Spawned new player: {:?} at {:?}", player_entity, player_coords ); + commands + .entity(player_entity) + .insert(StateScoped(Screen::Gameplay)); } }