From 38b796ad31ba5ecaeb07d697044d3918d50c9710 Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Wed, 8 Nov 2023 15:36:16 -0800 Subject: [PATCH] bug infestation. selecting/capturing/etc in 2d works worse than in 3d --- src/display2d.rs | 2 +- src/game.rs | 82 +++++++++++++++++++++++++----------------------- src/main.rs | 4 +-- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/display2d.rs b/src/display2d.rs index 7055571..f690311 100644 --- a/src/display2d.rs +++ b/src/display2d.rs @@ -33,7 +33,7 @@ impl Plugin for Display2dPlugin { }), update_background.run_if(on_event::()), set_transform - .after(game::update_board::) + .after(game::update_board) .run_if(any_component_changed::), set_piece_sprite.run_if(any_component_changed::), set_tile_sprite.run_if(any_component_added::), diff --git a/src/game.rs b/src/game.rs index dcedf1d..6912e12 100644 --- a/src/game.rs +++ b/src/game.rs @@ -11,8 +11,7 @@ impl Plugin for GamePlugin { Update, ( menu::exit_to_menu.run_if(in_state(GameState::Play)), - update_board::.run_if(on_event::()), - update_board::.run_if(on_event::()), + update_board.run_if(on_event::()), set_side.run_if(on_event::()), // TODO: correct run_if? cancel_place.run_if(|buttons: Res>| -> bool { buttons.just_pressed(MouseButton::Right) @@ -20,10 +19,7 @@ impl Plugin for GamePlugin { select_sync.run_if(any_component_added::), deselect_sync.run_if(any_component_removed::()), move_piece.run_if(any_component_added::), - exclusive_select:: - .run_if(any_component_added::), - exclusive_select:: - .run_if(any_component_added::), + capture_piece.run_if(any_component_added::), ), ) .add_systems( @@ -62,6 +58,9 @@ pub(crate) fn tiles() -> impl Iterator { #[derive(Debug, Component)] pub(crate) struct BoardComponent; +#[derive(Debug, Component)] +pub(crate) struct Captured; + // manually for the type. impl std::fmt::Display for Piece { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { @@ -76,7 +75,6 @@ impl std::fmt::Display for Piece { #[derive(Debug)] pub(crate) enum GameError { NullMove, - InvalidMove, InvalidIndex, } @@ -150,12 +148,14 @@ impl Board { ) -> Result, GameError> { if from == to { Err(GameError::NullMove) - } else if self.at(to).is_none() { + } else { self.at(from).map_or(Err(GameError::NullMove), |from_val| { // The current epoch is the last epoch + 1 let epoch = self.moves.last().unwrap_or(&Move { ..default() }).epoch + 1; + // Local moves vec we can return let mut moves = vec![]; + // If the position we are moving to is occupied, capture the removal in the ledger if self.inner[to.y][to.x].is_some() { moves.push(Move { @@ -164,21 +164,21 @@ impl Board { to: None, }); } + // Capture the intened move in the moves ledger moves.push(Move { epoch, from: from.clone(), to: Some(to.clone()), }); + self.moves.extend(moves.clone()); - // TODO: We can self.inner.swap(to, from) if board is single vec - // Update board to reflect move + self.inner[to.y][to.x] = Some(from_val); self.inner[from.y][from.x] = None; + Ok(moves) }) - } else { - Err(GameError::InvalidMove) } } @@ -272,9 +272,10 @@ fn debug_board(board: Res, mut debug_info: ResMut) { /// Update this method to use a diff between the board and the state of the 2d/3d worlds /// Only update the tiles without a corresponding board piece -pub(crate) fn update_board( +pub(crate) fn update_board( mut events: EventReader, - mut pieces: Query<(Entity, &mut BoardIndex), (With, With)>, + mut pieces: Query<(Entity, &mut BoardIndex), (With, Without)>, + tiles: Query<(Entity, &BoardIndex), (With, Without)>, mut commands: Commands, ) { events.iter().for_each(|Move { from, to, .. }| { @@ -282,12 +283,25 @@ pub(crate) fn update_board( .iter_mut() .filter(|(_, index)| **index == *from) .for_each(|(entity, mut index)| { - if let Some(idx) = to { - *index = idx.clone(); - } else { - commands.entity(entity).remove::(); + match to { + Some(to_idx) => { + *index = to_idx.clone(); + } + None => { + commands + .entity(entity) + .remove::() + .insert(Captured); + } } - }) + commands.entity(entity).remove::(); + }); + tiles + .iter() + .filter_map(|(entity, idx)| (Some(idx) == to.as_ref() || idx == from).then_some(entity)) + .for_each(|entity| { + commands.entity(entity).remove::(); + }); }) } @@ -349,44 +363,32 @@ fn cancel_place(current: Query, With)>, mut comma /// When a tile is selected, move all selected pieces to that index fn move_piece( - events: Query<(Entity, &BoardIndex), (With, Added)>, - selected_pieces: Query<(Entity, &BoardIndex), (With, With)>, + events: Query<(Entity, &BoardIndex), (With, Added)>, + selected_pieces: Query<&BoardIndex, (With, With)>, mut board: ResMut, mut commands: Commands, mut move_events: EventWriter, ) { events.iter().for_each(|(tile, to)| { - selected_pieces.iter().for_each(|(piece, from)| { + selected_pieces.iter().for_each(|from| { // Move piece match board.move_piece(from, to) { Ok(moves) => { // De-select the piece - commands.entity(piece).remove::(); + info!("Applying moves {:?}", moves); moves.iter().for_each(|m| move_events.send(m.clone())); } Err(GameError::NullMove) => warn!("Null move!"), - Err(GameError::InvalidMove) => warn!("Invalid move!"), Err(GameError::InvalidIndex) => warn!("Invalid index!"), } }); - - // De-select the tile - commands.entity(tile).remove::(); }); } -/// TEMPORARY: Exclusive selection; only select one piece at a time -fn exclusive_select( - events: Query, With, With)>, - selected: Query, With, With)>, - mut commands: Commands, -) { - // Iterate over selected component markers on pieces - events.iter().for_each(|entity| { - // Selected > 2 (one for each display2d/3d) - selected.iter().filter(|&e| e != entity).for_each(|e| { - // Remove the previously added selected marker - commands.entity(e).remove::(); - }); +/// When a piece's _BoardIndex_ is removed, we hide that entity from the viewer +fn capture_piece(mut events: Query<&mut Visibility, Added>) { + events.iter_mut().for_each(|mut vis| { + info!("Hiding captured piece"); + *vis = Visibility::Hidden }); } diff --git a/src/main.rs b/src/main.rs index c150148..b8be03f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,7 +143,7 @@ fn toggle_display_mode( fn activate( mut cameras: Query<&mut Camera, With>, - mut entities: Query<&mut Visibility, With>, + mut entities: Query<&mut Visibility, (With, Without)>, ) { cameras.iter_mut().for_each(|mut camera| { camera.is_active = true; @@ -155,7 +155,7 @@ fn activate( fn deactivate( mut cameras: Query<&mut Camera, With>, - mut entities: Query<&mut Visibility, With>, + mut entities: Query<&mut Visibility, (With, Without)>, ) { cameras.iter_mut().for_each(|mut camera| { camera.is_active = false;