|
|
|
|
@ -6,8 +6,7 @@ pub(crate) struct GamePlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for GamePlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.add_event::<GameEvent>()
|
|
|
|
|
.add_event::<Move>()
|
|
|
|
|
app.add_event::<Move>()
|
|
|
|
|
.add_systems(Startup, setup_board)
|
|
|
|
|
.add_systems(
|
|
|
|
|
Update,
|
|
|
|
|
@ -23,6 +22,12 @@ impl Plugin for GamePlugin {
|
|
|
|
|
move_piece.run_if(any_component_added::<Selected>),
|
|
|
|
|
capture_piece.run_if(any_component_added::<Captured>),
|
|
|
|
|
null_selections.run_if(any_component_added::<Selected>),
|
|
|
|
|
pick_up_audio::<display2d::Display2d>
|
|
|
|
|
.run_if(in_state(DisplayState::Display2d))
|
|
|
|
|
.run_if(any_component_added::<Selected>),
|
|
|
|
|
pick_up_audio::<display3d::Display3d>
|
|
|
|
|
.run_if(in_state(DisplayState::Display3d))
|
|
|
|
|
.run_if(any_component_added::<Selected>),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(
|
|
|
|
|
@ -88,12 +93,6 @@ pub(crate) enum GameError {
|
|
|
|
|
InvalidIndex,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Event)]
|
|
|
|
|
pub(crate) enum GameEvent {
|
|
|
|
|
SelectPiece,
|
|
|
|
|
PlacePiece,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The board is setup like this:
|
|
|
|
|
/// ```text
|
|
|
|
|
/// 0 1 2 3 4 5 6 7
|
|
|
|
|
@ -432,33 +431,49 @@ fn move_piece(
|
|
|
|
|
selected_pieces: Query<&BoardIndex, (With<Selected>, With<Piece>)>,
|
|
|
|
|
mut board: ResMut<Board>,
|
|
|
|
|
mut move_events: EventWriter<Move>,
|
|
|
|
|
mut writer: EventWriter<audio::AudioEvent>,
|
|
|
|
|
) {
|
|
|
|
|
events.iter().for_each(|to| {
|
|
|
|
|
selected_pieces.iter().for_each(|from| {
|
|
|
|
|
// Move piece
|
|
|
|
|
match board.move_piece(*from, *to) {
|
|
|
|
|
Ok(moves) => {
|
|
|
|
|
// De-select the piece
|
|
|
|
|
info!("Applying moves {:?}", moves);
|
|
|
|
|
moves.iter().for_each(|m| move_events.send(m.clone()));
|
|
|
|
|
if from != to {
|
|
|
|
|
info!("Applying move {:?} -> {:?}", from, to);
|
|
|
|
|
// Move piece
|
|
|
|
|
match board.move_piece(*from, *to) {
|
|
|
|
|
Ok(moves) => {
|
|
|
|
|
// De-select the piece
|
|
|
|
|
info!("Applying moves {:?}", moves);
|
|
|
|
|
moves.iter().for_each(|m| move_events.send(m.clone()));
|
|
|
|
|
writer.send(audio::AudioEvent::PutDown)
|
|
|
|
|
}
|
|
|
|
|
Err(GameError::NullMove) => warn!("Null move!"),
|
|
|
|
|
Err(GameError::InvalidIndex) => warn!("Invalid index!"),
|
|
|
|
|
}
|
|
|
|
|
Err(GameError::NullMove) => warn!("Null move!"),
|
|
|
|
|
Err(GameError::InvalidIndex) => warn!("Invalid index!"),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn pick_up_audio<D: Component>(
|
|
|
|
|
selected: Query<Entity, (With<Selected>, With<Piece>, With<D>)>,
|
|
|
|
|
mut writer: EventWriter<audio::AudioEvent>,
|
|
|
|
|
) {
|
|
|
|
|
if selected.iter().len() == 1 {
|
|
|
|
|
writer.send(audio::AudioEvent::PickUp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// De-select anything that shouldn't be selected
|
|
|
|
|
/// Namely tiles when there are not selected pieces
|
|
|
|
|
fn null_selections(
|
|
|
|
|
events: Query<Entity, Added<Selected>>,
|
|
|
|
|
selected_pieces: Query<Entity, (With<Selected>, With<Piece>)>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
mut writer: EventWriter<audio::AudioEvent>,
|
|
|
|
|
) {
|
|
|
|
|
events.iter().for_each(|entity| {
|
|
|
|
|
if selected_pieces.is_empty() {
|
|
|
|
|
commands.entity(entity).remove::<Selected>();
|
|
|
|
|
writer.send(audio::AudioEvent::PutDown);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|