Fix direction chance sound effect

This commit is contained in:
2023-03-18 17:51:00 +01:00
parent 5d92c82db4
commit 18f55a9cce
3 changed files with 18 additions and 22 deletions

View File

@@ -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(&current_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());
}
}