Moving works in 2d!

Now just need to generalize 3d selection to select pieces + board tiles for movement to work there!

Exicted!
bevy0.12
Elijah C. Voigt 2 years ago
parent 729c74e932
commit b4270899d2

@ -18,6 +18,7 @@ impl Plugin for GamePlugin {
}), }),
select_sync.run_if(any_component_added::<Selected>), select_sync.run_if(any_component_added::<Selected>),
deselect_sync.run_if(any_component_removed::<Selected>()), deselect_sync.run_if(any_component_removed::<Selected>()),
move_piece.run_if(any_component_added::<Selected>),
), ),
) )
.add_systems( .add_systems(
@ -333,12 +334,37 @@ fn deselect_sync(
}) })
} }
fn cancel_place( fn cancel_place(current: Query<Entity, (With<Selected>, With<Piece>)>, mut commands: Commands) {
current: Query<Entity, (With<game::Selected>, With<game::Piece>)>,
mut commands: Commands,
) {
current.iter().for_each(|entity| { current.iter().for_each(|entity| {
info!("De-selecting {:?}", entity); info!("De-selecting {:?}", entity);
commands.entity(entity).remove::<game::Selected>(); commands.entity(entity).remove::<Selected>();
});
}
/// When a tile is selected, move all selected pieces to that index
fn move_piece(
events: Query<(Entity, &BoardIndex), (With<Tile>, Added<Selected>)>,
selected_pieces: Query<(Entity, &BoardIndex), (With<Selected>, With<Piece>)>,
mut board: ResMut<Board>,
mut commands: Commands,
mut move_events: EventWriter<Move>,
) {
events.iter().for_each(|(tile, to)| {
selected_pieces.iter().for_each(|(piece, from)| {
// Move piece
match board.move_piece(from, to) {
Ok(moves) => {
// De-select the piece
commands.entity(piece).remove::<Selected>();
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::<Selected>();
}); });
} }

Loading…
Cancel
Save