From 63dac42250179879e91082a96aa0f00b1305fe10 Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Wed, 24 Jan 2024 21:32:46 -0800 Subject: [PATCH] Refactor: valid move indicators no longer despawn --- src/display3d.rs | 58 +++++++++++++----------------------------------- src/game.rs | 35 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/display3d.rs b/src/display3d.rs index 60db931..78a1a11 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -56,8 +56,6 @@ impl Plugin for Display3dPlugin { switch_sides .run_if(in_state(GameState::Play)) .run_if(state_changed::()), - create_valid_move_entity.run_if(any_component_added::), - remove_valid_move_entity.run_if(any_component_removed::()), set_valid_move_model.run_if(any_component_added::), update_tweaks.run_if(on_event::>()), scale_lighting.run_if( @@ -176,6 +174,20 @@ fn initialize(mut commands: Commands, board: Res, assets: Res, Added, With)>, - board: Res, - root: Query>, - mut commands: Commands, -) { - events.iter().for_each(|idx| { - if let Ok(board_entity) = root.get_single() { - board - .valid_moves(*idx) - .iter() - .map(|i| Transform::from_translation(board_translation(i))) - .for_each(|t| { - commands.entity(board_entity).with_children(|parent| { - parent - .spawn((Display3d, game::ValidMove, SceneBundle { ..default() })) - .insert(t); - }); - }); - } - }); -} - fn set_valid_move_model( mut events: Query<&mut Handle, (With, Added)>, gltfs: Res>, @@ -866,20 +852,6 @@ fn _play_valid_move_animation(_players: Query<&AnimationPlayer>) { todo!(); } -// TODO: Do not create/delete entities at runtime -/// Remove "Valid Move" indicators when a piece is de-selected -fn remove_valid_move_entity( - mut events: RemovedComponents, - valid_moves: Query>, - mut commands: Commands, -) { - events.read().for_each(|_| { - valid_moves.iter().for_each(|entity| { - commands.entity(entity).despawn_recursive(); - }); - }); -} - fn pick_up( mut events: Query< (Entity, &game::Piece), @@ -1378,7 +1350,7 @@ fn capture_piece( commands.entity(entity).insert(orig.clone()); commands.entity(entity).remove::>>(); } else { - warn!("Entity {} does not have original material") + warn!("Entity {:?} does not have original material", entity) } } }); diff --git a/src/game.rs b/src/game.rs index 88c3580..ad624e2 100644 --- a/src/game.rs +++ b/src/game.rs @@ -26,6 +26,8 @@ impl Plugin for GamePlugin { switch_sides.run_if(|input: Res>| -> bool { input.just_pressed(KeyCode::N) }), + show_valid_moves.run_if(any_component_added::), + hide_valid_moves.run_if(any_component_removed::()), ), ) .add_systems( @@ -610,3 +612,36 @@ fn switch_sides(state: Res>, mut next: ResMut next.set(TurnState::SideA), } } + +/// Spawn "Valid move" indicators when a piece is selected +/// Another system registers these new entities and associates the correct models and plays animations. +fn show_valid_moves( + events: Query<&BoardIndex, (With, Added)>, + board: Res, + mut valid_moves: Query<(&BoardIndex, &mut Visibility), With>, +) { + // When a piece is selected + events.iter().for_each(|idx| { + // Iterate over all ValidMove entities + // For each one with a ValidMove index, make it visible + board.valid_moves(*idx).iter().for_each(|idx| { + valid_moves.iter_mut().for_each(|(i, mut vis)| { + if i == idx { + *vis = Visibility::Inherited; + } + }); + }) + }); +} + +/// Hide "Valid Move" indicators when a piece is de-selected +fn hide_valid_moves( + mut events: RemovedComponents, + mut valid_moves: Query<&mut Visibility, With>, +) { + events.read().for_each(|_| { + valid_moves.iter_mut().for_each(|mut visibility| { + *visibility = Visibility::Hidden; + }); + }); +}