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,31 +191,34 @@ fn move_player(
keyboard_input: Res<Input<KeyCode>>,
mut game: ResMut<Game>,
mut transforms: Query<&mut Transform>,
time: Res<Time>,
) {
if game.player.move_cooldown.tick(time.delta()).finished() {
let mut moved = false;
let mut rotation = 0.0;
if keyboard_input.just_pressed(KeyCode::Up) {
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.just_pressed(KeyCode::Down) {
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.just_pressed(KeyCode::Right) {
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.just_pressed(KeyCode::Left) {
if keyboard_input.pressed(KeyCode::Left) {
if game.player.j > 0 {
game.player.j -= 1;
}
@ -223,6 +228,7 @@ fn move_player(
// 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,
@ -233,6 +239,7 @@ fn move_player(
..Default::default()
};
}
}
// eat the cake!
if let Some(entity) = game.bonus.entity {