Alien cake addict: Allow holding movement keys (#2072)

I wanted to try one of the new examples but it felt so clunky that I wanted to improve it.

It did make me feel like maybe some input axes abstraction like Unity has might be useful.

Also, eating cake should probably be a separate system from movement.
This commit is contained in:
RedlineTriad 2022-02-15 22:14:58 +00:00
parent 330160cf14
commit 5104397683

View file

@ -46,6 +46,7 @@ struct Player {
entity: Option<Entity>,
i: usize,
j: usize,
move_cooldown: Timer,
}
#[derive(Default)]
@ -97,6 +98,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
game.score = 0;
game.player.i = BOARD_SIZE_I / 2;
game.player.j = BOARD_SIZE_J / 2;
game.player.move_cooldown = Timer::from_seconds(0.3, false);
commands.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(4.0, 10.0, 4.0),
@ -189,49 +191,54 @@ fn move_player(
keyboard_input: Res<Input<KeyCode>>,
mut game: ResMut<Game>,
mut transforms: Query<&mut Transform>,
time: Res<Time>,
) {
let mut moved = false;
let mut rotation = 0.0;
if keyboard_input.just_pressed(KeyCode::Up) {
if game.player.i < BOARD_SIZE_I - 1 {
game.player.i += 1;
}
rotation = -std::f32::consts::FRAC_PI_2;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Down) {
if game.player.i > 0 {
game.player.i -= 1;
}
rotation = std::f32::consts::FRAC_PI_2;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Right) {
if game.player.j < BOARD_SIZE_J - 1 {
game.player.j += 1;
}
rotation = std::f32::consts::PI;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Left) {
if game.player.j > 0 {
game.player.j -= 1;
}
rotation = 0.0;
moved = true;
}
if game.player.move_cooldown.tick(time.delta()).finished() {
let mut moved = false;
let mut rotation = 0.0;
// move on the board
if moved {
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
translation: Vec3::new(
game.player.i as f32,
game.board[game.player.j][game.player.i].height,
game.player.j as f32,
),
rotation: Quat::from_rotation_y(rotation),
..Default::default()
};
if keyboard_input.pressed(KeyCode::Up) {
if game.player.i < BOARD_SIZE_I - 1 {
game.player.i += 1;
}
rotation = -std::f32::consts::FRAC_PI_2;
moved = true;
}
if keyboard_input.pressed(KeyCode::Down) {
if game.player.i > 0 {
game.player.i -= 1;
}
rotation = std::f32::consts::FRAC_PI_2;
moved = true;
}
if keyboard_input.pressed(KeyCode::Right) {
if game.player.j < BOARD_SIZE_J - 1 {
game.player.j += 1;
}
rotation = std::f32::consts::PI;
moved = true;
}
if keyboard_input.pressed(KeyCode::Left) {
if game.player.j > 0 {
game.player.j -= 1;
}
rotation = 0.0;
moved = true;
}
// move on the board
if moved {
game.player.move_cooldown.reset();
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
translation: Vec3::new(
game.player.i as f32,
game.board[game.player.j][game.player.i].height,
game.player.j as f32,
),
rotation: Quat::from_rotation_y(rotation),
..Default::default()
};
}
}
// eat the cake!