input system

This commit is contained in:
Christian Nieves
2024-12-08 02:57:20 -06:00
parent dc7e53c58d
commit f02ba89a93

View File

@ -6,8 +6,29 @@ const ARENA_WIDTH: u32 = 20;
const ARENA_HEIGHT: u32 = 15; const ARENA_HEIGHT: u32 = 15;
const WINDOW_WIDTH: f32 = 1280.0; 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)] #[derive(Component)]
struct SnakeHead; struct SnakeHead {
direction: Direction,
}
#[derive(Component)] #[derive(Component)]
struct Position { struct Position {
@ -33,22 +54,45 @@ impl Size {
#[derive(Resource)] #[derive(Resource)]
struct MovementTimer(Timer); struct MovementTimer(Timer);
fn snake_movement_system( fn snake_movement_input(input: Res<ButtonInput<KeyCode>>, mut heads: Query<(&mut SnakeHead)>) {
input: Res<ButtonInput<KeyCode>>, if let Some(mut head) = heads.iter_mut().next() {
let dir: Direction = if input.pressed(KeyCode::ArrowUp) {
mut head_transforms: Query<(&SnakeHead, &mut Position)>, Direction::Up
) {
for (_, mut pos) in head_transforms.iter_mut() {
if input.pressed(KeyCode::ArrowUp) {
pos.y += 1;
} else if input.pressed(KeyCode::ArrowDown) { } else if input.pressed(KeyCode::ArrowDown) {
pos.y -= 1; Direction::Down
} else if input.pressed(KeyCode::ArrowLeft) { } else if input.pressed(KeyCode::ArrowLeft) {
pos.x -= 1; Direction::Left
} else if input.pressed(KeyCode::ArrowRight) { } else if input.pressed(KeyCode::ArrowRight) {
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; pos.x += 1;
} }
} }
}
} }
fn scale_translation( fn scale_translation(
@ -91,10 +135,12 @@ fn setup_player(mut commands: Commands) {
commands commands
.spawn(Sprite { .spawn(Sprite {
color: SNAKE_HEAD_COLOR, color: SNAKE_HEAD_COLOR,
custom_size: Some(Vec2::new(10.0, 10.0)), custom_size: Some(Vec2::new(1.0, 1.0)),
..default() ..default()
}) })
.insert(SnakeHead) .insert(SnakeHead {
direction: Direction::Right,
})
.insert(Position { x: 0, y: 0 }) .insert(Position { x: 0, y: 0 })
.insert(Size::square(1.0)); .insert(Size::square(1.0));
} }
@ -107,7 +153,8 @@ pub struct SnakePlugin;
impl Plugin for SnakePlugin { impl Plugin for SnakePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, (setup_camera, setup_player)); 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.add_systems(PostUpdate, (scale_translation, position_translation));
app.insert_resource(MovementTimer(Timer::from_seconds( app.insert_resource(MovementTimer(Timer::from_seconds(