mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 15:14:50 +00:00
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:
parent
330160cf14
commit
5104397683
1 changed files with 48 additions and 41 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue