|
@@ -76,28 +76,57 @@ fn create_board(
|
|
|
|
|
|
|
|
|
fn select_square(
|
|
|
+ mut commands: Commands,
|
|
|
mouse_button_inputs: Res<Input<MouseButton>>,
|
|
|
mut selected_square: ResMut<SelectedSquare>,
|
|
|
mut selected_piece: ResMut<SelectedPiece>,
|
|
|
camera_query: Query<&PickingCamera>,
|
|
|
squares_query: Query<&Square>,
|
|
|
- mut pieces_query: Query<(Entity, &mut Piece)>,
|
|
|
+ mut pieces_query: Query<(Entity, &mut Piece, &Children)>,
|
|
|
) {
|
|
|
if !mouse_button_inputs.just_pressed(MouseButton::Left) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- let pieces_vec = pieces_query.iter_mut().map(|(_, piece)| *piece).collect();
|
|
|
-
|
|
|
for camera in camera_query.iter() {
|
|
|
if let Some((square_entity, _intersection)) = camera.intersect_top() {
|
|
|
+ let pieces_entity_vec: Vec<(Entity, Piece, Vec<Entity>)> = pieces_query
|
|
|
+ .iter_mut()
|
|
|
+ .map(|(entity, piece, children)| {
|
|
|
+ (
|
|
|
+ entity,
|
|
|
+ *piece,
|
|
|
+ children.iter().map(|entity| *entity).collect()
|
|
|
+ )
|
|
|
+ })
|
|
|
+ .collect();
|
|
|
+ let pieces_vec = pieces_query
|
|
|
+ .iter_mut()
|
|
|
+ .map(|(_, piece, _)| *piece)
|
|
|
+ .collect();
|
|
|
+
|
|
|
if let Ok(square) = squares_query.get(square_entity) {
|
|
|
selected_square.entity = Some(square_entity);
|
|
|
|
|
|
// if a piece is already selected, set it to move
|
|
|
if let Some(selected_piece_entity) = selected_piece.entity {
|
|
|
- if let Ok((_piece_entity, mut piece)) = pieces_query.get_mut(selected_piece_entity) {
|
|
|
+ if let Ok((_piece_entity, mut piece, _)) = pieces_query.get_mut(selected_piece_entity) {
|
|
|
if piece.is_move_valid((square.x, square.y), &pieces_vec) {
|
|
|
+ // check to see if we're capturing
|
|
|
+ for (other_entity, other_piece, other_children) in pieces_entity_vec {
|
|
|
+ if other_piece.x == square.x
|
|
|
+ && other_piece.y == square.y
|
|
|
+ && other_piece.color != piece.color
|
|
|
+ {
|
|
|
+ // despawn the piece and its children
|
|
|
+ commands.entity(other_entity).despawn();
|
|
|
+ for child in other_children {
|
|
|
+ commands.entity(child).despawn();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // and set it to move
|
|
|
piece.x = square.x;
|
|
|
piece.y = square.y;
|
|
|
}
|
|
@@ -106,7 +135,7 @@ fn select_square(
|
|
|
selected_piece.entity = None;
|
|
|
} else {
|
|
|
// otherwise, select the piece (if one) in the selected square
|
|
|
- for (piece_entity, piece) in pieces_query.iter_mut() {
|
|
|
+ for (piece_entity, piece, _) in pieces_query.iter_mut() {
|
|
|
if piece.x == square.x && piece.y == square.y {
|
|
|
selected_piece.entity = Some(piece_entity);
|
|
|
break;
|