Update Bevy to 0.10

This commit is contained in:
2023-03-12 20:34:23 +01:00
parent 326e75e40a
commit 3724db44b3
10 changed files with 853 additions and 920 deletions

61
src/snake/direction.rs Normal file
View 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));
}
}