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>() any_component_changed::<Piece>()
.or_else(any_component_changed::<Side>()) .or_else(any_component_changed::<Side>())
.or_else(any_component_changed::<BoardIndex>()) .or_else(any_component_changed::<BoardIndex>())
.or_else(any_component_added::<Animating>())
.or_else(any_component_removed::<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_board_model.run_if(any_component_added::<game::BoardComponent>()),
set_title_model.run_if(any_component_added::<TitleText>()), 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 // Pieces
let mut angle = 0.0;
board.pieces().iter().for_each(|(index, piece)| { board.pieces().iter().for_each(|(index, piece)| {
let side = Board::side(*index).expect("Spawn valid side"); 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(( parent.spawn((
side, side,
DisplayState::Display3d, DisplayState::Display3d,
Display3d, Display3d,
*piece, *piece,
*index, *index,
SceneBundle { ..default() }, SceneBundle {
transform,
..default()
},
game::Selectable, game::Selectable,
Dissolvable { Dissolvable {
start: 1.0, start: 1.0,
@ -675,11 +686,14 @@ fn select(
// Query Selectable with BoardIndex // Query Selectable with BoardIndex
query: Query<(&BoardIndex, &Side), With<Selectable>>, query: Query<(&BoardIndex, &Side), With<Selectable>>,
piece: Res<PiecePointer>, piece: Res<PiecePointer>,
mut selections: EventWriter<game::Selection>, selected: Query<&BoardIndex, With<game::Selected>>,
selected: Query<Entity, With<game::Selected>>,
state: Res<State<game::TurnState>>, state: Res<State<game::TurnState>>,
mut selections: EventWriter<game::Selection>,
mut moves: EventWriter<Move>
) { ) {
match *piece { 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)) => { PiecePointer(Some(e)) => {
query.get(e).iter().for_each(|(board_index, side)| { query.get(e).iter().for_each(|(board_index, side)| {
let side_check = !selected.is_empty() || state.get().0 == **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(); let speed = tweak.get::<f32>("animation_fast_speed").unwrap();
players.iter_mut().for_each(|mut p| { players.iter_mut().for_each(|mut p| {
debug!("Skipping animation"); debug!("Skipping animation");
// HACK: Set the speed of the animation to turbo
p.set_speed(speed); p.set_speed(speed);
}) })
} }

@ -21,7 +21,6 @@ impl Plugin for GamePlugin {
.run_if(on_event::<Move>()) .run_if(on_event::<Move>())
.after(handle_selection), .after(handle_selection),
set_side.run_if(any_component_changed::<BoardIndex>()), set_side.run_if(any_component_changed::<BoardIndex>()),
cancel_place.run_if(just_pressed(MouseButton::Right)),
handle_selection.run_if(on_event::<Selection>()), handle_selection.run_if(on_event::<Selection>()),
show_valid_moves.run_if(any_component_added::<Selected>()), show_valid_moves.run_if(any_component_added::<Selected>()),
hide_valid_moves.run_if(any_component_removed::<Selected>()), hide_valid_moves.run_if(any_component_removed::<Selected>()),
@ -372,14 +371,10 @@ impl Board {
fn new() -> Board { fn new() -> Board {
Board::from_ascii( Board::from_ascii(
// r#".....dqq r#".....dqq
// dpp..pdq dpp..pdq
// qdp..ppd qdp..ppd
// qqd....."#, qqd....."#,
r#"........
...p....
..p...p.
........"#,
) )
} }
@ -1040,9 +1035,6 @@ pub(crate) fn update_board(
if *from != *to_idx { if *from != *to_idx {
match move_type { match move_type {
MoveType::Promotion(piece) => { 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 commands
.entity(entity) .entity(entity)
.insert(*piece); .insert(*piece);
@ -1055,7 +1047,7 @@ pub(crate) fn update_board(
next_state.set(ns); 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 => { None => {
match move_type { match move_type {
MoveType::Capture => { MoveType::Capture => {
@ -1069,7 +1061,7 @@ pub(crate) fn update_board(
commands commands
.entity(entity) .entity(entity)
.remove::<BoardIndex>() .remove::<BoardIndex>()
.insert(Promoted); .insert((Promoted, Visibility::Hidden));
}, },
_ => { _ => {
panic!("How did you do this!?"); panic!("How did you do this!?");
@ -1353,18 +1345,6 @@ fn handle_selection(
*latest = None; *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 /// Panics if more than two pieces are selected at a time
fn asserts<T: Component>( fn asserts<T: Component>(
selected_pieces: Query<Entity, (With<Piece>, With<T>, With<Selected>)>, selected_pieces: Query<Entity, (With<Piece>, With<T>, With<Selected>)>,
@ -1431,6 +1411,7 @@ fn reset_game(
mut board: ResMut<Board>, mut board: ResMut<Board>,
mut score: ResMut<Score>, mut score: ResMut<Score>,
mut commands: Commands, mut commands: Commands,
mut turn_state: ResMut<NextState<TurnState>>,
) { ) {
// Setup the board // Setup the board
*board = Board::new(); *board = Board::new();
@ -1438,12 +1419,17 @@ fn reset_game(
// Reset the score // Reset the score
*score = Score { ..default() }; *score = Score { ..default() };
// Reset to starting side
turn_state.set(TurnState(Side::B));
// Move all pieces to their correct spots // Move all pieces to their correct spots
pieces pieces
.iter() .iter()
.zip(board.pieces().iter()) .zip(board.pieces().iter())
.for_each(|(e, (i, p))| { .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