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( 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>, pan: Res<PanLevel>,
) { ) {
// TODO: what is this used for? Why it doesn't work for a moving Player? // 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() { 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( transform.translation = (bevy_ecs_ldtk::utils::grid_coords_to_translation(
*grid_coords, *grid_coords,
IVec2::splat(GRID_SIZE), IVec2::splat(GRID_SIZE),
@ -117,6 +120,7 @@ pub enum Direction {
E, E,
S, S,
W, W,
// we ignore the diagonals NE SE NW SW
} }
impl TryFrom<&str> for Direction { impl TryFrom<&str> for Direction {
@ -218,6 +222,7 @@ pub fn spawn_level(
commands.spawn(( commands.spawn((
Name::new("Ldtk level"), Name::new("Ldtk level"),
StateScoped(Screen::Gameplay),
LdtkWorldBundle { LdtkWorldBundle {
ldtk_handle: level_assets.world.clone(), ldtk_handle: level_assets.world.clone(),
transform: Transform::from_xyz(-half_size.x, half_size.y, 0.0), 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_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()); *level_selection = LevelSelection::Iid(level_iid.clone());
} }
} }

View file

@ -10,9 +10,10 @@ use crate::{
AppSystems, PausableSystems, AppSystems, PausableSystems,
asset_tracking::LoadResource, asset_tracking::LoadResource,
demo::{ demo::{
animation::PlayerAnimation, // animation::PlayerAnimation,
movement::{MovementController, ScreenWrap}, movement::MovementController,
}, },
screens::Screen,
}; };
pub(super) fn plugin(app: &mut App) { pub(super) fn plugin(app: &mut App) {
@ -89,12 +90,15 @@ fn record_player_directional_input(
) { ) {
// Collect directional input. // Collect directional input.
let mut intent = Vec2::ZERO; 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) { 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) { 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) { if input.just_pressed(KeyCode::KeyA) || input.pressed(KeyCode::ArrowLeft) {
intent.x -= 1.0; intent.x -= 1.0;
@ -144,7 +148,7 @@ impl FromWorld for PlayerAssets {
} }
fn process_player( fn process_player(
mut _commands: Commands, mut commands: Commands,
new_players: Query<(Entity, &GridCoords), Added<Player>>, new_players: Query<(Entity, &GridCoords), Added<Player>>,
) { ) {
for (player_entity, player_coords) in new_players.iter() { for (player_entity, player_coords) in new_players.iter() {
@ -152,5 +156,8 @@ fn process_player(
"Spawned new player: {:?} at {:?}", "Spawned new player: {:?} at {:?}",
player_entity, player_coords player_entity, player_coords
); );
commands
.entity(player_entity)
.insert(StateScoped(Screen::Gameplay));
} }
} }