Fix direction chance sound effect
This commit is contained in:
@@ -24,7 +24,7 @@ pub struct EatenEvent(Option<Entity>);
|
||||
#[derive(Component)]
|
||||
pub struct Fruit;
|
||||
|
||||
fn eaten_event_sent(mut eaten_event_reader: EventReader<EatenEvent>) -> bool {
|
||||
pub fn eaten_event_sent(mut eaten_event_reader: EventReader<EatenEvent>) -> bool {
|
||||
eaten_event_reader.iter().count() != 0
|
||||
}
|
||||
|
||||
|
||||
19
src/snake.rs
19
src/snake.rs
@@ -56,7 +56,7 @@ impl Plugin for SnakePlugin {
|
||||
game_over_system
|
||||
.in_schedule(CoreSchedule::FixedUpdate)
|
||||
.in_set(SystemSet::CollisionDetection)
|
||||
.in_set(OnUpdate(GameState::InGame))
|
||||
.run_if(in_state(GameState::InGame))
|
||||
.after(collision_system)
|
||||
.run_if(about_to_collide),
|
||||
)
|
||||
@@ -65,20 +65,13 @@ impl Plugin for SnakePlugin {
|
||||
.run_if(in_state(GameState::InGame))
|
||||
.run_if(about_to_collide),
|
||||
)
|
||||
.add_system(
|
||||
tick_sound_system
|
||||
.in_schedule(CoreSchedule::FixedUpdate)
|
||||
.run_if(in_state(GameState::InGame))
|
||||
.run_if(not(about_to_collide)),
|
||||
)
|
||||
.add_system(
|
||||
blip_sound_system
|
||||
.in_schedule(CoreSchedule::FixedUpdate)
|
||||
.run_if(in_state(GameState::InGame)),
|
||||
.run_if(fruit::eaten_event_sent),
|
||||
)
|
||||
.add_system(direction::start_game_system.in_set(OnUpdate(GameState::Begin)))
|
||||
.add_system(direction::start_game_system.run_if(in_state(GameState::Begin)))
|
||||
.add_system(direction::change_direction_system)
|
||||
.add_system(grid_transform_system.in_set(OnUpdate(GameState::InGame)))
|
||||
.add_system(grid_transform_system.run_if(in_state(GameState::InGame)))
|
||||
.add_system(add_tail_system)
|
||||
.add_system(bulge::add_bulge_system)
|
||||
.add_system(bulge::propagate_bulge_system)
|
||||
@@ -272,10 +265,6 @@ fn collision_sound_system(audio: Res<Audio>, audio_assets: Res<audio::Assets>) {
|
||||
audio.play(audio_assets.collision.clone());
|
||||
}
|
||||
|
||||
fn tick_sound_system(audio: Res<Audio>, audio_assets: Res<audio::Assets>) {
|
||||
audio.play(audio_assets.tick.clone());
|
||||
}
|
||||
|
||||
fn blip_sound_system(
|
||||
audio: Res<Audio>,
|
||||
audio_assets: Res<audio::Assets>,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use super::Snake;
|
||||
use crate::GameState;
|
||||
use crate::{audio, GameState};
|
||||
use bevy::prelude::*;
|
||||
use leafwing_input_manager::prelude::*;
|
||||
|
||||
#[derive(Actionlike, Component, Copy, Clone, Debug)]
|
||||
#[derive(Actionlike, Component, Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
@@ -11,7 +11,7 @@ pub(super) enum Direction {
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Component, Copy, Clone, Debug)]
|
||||
#[derive(Component, Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[component(storage = "SparseSet")]
|
||||
pub(super) struct NewDirection(Direction);
|
||||
|
||||
@@ -41,12 +41,14 @@ pub(super) fn apply_direction_system(
|
||||
pub(super) fn change_direction_system(
|
||||
mut commands: Commands,
|
||||
mut query: Query<(Entity, &ActionState<Direction>, Option<&Direction>), With<Snake>>,
|
||||
audio: Res<Audio>,
|
||||
audio_assets: Res<audio::Assets>,
|
||||
) {
|
||||
let (snake, action_state, direction) = query.single_mut();
|
||||
let new_direction = action_state.get_pressed().iter().cloned().last();
|
||||
let new_direction = action_state.get_just_pressed().iter().cloned().last();
|
||||
|
||||
if let Some(new_direction) = new_direction {
|
||||
if let Some(current_direction) = direction {
|
||||
if let Some(¤t_direction) = direction {
|
||||
if let (Direction::Up, Direction::Down)
|
||||
| (Direction::Down, Direction::Up)
|
||||
| (Direction::Left, Direction::Right)
|
||||
@@ -54,8 +56,13 @@ pub(super) fn change_direction_system(
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if new_direction == current_direction {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
commands.entity(snake).insert(NewDirection(new_direction));
|
||||
audio.play(audio_assets.tick.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user