De-select does not work...

bevy0.12
Elijah Voigt 2 years ago
parent ea99af748c
commit 983779c948

@ -1,13 +1,8 @@
[toolchain] [toolchain]
channel = "nightly" channel = "nightly"
components = [ components = ["rustfmt", "rustc-dev", "clippy", "rls"]
"rustfmt",
"rustc-dev",
"clippy",
"rls",
]
targets = [ targets = [
"aarch64-apple-darwin", # "aarch64-apple-darwin",
"x86_64-unknown-linux-gnu", "x86_64-unknown-linux-gnu",
"x86_64-pc-windows-msvc", # "x86_64-pc-windows-msvc",
] ]

@ -2,12 +2,16 @@
/// TODO: Custom Asset: FmodEventMapper /// TODO: Custom Asset: FmodEventMapper
/// ///
use crate::prelude::*; use crate::prelude::*;
use bevy_fmod::prelude::AudioSource;
use bevy_fmod::prelude::*; use bevy_fmod::prelude::*;
pub(crate) struct AudioPlugin; pub(crate) struct AudioPlugin;
impl Plugin for AudioPlugin { impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_event::<AudioEvent>();
#[cfg(feature = "fmod")]
app.add_plugins(FmodPlugin { app.add_plugins(FmodPlugin {
audio_banks_paths: &[ audio_banks_paths: &[
"./assets/audio/Martian Chess Audio/Build/Desktop/Master.bank", "./assets/audio/Martian Chess Audio/Build/Desktop/Master.bank",
@ -15,19 +19,26 @@ impl Plugin for AudioPlugin {
"./assets/audio/Martian Chess Audio/Build/Desktop/Music.bank", "./assets/audio/Martian Chess Audio/Build/Desktop/Music.bank",
"./assets/audio/Martian Chess Audio/Build/Desktop/SFX.bank", "./assets/audio/Martian Chess Audio/Build/Desktop/SFX.bank",
], ],
}) });
.add_systems(OnExit(GameState::Loading), play_background)
.add_systems( app.add_systems(OnExit(GameState::Loading), play_background);
app.add_systems(
Update, Update,
( (
play_audio.run_if(any_with_component::<AudioSource>()), play_audio.run_if(any_component_added::<AudioSource>),
button_audio.run_if(any_with_component::<Button>()), audio_trigger.run_if(on_event::<AudioEvent>()),
audio_events.run_if(on_event::<game::GameEvent>()),
), ),
); );
} }
} }
#[derive(Event)]
pub enum AudioEvent {
MenuSelect,
PickUp,
PutDown,
}
fn play_background(studio: Res<FmodStudio>, mut commands: Commands) { fn play_background(studio: Res<FmodStudio>, mut commands: Commands) {
commands.spawn(AudioSource::new( commands.spawn(AudioSource::new(
studio.0.get_event("event:/Music/Main Track").unwrap(), studio.0.get_event("event:/Music/Main Track").unwrap(),
@ -38,36 +49,17 @@ fn play_audio(mut events: Query<&AudioSource, Added<AudioSource>>) {
events.iter_mut().for_each(|aud| aud.play()); events.iter_mut().for_each(|aud| aud.play());
} }
fn button_audio( fn audio_trigger(
events: Query<&Interaction, (Changed<Interaction>, With<Button>)>, mut events: EventReader<AudioEvent>,
studio: Res<FmodStudio>,
mut commands: Commands,
) {
events
.iter()
.filter(|&interaction| *interaction != Interaction::None)
.for_each(|_| {
commands.spawn(AudioSource::new(
studio.0.get_event("event:/SFX/MenuSelect").unwrap(),
));
});
}
fn audio_events(
mut reader: EventReader<game::GameEvent>,
studio: Res<FmodStudio>, studio: Res<FmodStudio>,
mut commands: Commands, mut commands: Commands,
) { ) {
reader.iter().for_each(|event| match event { events.iter().for_each(|event| {
game::GameEvent::SelectPiece => { let aud = match event {
commands.spawn(AudioSource::new( AudioEvent::MenuSelect => studio.0.get_event("event:/SFX/MenuSelect"),
studio.0.get_event("event:/SFX/PickUpPiece").unwrap(), AudioEvent::PickUp => studio.0.get_event("event:/SFX/PickUpPiece"),
)); AudioEvent::PutDown => studio.0.get_event("event:/SFX/PutDownPiece"),
} };
game::GameEvent::PlacePiece => { commands.spawn(AudioSource::new(aud.unwrap()));
commands.spawn(AudioSource::new(
studio.0.get_event("event:/SFX/PutDownPiece").unwrap(),
));
}
}) })
} }

@ -30,7 +30,7 @@ impl Plugin for Display3dPlugin {
.add_systems( .add_systems(
Update, Update,
( (
hydrate_camera, // TODO: add run_if... hydrate_camera.run_if(any_component_added::<Camera3d>),
set_piece_model.run_if(any_component_added::<Piece>), set_piece_model.run_if(any_component_added::<Piece>),
set_board_model.run_if(any_component_added::<game::BoardComponent>), set_board_model.run_if(any_component_added::<game::BoardComponent>),
set_tile_hitbox.run_if(any_component_added::<game::Tile>), set_tile_hitbox.run_if(any_component_added::<game::Tile>),

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

@ -1,8 +1,6 @@
#![feature(iter_array_chunks)] // used in ray.rs #![feature(iter_array_chunks)] // used in ray.rs
#[cfg(feature = "fmod")]
mod audio; mod audio;
mod credits; mod credits;
mod debug; mod debug;
mod display2d; mod display2d;
@ -59,7 +57,6 @@ fn main() {
app.add_plugins(game::GamePlugin); app.add_plugins(game::GamePlugin);
app.add_plugins(loading::LoadingPlugin); app.add_plugins(loading::LoadingPlugin);
app.add_plugins(menu::MenuPlugin); app.add_plugins(menu::MenuPlugin);
#[cfg(feature = "fmod")]
app.add_plugins(audio::AudioPlugin); app.add_plugins(audio::AudioPlugin);
app.run(); app.run();
} }

@ -142,12 +142,12 @@ fn handle_menu_button(
mut next_gs: ResMut<NextState<GameState>>, mut next_gs: ResMut<NextState<GameState>>,
mut next_ds: ResMut<NextState<DisplayState>>, mut next_ds: ResMut<NextState<DisplayState>>,
) { ) {
events events.iter().for_each(|(i, gs, ds)| match i {
.iter() Interaction::Pressed => {
.filter(|(&interaction, ..)| interaction == Interaction::Pressed)
.for_each(|(.., gs, ds)| {
next_gs.set(gs.clone()); next_gs.set(gs.clone());
next_ds.set(ds.clone()); next_ds.set(ds.clone());
}
_ => (),
}); });
} }
@ -164,6 +164,7 @@ fn handle_menu_quit(
fn interactive_button( fn interactive_button(
mut events: Query<(&mut BackgroundColor, &Interaction), (With<Button>, Changed<Interaction>)>, mut events: Query<(&mut BackgroundColor, &Interaction), (With<Button>, Changed<Interaction>)>,
mut window: Query<&mut Window, With<PrimaryWindow>>, mut window: Query<&mut Window, With<PrimaryWindow>>,
mut writer: EventWriter<crate::audio::AudioEvent>,
) { ) {
events events
.iter_mut() .iter_mut()
@ -175,6 +176,7 @@ fn interactive_button(
Interaction::Hovered => { Interaction::Hovered => {
window.single_mut().cursor.icon = CursorIcon::Hand; window.single_mut().cursor.icon = CursorIcon::Hand;
bg_color.0.set_a(0.75); bg_color.0.set_a(0.75);
writer.send(crate::audio::AudioEvent::MenuSelect)
} }
Interaction::Pressed => { Interaction::Pressed => {
window.single_mut().cursor.icon = CursorIcon::Grab; window.single_mut().cursor.icon = CursorIcon::Grab;

Loading…
Cancel
Save