Just left click (now for de-selecting pieces)

main
Elijah C. Voigt 1 year ago
parent 9c396e3183
commit c319ecab61

@ -62,8 +62,10 @@ impl Plugin for Display3dPlugin {
any_component_changed::<Piece>()
.or_else(any_component_changed::<Side>())
.or_else(any_component_changed::<BoardIndex>())
.or_else(any_component_added::<Animating>())
.or_else(any_component_removed::<Animating>())
.or_else(any_component_removed::<Captured>()),
.or_else(any_component_removed::<Captured>())
.or_else(any_component_removed::<Promoted>()),
),
set_board_model.run_if(any_component_added::<game::BoardComponent>()),
set_title_model.run_if(any_component_added::<TitleText>()),
@ -280,16 +282,25 @@ fn initialize(mut commands: Commands, board: Res<game::Board>, assets: Res<Asset
});
// Pieces
let mut angle = 0.0;
board.pieces().iter().for_each(|(index, piece)| {
let side = Board::side(*index).expect("Spawn valid side");
// Rotates each piece 90 degrees offset from the previous piece
let rotation = Quat::from_rotation_y(angle);
let transform = Transform::default().with_rotation(rotation);
angle += std::f32::consts::PI / 2.0;
parent.spawn((
side,
DisplayState::Display3d,
Display3d,
*piece,
*index,
SceneBundle { ..default() },
SceneBundle {
transform,
..default()
},
game::Selectable,
Dissolvable {
start: 1.0,
@ -675,11 +686,14 @@ fn select(
// Query Selectable with BoardIndex
query: Query<(&BoardIndex, &Side), With<Selectable>>,
piece: Res<PiecePointer>,
mut selections: EventWriter<game::Selection>,
selected: Query<Entity, With<game::Selected>>,
selected: Query<&BoardIndex, With<game::Selected>>,
state: Res<State<game::TurnState>>,
mut selections: EventWriter<game::Selection>,
mut moves: EventWriter<Move>
) {
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::<f32>("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);
})
}

@ -21,7 +21,6 @@ impl Plugin for GamePlugin {
.run_if(on_event::<Move>())
.after(handle_selection),
set_side.run_if(any_component_changed::<BoardIndex>()),
cancel_place.run_if(just_pressed(MouseButton::Right)),
handle_selection.run_if(on_event::<Selection>()),
show_valid_moves.run_if(any_component_added::<Selected>()),
hide_valid_moves.run_if(any_component_removed::<Selected>()),
@ -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::<BoardIndex>()
.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<Selected>>, mut events: EventWriter<Move>) {
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<T: Component>(
selected_pieces: Query<Entity, (With<Piece>, With<T>, With<Selected>)>,
@ -1431,6 +1411,7 @@ fn reset_game(
mut board: ResMut<Board>,
mut score: ResMut<Score>,
mut commands: Commands,
mut turn_state: ResMut<NextState<TurnState>>,
) {
// 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::<Captured>();
commands.entity(e)
.insert((*i, *p, Visibility::Inherited))
.remove::<(Captured, Promoted)>();
});
}

Loading…
Cancel
Save