From 73af4d370694b57f2fb57e1a18921c12b17c57cb Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sat, 28 Dec 2024 15:52:57 -0800 Subject: [PATCH] Debug shows world coordinates which is nice. --- src/debug.rs | 46 +++++++++++++++++++++++++++++++++++----------- src/play.rs | 29 +++++++++++++++++++++++++---- src/setup.rs | 4 ++-- 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/src/debug.rs b/src/debug.rs index 6f96a8d..37be0d7 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -8,27 +8,40 @@ pub struct DebugPlugin; impl Plugin for DebugPlugin { fn build(&self, app: &mut App) { app.add_observer(track_card_info) - .add_systems(Startup, init_ui); + .add_systems(Startup, init_ui) + .add_systems(Update, set_debug_location); } } #[derive(Component)] pub(crate) struct DebugText; +#[derive(Component)] +pub(crate) struct DebugCardText; + +#[derive(Component)] +pub(crate) struct DebugLocationText; + fn init_ui(mut commands: Commands) { commands .spawn(( - Sprite::from_color(Color::BLACK.with_alpha(0.9), [250.0, 150.0].into()), + Sprite::from_color(Color::BLACK.with_alpha(0.9), [250.0, 200.0].into()), DebugText, - Visibility::Hidden, + Visibility::Inherited, Transform::default().with_translation(Vec3::new(0.0, 0.0, 1.0)), PickingBehavior::IGNORE, )) .with_children(|parent| { parent.spawn(( Text2d("...".to_string()), - DebugText, - Transform::default().with_translation(Vec3::new(0.0, 0.0, 2.0)), + DebugLocationText, + Transform::default().with_translation(Vec3::new(-50.0, 80.0, 2.0)), + PickingBehavior::IGNORE, + )); + parent.spawn(( + Text2d("...".to_string()), + DebugCardText, + Transform::default().with_translation(Vec3::new(10.0, -25.0, 2.0)), PickingBehavior::IGNORE, )); }); @@ -51,13 +64,11 @@ fn track_card_info( pub(crate) fn set_debug_card( trigger: Trigger>, cards: Query<&Card>, - mut vis: Query<&mut Visibility, With>, - mut debug_text: Query<&mut Text2d, With>, + mut vis: Query<&mut Visibility, With>, + mut debug_text: Single<&mut Text2d, With>, ) { let card = cards.get(trigger.entity()).unwrap(); - debug_text.iter_mut().for_each(|mut text| { - text.0 = format!("{:#?}", card); - }); + debug_text.0 = format!("{:#?}", card); vis.iter_mut().for_each(|mut v| { *v = Visibility::Inherited; }); @@ -65,9 +76,22 @@ pub(crate) fn set_debug_card( pub(crate) fn hide_debug_card( _trigger: Trigger>, - mut vis: Query<&mut Visibility, With>, + mut vis: Query<&mut Visibility, With>, ) { vis.iter_mut().for_each(|mut v| { *v = Visibility::Hidden; }); } + +fn set_debug_location( + window: Single<&Window>, + camera: Single<(&Camera, &GlobalTransform)>, + mut text: Single<&mut Text2d, With>, +) { + let (cam, gt) = *camera; + if let Some(pos) = window.cursor_position() { + if let Ok(world_pos) = cam.viewport_to_world_2d(gt, pos) { + text.0 = format!("{:.1},{:.1}", world_pos.x, world_pos.y); + } + } +} diff --git a/src/play.rs b/src/play.rs index 60b7c2f..b914d91 100644 --- a/src/play.rs +++ b/src/play.rs @@ -226,12 +226,33 @@ fn set_added(query: Query>) -> bool { /// When a card is added to the board, place it on the screen pub(crate) fn place_card( trigger: Trigger, - mut query: Query<(&PlayLocation, &mut Visibility, &mut Transform)>, + mut query: Query<( + Entity, + &PlayLocation, + &mut Visibility, + &Name, + &mut AnimationPlayer, + )>, + animation_store: Res, + mut commands: Commands, ) { - let (pl, mut visibility, mut transform) = query.get_mut(trigger.entity()).unwrap(); + let (entity, play_location, mut visibility, name, mut player) = + query.get_mut(trigger.entity()).unwrap(); + + let (graph_handle, animation_index) = animation_store + .store + .get(&format!("deck->{:?}", (play_location.x, play_location.y))) + .unwrap(); + + player.play(animation_index.clone()); - // Set it's transform based on it's placement - transform.translation = card_placement(pl); + commands.entity(entity).insert(( + graph_handle.clone(), + AnimationTarget { + id: AnimationTargetId::from_name(name), + player: entity, + }, + )); // Set it to visible *visibility = Visibility::Inherited; diff --git a/src/setup.rs b/src/setup.rs index 14f49c2..44fda02 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -123,9 +123,9 @@ fn setup_animations( // For each spot on board RangeInclusive::::new(0, 3).for_each(|x| { RangeInclusive::::new(0, 3).for_each(|y| { - let a = Vec3::new(-100.0, 0.0, 0.0); + let a = Vec3::new(-400.0, -200.0, 0.0); let b = play::card_placement(&play::PlayLocation { x, y }); - let c = Vec3::new(100.0, 0.0, 0.0); + let c = Vec3::new(-400.0, 200.0, 0.0); // Serve Deck -> Spot Animation {