From c319ecab6188843da07313e4fb88b96adb98f6fe Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Sun, 5 May 2024 21:05:42 -0700 Subject: [PATCH] Just left click (now for de-selecting pieces) --- src/display3d.rs | 34 ++++++++++++++++++++++++++++------ src/game.rs | 40 +++++++++++++--------------------------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/display3d.rs b/src/display3d.rs index 9c56a7a..36a5bdd 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -62,8 +62,10 @@ impl Plugin for Display3dPlugin { any_component_changed::() .or_else(any_component_changed::()) .or_else(any_component_changed::()) + .or_else(any_component_added::()) .or_else(any_component_removed::()) - .or_else(any_component_removed::()), + .or_else(any_component_removed::()) + .or_else(any_component_removed::()), ), set_board_model.run_if(any_component_added::()), set_title_model.run_if(any_component_added::()), @@ -280,16 +282,25 @@ fn initialize(mut commands: Commands, board: Res, assets: Res>, piece: Res, - mut selections: EventWriter, - selected: Query>, + selected: Query<&BoardIndex, With>, state: Res>, + mut selections: EventWriter, + mut moves: EventWriter ) { match *piece { + // Something is selected, so send an event saying such + // Why send the event instead of just detect the resource update? PiecePointer(Some(e)) => { query.get(e).iter().for_each(|(board_index, side)| { let side_check = !selected.is_empty() || state.get().0 == **side; @@ -689,7 +703,16 @@ fn select( } }); } - _ => (), + // Nothing selected, cancel the selection + PiecePointer(None) => { + selected.iter().for_each(|board_index| { + moves.send(Move { + from: *board_index, + to: Some(*board_index), + ..default() + }); + }) + }, } } @@ -981,7 +1004,6 @@ fn skip_animation( let speed = tweak.get::("animation_fast_speed").unwrap(); players.iter_mut().for_each(|mut p| { debug!("Skipping animation"); - // HACK: Set the speed of the animation to turbo p.set_speed(speed); }) } diff --git a/src/game.rs b/src/game.rs index f6e5fa3..7288690 100644 --- a/src/game.rs +++ b/src/game.rs @@ -21,7 +21,6 @@ impl Plugin for GamePlugin { .run_if(on_event::()) .after(handle_selection), set_side.run_if(any_component_changed::()), - cancel_place.run_if(just_pressed(MouseButton::Right)), handle_selection.run_if(on_event::()), show_valid_moves.run_if(any_component_added::()), hide_valid_moves.run_if(any_component_removed::()), @@ -372,14 +371,10 @@ impl Board { fn new() -> Board { Board::from_ascii( - // r#".....dqq - // dpp..pdq - // qdp..ppd - // qqd....."#, - r#"........ - ...p.... - ..p...p. - ........"#, + r#".....dqq + dpp..pdq + qdp..ppd + qqd....."#, ) } @@ -1040,9 +1035,6 @@ pub(crate) fn update_board( if *from != *to_idx { match move_type { MoveType::Promotion(piece) => { - error!("Hey Eli, this is where you are merging pieces"); - // Here we insert a new Piece type based on the merge - // Other system should automatically update the model commands .entity(entity) .insert(*piece); @@ -1055,7 +1047,7 @@ pub(crate) fn update_board( next_state.set(ns); } } - // We are moving off the board (e.g,. capture or merge) + // We are moving off the board (e.g,. capture or promotion/merge) None => { match move_type { MoveType::Capture => { @@ -1069,7 +1061,7 @@ pub(crate) fn update_board( commands .entity(entity) .remove::() - .insert(Promoted); + .insert((Promoted, Visibility::Hidden)); }, _ => { panic!("How did you do this!?"); @@ -1353,18 +1345,6 @@ fn handle_selection( *latest = None; } -/// Triggered when right-mouse-button clicked -fn cancel_place(current: Query<&BoardIndex, With>, mut events: EventWriter) { - current.iter().for_each(|board_index| { - info!("De-selecting piece at {:?}", board_index); - events.send(Move { - from: *board_index, - to: Some(*board_index), - ..default() - }); - }); -} - /// Panics if more than two pieces are selected at a time fn asserts( selected_pieces: Query, With, With)>, @@ -1431,6 +1411,7 @@ fn reset_game( mut board: ResMut, mut score: ResMut, mut commands: Commands, + mut turn_state: ResMut>, ) { // Setup the board *board = Board::new(); @@ -1438,12 +1419,17 @@ fn reset_game( // Reset the score *score = Score { ..default() }; + // Reset to starting side + turn_state.set(TurnState(Side::B)); + // Move all pieces to their correct spots pieces .iter() .zip(board.pieces().iter()) .for_each(|(e, (i, p))| { - commands.entity(e).insert((*i, *p)).remove::(); + commands.entity(e) + .insert((*i, *p, Visibility::Inherited)) + .remove::<(Captured, Promoted)>(); }); }