input system
This commit is contained in:
@ -6,8 +6,29 @@ const ARENA_WIDTH: u32 = 20;
|
||||
const ARENA_HEIGHT: u32 = 15;
|
||||
const WINDOW_WIDTH: f32 = 1280.0;
|
||||
|
||||
#[derive(Eq, PartialEq, Copy, Clone)]
|
||||
enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
fn opposite(self) -> Self {
|
||||
match self {
|
||||
Self::Left => Self::Right,
|
||||
Self::Right => Self::Left,
|
||||
Self::Up => Self::Down,
|
||||
Self::Down => Self::Up,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct SnakeHead;
|
||||
struct SnakeHead {
|
||||
direction: Direction,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Position {
|
||||
@ -33,20 +54,43 @@ impl Size {
|
||||
#[derive(Resource)]
|
||||
struct MovementTimer(Timer);
|
||||
|
||||
fn snake_movement_system(
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
|
||||
mut head_transforms: Query<(&SnakeHead, &mut Position)>,
|
||||
) {
|
||||
for (_, mut pos) in head_transforms.iter_mut() {
|
||||
if input.pressed(KeyCode::ArrowUp) {
|
||||
pos.y += 1;
|
||||
fn snake_movement_input(input: Res<ButtonInput<KeyCode>>, mut heads: Query<(&mut SnakeHead)>) {
|
||||
if let Some(mut head) = heads.iter_mut().next() {
|
||||
let dir: Direction = if input.pressed(KeyCode::ArrowUp) {
|
||||
Direction::Up
|
||||
} else if input.pressed(KeyCode::ArrowDown) {
|
||||
pos.y -= 1;
|
||||
Direction::Down
|
||||
} else if input.pressed(KeyCode::ArrowLeft) {
|
||||
pos.x -= 1;
|
||||
Direction::Left
|
||||
} else if input.pressed(KeyCode::ArrowRight) {
|
||||
pos.x += 1;
|
||||
Direction::Right
|
||||
} else {
|
||||
head.direction
|
||||
};
|
||||
// allow all inputs except turning around
|
||||
if dir != head.direction.opposite() {
|
||||
head.direction = dir;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn snake_movement(
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
mut timer: ResMut<MovementTimer>,
|
||||
mut heads: Query<(&SnakeHead, &mut Position)>,
|
||||
) {
|
||||
if let Some((head, mut pos)) = heads.iter_mut().next() {
|
||||
if timer.0.tick(time.delta()).just_finished() {
|
||||
if head.direction == Direction::Up {
|
||||
pos.y += 1;
|
||||
} else if head.direction == Direction::Down {
|
||||
pos.y -= 1;
|
||||
} else if head.direction == Direction::Left {
|
||||
pos.x -= 1;
|
||||
} else if head.direction == Direction::Right {
|
||||
pos.x += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,10 +135,12 @@ fn setup_player(mut commands: Commands) {
|
||||
commands
|
||||
.spawn(Sprite {
|
||||
color: SNAKE_HEAD_COLOR,
|
||||
custom_size: Some(Vec2::new(10.0, 10.0)),
|
||||
custom_size: Some(Vec2::new(1.0, 1.0)),
|
||||
..default()
|
||||
})
|
||||
.insert(SnakeHead)
|
||||
.insert(SnakeHead {
|
||||
direction: Direction::Right,
|
||||
})
|
||||
.insert(Position { x: 0, y: 0 })
|
||||
.insert(Size::square(1.0));
|
||||
}
|
||||
@ -107,7 +153,8 @@ pub struct SnakePlugin;
|
||||
impl Plugin for SnakePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, (setup_camera, setup_player));
|
||||
app.add_systems(Update, snake_movement_system);
|
||||
app.add_systems(Update, snake_movement_input);
|
||||
app.add_systems(FixedUpdate, snake_movement);
|
||||
app.add_systems(PostUpdate, (scale_translation, position_translation));
|
||||
|
||||
app.insert_resource(MovementTimer(Timer::from_seconds(
|
||||
|
Reference in New Issue
Block a user