mirror of
https://github.com/bevyengine/bevy
synced 2024-09-20 14:32:06 +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>,
|
entity: Option<Entity>,
|
||||||
i: usize,
|
i: usize,
|
||||||
j: usize,
|
j: usize,
|
||||||
|
move_cooldown: Timer,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -97,6 +98,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
|
||||||
game.score = 0;
|
game.score = 0;
|
||||||
game.player.i = BOARD_SIZE_I / 2;
|
game.player.i = BOARD_SIZE_I / 2;
|
||||||
game.player.j = BOARD_SIZE_J / 2;
|
game.player.j = BOARD_SIZE_J / 2;
|
||||||
|
game.player.move_cooldown = Timer::from_seconds(0.3, false);
|
||||||
|
|
||||||
commands.spawn_bundle(PointLightBundle {
|
commands.spawn_bundle(PointLightBundle {
|
||||||
transform: Transform::from_xyz(4.0, 10.0, 4.0),
|
transform: Transform::from_xyz(4.0, 10.0, 4.0),
|
||||||
|
@ -189,49 +191,54 @@ fn move_player(
|
||||||
keyboard_input: Res<Input<KeyCode>>,
|
keyboard_input: Res<Input<KeyCode>>,
|
||||||
mut game: ResMut<Game>,
|
mut game: ResMut<Game>,
|
||||||
mut transforms: Query<&mut Transform>,
|
mut transforms: Query<&mut Transform>,
|
||||||
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
let mut moved = false;
|
if game.player.move_cooldown.tick(time.delta()).finished() {
|
||||||
let mut rotation = 0.0;
|
let mut moved = false;
|
||||||
if keyboard_input.just_pressed(KeyCode::Up) {
|
let mut rotation = 0.0;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// move on the board
|
if keyboard_input.pressed(KeyCode::Up) {
|
||||||
if moved {
|
if game.player.i < BOARD_SIZE_I - 1 {
|
||||||
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
|
game.player.i += 1;
|
||||||
translation: Vec3::new(
|
}
|
||||||
game.player.i as f32,
|
rotation = -std::f32::consts::FRAC_PI_2;
|
||||||
game.board[game.player.j][game.player.i].height,
|
moved = true;
|
||||||
game.player.j as f32,
|
}
|
||||||
),
|
if keyboard_input.pressed(KeyCode::Down) {
|
||||||
rotation: Quat::from_rotation_y(rotation),
|
if game.player.i > 0 {
|
||||||
..Default::default()
|
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!
|
// eat the cake!
|
||||||
|
|
Loading…
Reference in a new issue