De-select does not work...

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

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

@ -2,12 +2,16 @@
/// TODO: Custom Asset: FmodEventMapper
///
use crate::prelude::*;
use bevy_fmod::prelude::AudioSource;
use bevy_fmod::prelude::*;
pub(crate) struct AudioPlugin;
impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.add_event::<AudioEvent>();
#[cfg(feature = "fmod")]
app.add_plugins(FmodPlugin {
audio_banks_paths: &[
"./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/SFX.bank",
],
})
.add_systems(OnExit(GameState::Loading), play_background)
.add_systems(
});
app.add_systems(OnExit(GameState::Loading), play_background);
app.add_systems(
Update,
(
play_audio.run_if(any_with_component::<AudioSource>()),
button_audio.run_if(any_with_component::<Button>()),
audio_events.run_if(on_event::<game::GameEvent>()),
play_audio.run_if(any_component_added::<AudioSource>),
audio_trigger.run_if(on_event::<AudioEvent>()),
),
);
}
}
#[derive(Event)]
pub enum AudioEvent {
MenuSelect,
PickUp,
PutDown,
}
fn play_background(studio: Res<FmodStudio>, mut commands: Commands) {
commands.spawn(AudioSource::new(
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());
}
fn button_audio(
events: Query<&Interaction, (Changed<Interaction>, With<Button>)>,
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>,
fn audio_trigger(
mut events: EventReader<AudioEvent>,
studio: Res<FmodStudio>,
mut commands: Commands,
) {
reader.iter().for_each(|event| match event {
game::GameEvent::SelectPiece => {
commands.spawn(AudioSource::new(
studio.0.get_event("event:/SFX/PickUpPiece").unwrap(),
));
}
game::GameEvent::PlacePiece => {
commands.spawn(AudioSource::new(
studio.0.get_event("event:/SFX/PutDownPiece").unwrap(),
));
}
events.iter().for_each(|event| {
let aud = match event {
AudioEvent::MenuSelect => studio.0.get_event("event:/SFX/MenuSelect"),
AudioEvent::PickUp => studio.0.get_event("event:/SFX/PickUpPiece"),
AudioEvent::PutDown => studio.0.get_event("event:/SFX/PutDownPiece"),
};
commands.spawn(AudioSource::new(aud.unwrap()));
})
}

@ -30,7 +30,7 @@ impl Plugin for Display3dPlugin {
.add_systems(
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_board_model.run_if(any_component_added::<game::BoardComponent>),
set_tile_hitbox.run_if(any_component_added::<game::Tile>),

@ -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| {
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!"),
}
}
});
});
}
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);
}
});
}

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

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

Loading…
Cancel
Save