Use input manager for snake actions
This commit is contained in:
@@ -74,12 +74,7 @@ fn setup_system(mut commands: Commands) {
|
||||
},
|
||||
..Default::default()
|
||||
})
|
||||
.insert(Name::new("Orthographic Camera"))
|
||||
// .insert(BloomSettings {
|
||||
// threshold: 0.7,
|
||||
// ..Default::default()
|
||||
// })
|
||||
;
|
||||
.insert(Name::new("Orthographic Camera"));
|
||||
}
|
||||
|
||||
fn camera_move_system(
|
||||
|
||||
101
src/snake.rs
101
src/snake.rs
@@ -1,11 +1,14 @@
|
||||
mod appearance;
|
||||
mod bulge;
|
||||
mod direction;
|
||||
mod movement;
|
||||
|
||||
use crate::{fruit, grid, tick, AppState};
|
||||
use bevy::prelude::*;
|
||||
use direction::Direction;
|
||||
use itertools::Itertools;
|
||||
use iyes_loopless::prelude::*;
|
||||
use leafwing_input_manager::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, SystemLabel)]
|
||||
enum SystemLabel {
|
||||
@@ -20,11 +23,9 @@ impl Plugin for SnakePlugin {
|
||||
SystemSet::new().with_system(segments_movement_system.run_in_state(AppState::InGame));
|
||||
|
||||
let ticked_system_set = SystemSet::new()
|
||||
.with_system(
|
||||
apply_direction_system
|
||||
.run_in_state(AppState::InGame)
|
||||
.before(SystemLabel::Collision),
|
||||
)
|
||||
.with_system(direction::apply_direction_system.run_in_state(AppState::InGame));
|
||||
|
||||
let collision_system_set = SystemSet::new()
|
||||
.with_system(
|
||||
game_over_system
|
||||
.run_in_state(AppState::InGame)
|
||||
@@ -38,13 +39,16 @@ impl Plugin for SnakePlugin {
|
||||
|
||||
app.add_fixed_timestep_system_set(tick::TICK_TIME_STEP_NAME, 0, ticked_system_set)
|
||||
.add_fixed_timestep_child_stage(tick::TICK_TIME_STEP_NAME)
|
||||
.add_fixed_timestep_system_set(tick::TICK_TIME_STEP_NAME, 1, movement_system_set);
|
||||
.add_fixed_timestep_system_set(tick::TICK_TIME_STEP_NAME, 1, collision_system_set)
|
||||
.add_fixed_timestep_child_stage(tick::TICK_TIME_STEP_NAME)
|
||||
.add_fixed_timestep_system_set(tick::TICK_TIME_STEP_NAME, 2, movement_system_set);
|
||||
|
||||
app.add_event::<AddTailEvent>()
|
||||
app.add_plugin(InputManagerPlugin::<direction::Direction>::default())
|
||||
.add_event::<AddTailEvent>()
|
||||
.insert_resource(bulge::PropagationTimer::default())
|
||||
.add_startup_system(setup_snake_system)
|
||||
.add_system(add_direction_system.run_in_state(AppState::Begin))
|
||||
.add_system(change_direction_system.run_in_state(AppState::InGame))
|
||||
.add_system(direction::start_game_system.run_in_state(AppState::Begin))
|
||||
.add_system(direction::change_direction_system)
|
||||
.add_system(grid_transform_system.run_in_state(AppState::InGame))
|
||||
.add_system(add_tail_system)
|
||||
.add_system(bulge::add_bulge_system)
|
||||
@@ -74,7 +78,6 @@ struct Snake;
|
||||
#[derive(Bundle)]
|
||||
struct SnakeBundle {
|
||||
snake: Snake,
|
||||
direction_buffer: DirectionBuffer,
|
||||
collision: Collision,
|
||||
segments: Segments,
|
||||
|
||||
@@ -82,33 +85,6 @@ struct SnakeBundle {
|
||||
spatial_bundle: SpatialBundle,
|
||||
}
|
||||
|
||||
#[derive(Component, Copy, Clone, Debug)]
|
||||
enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
fn from_keypress(keypress: Res<Input<KeyCode>>) -> Option<Self> {
|
||||
if keypress.pressed(KeyCode::Up) {
|
||||
Some(Self::Up)
|
||||
} else if keypress.pressed(KeyCode::Down) {
|
||||
Some(Self::Down)
|
||||
} else if keypress.pressed(KeyCode::Left) {
|
||||
Some(Self::Left)
|
||||
} else if keypress.pressed(KeyCode::Right) {
|
||||
Some(Self::Right)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Default, Debug)]
|
||||
struct DirectionBuffer(Option<Direction>);
|
||||
|
||||
#[derive(Component, Default, Debug)]
|
||||
struct Collision {
|
||||
about_to_collide: bool,
|
||||
@@ -144,28 +120,23 @@ fn setup_snake_system(mut commands: Commands) {
|
||||
commands
|
||||
.spawn(SnakeBundle {
|
||||
snake: Snake,
|
||||
direction_buffer: Default::default(),
|
||||
collision: Default::default(),
|
||||
segments: Segments(vec![snake_head]),
|
||||
spatial_bundle: Default::default(),
|
||||
})
|
||||
.insert(Name::new("Snake"))
|
||||
.insert(InputManagerBundle::<Direction> {
|
||||
input_map: InputMap::new([
|
||||
(KeyCode::Up, Direction::Up),
|
||||
(KeyCode::Down, Direction::Down),
|
||||
(KeyCode::Left, Direction::Left),
|
||||
(KeyCode::Right, Direction::Right),
|
||||
]),
|
||||
..Default::default()
|
||||
})
|
||||
.add_child(snake_head);
|
||||
}
|
||||
|
||||
fn add_direction_system(
|
||||
mut commands: Commands,
|
||||
keypress: Res<Input<KeyCode>>,
|
||||
mut query: Query<Entity, With<Snake>>,
|
||||
) {
|
||||
if let Some(direction) = Direction::from_keypress(keypress) {
|
||||
let snake = query.single_mut();
|
||||
commands.entity(snake).insert(direction);
|
||||
|
||||
commands.insert_resource(NextState(AppState::InGame));
|
||||
}
|
||||
}
|
||||
|
||||
fn eaten_event_system(
|
||||
mut eaten_event_reader: EventReader<fruit::EatenEvent>,
|
||||
mut tail_event_writer: EventWriter<AddTailEvent>,
|
||||
@@ -196,34 +167,6 @@ fn add_tail_system(
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_direction_system(mut query: Query<(&mut Direction, &mut DirectionBuffer), With<Snake>>) {
|
||||
for (mut direction, mut direction_buffer) in query.iter_mut() {
|
||||
if let Some(new_direction) = direction_buffer.0 {
|
||||
*direction = new_direction;
|
||||
direction_buffer.0 = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn change_direction_system(
|
||||
keypress: Res<Input<KeyCode>>,
|
||||
mut query: Query<(&Direction, &mut DirectionBuffer), With<Snake>>,
|
||||
) {
|
||||
if let Some(new_direction) = Direction::from_keypress(keypress) {
|
||||
let (direction, mut direction_buffer) = query.single_mut();
|
||||
|
||||
if let (Direction::Up, Direction::Down)
|
||||
| (Direction::Down, Direction::Up)
|
||||
| (Direction::Left, Direction::Right)
|
||||
| (Direction::Right, Direction::Left) = (direction, new_direction)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
direction_buffer.0 = Some(new_direction);
|
||||
}
|
||||
}
|
||||
|
||||
fn grid_transform_system(
|
||||
mut query: Query<(&mut Transform, &grid::Coordinate), With<SnakeSegment>>,
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user