WIP
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use super::Snake;
|
||||
use crate::{audio, GameState};
|
||||
use crate::{audio, level::LevelState, AppState};
|
||||
use bevy::prelude::*;
|
||||
use leafwing_input_manager::prelude::*;
|
||||
|
||||
#[derive(Actionlike, Component, Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Actionlike, Reflect, Component, Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
@@ -13,35 +13,34 @@ pub(super) enum Direction {
|
||||
|
||||
#[derive(Component, Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[component(storage = "SparseSet")]
|
||||
pub(super) struct NewDirection(Direction);
|
||||
pub(super) struct NextDirection(Direction);
|
||||
|
||||
pub(super) fn start_game_system(
|
||||
query: Query<&ActionState<Direction>>,
|
||||
mut next_state: ResMut<NextState<GameState>>,
|
||||
mut next_state: ResMut<NextState<AppState>>,
|
||||
) {
|
||||
let action_state = query.single();
|
||||
|
||||
if !action_state.get_just_pressed().is_empty() {
|
||||
next_state.set(GameState::InGame);
|
||||
next_state.set(AppState::InGame(LevelState::InGame));
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn apply_direction_system(
|
||||
mut commands: Commands,
|
||||
mut query: Query<(Entity, &NewDirection), With<Snake>>,
|
||||
mut query: Query<(Entity, &NextDirection), With<Snake>>,
|
||||
) {
|
||||
for (snake, new_direction) in query.iter_mut() {
|
||||
commands
|
||||
.entity(snake)
|
||||
.insert(new_direction.0)
|
||||
.remove::<NewDirection>();
|
||||
.remove::<NextDirection>();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -62,7 +61,10 @@ pub(super) fn change_direction_system(
|
||||
}
|
||||
}
|
||||
|
||||
commands.entity(snake).insert(NewDirection(new_direction));
|
||||
audio.play(audio_assets.tick.clone());
|
||||
commands.entity(snake).insert(NextDirection(new_direction));
|
||||
commands.spawn(AudioBundle {
|
||||
source: audio_assets.tick.clone(),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user