Update Bevy to 0.10
This commit is contained in:
61
src/snake/direction.rs
Normal file
61
src/snake/direction.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use super::Snake;
|
||||
use crate::GameState;
|
||||
use bevy::prelude::*;
|
||||
use leafwing_input_manager::prelude::*;
|
||||
|
||||
#[derive(Actionlike, Component, Copy, Clone, Debug)]
|
||||
pub(super) enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Component, Copy, Clone, Debug)]
|
||||
#[component(storage = "SparseSet")]
|
||||
pub(super) struct NewDirection(Direction);
|
||||
|
||||
pub(super) fn start_game_system(
|
||||
query: Query<&ActionState<Direction>>,
|
||||
mut next_state: ResMut<NextState<GameState>>,
|
||||
) {
|
||||
let action_state = query.single();
|
||||
|
||||
if !action_state.get_just_pressed().is_empty() {
|
||||
next_state.set(GameState::InGame);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn apply_direction_system(
|
||||
mut commands: Commands,
|
||||
mut query: Query<(Entity, &NewDirection), With<Snake>>,
|
||||
) {
|
||||
for (snake, new_direction) in query.iter_mut() {
|
||||
commands
|
||||
.entity(snake)
|
||||
.insert(new_direction.0)
|
||||
.remove::<NewDirection>();
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn change_direction_system(
|
||||
mut commands: Commands,
|
||||
mut query: Query<(Entity, &ActionState<Direction>, Option<&Direction>), With<Snake>>,
|
||||
) {
|
||||
let (snake, action_state, direction) = query.single_mut();
|
||||
let new_direction = action_state.get_pressed().iter().cloned().last();
|
||||
|
||||
if let Some(new_direction) = new_direction {
|
||||
if let Some(current_direction) = direction {
|
||||
if let (Direction::Up, Direction::Down)
|
||||
| (Direction::Down, Direction::Up)
|
||||
| (Direction::Left, Direction::Right)
|
||||
| (Direction::Right, Direction::Left) = (current_direction, new_direction)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
commands.entity(snake).insert(NewDirection(new_direction));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user