Skip to content

Commit 5104397

Browse files
committed
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.
1 parent 330160c commit 5104397

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

examples/game/alien_cake_addict.rs

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct Player {
4646
entity: Option<Entity>,
4747
i: usize,
4848
j: usize,
49+
move_cooldown: Timer,
4950
}
5051

5152
#[derive(Default)]
@@ -97,6 +98,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMu
9798
game.score = 0;
9899
game.player.i = BOARD_SIZE_I / 2;
99100
game.player.j = BOARD_SIZE_J / 2;
101+
game.player.move_cooldown = Timer::from_seconds(0.3, false);
100102

101103
commands.spawn_bundle(PointLightBundle {
102104
transform: Transform::from_xyz(4.0, 10.0, 4.0),
@@ -189,49 +191,54 @@ fn move_player(
189191
keyboard_input: Res<Input<KeyCode>>,
190192
mut game: ResMut<Game>,
191193
mut transforms: Query<&mut Transform>,
194+
time: Res<Time>,
192195
) {
193-
let mut moved = false;
194-
let mut rotation = 0.0;
195-
if keyboard_input.just_pressed(KeyCode::Up) {
196-
if game.player.i < BOARD_SIZE_I - 1 {
197-
game.player.i += 1;
196+
if game.player.move_cooldown.tick(time.delta()).finished() {
197+
let mut moved = false;
198+
let mut rotation = 0.0;
199+
200+
if keyboard_input.pressed(KeyCode::Up) {
201+
if game.player.i < BOARD_SIZE_I - 1 {
202+
game.player.i += 1;
203+
}
204+
rotation = -std::f32::consts::FRAC_PI_2;
205+
moved = true;
198206
}
199-
rotation = -std::f32::consts::FRAC_PI_2;
200-
moved = true;
201-
}
202-
if keyboard_input.just_pressed(KeyCode::Down) {
203-
if game.player.i > 0 {
204-
game.player.i -= 1;
207+
if keyboard_input.pressed(KeyCode::Down) {
208+
if game.player.i > 0 {
209+
game.player.i -= 1;
210+
}
211+
rotation = std::f32::consts::FRAC_PI_2;
212+
moved = true;
205213
}
206-
rotation = std::f32::consts::FRAC_PI_2;
207-
moved = true;
208-
}
209-
if keyboard_input.just_pressed(KeyCode::Right) {
210-
if game.player.j < BOARD_SIZE_J - 1 {
211-
game.player.j += 1;
214+
if keyboard_input.pressed(KeyCode::Right) {
215+
if game.player.j < BOARD_SIZE_J - 1 {
216+
game.player.j += 1;
217+
}
218+
rotation = std::f32::consts::PI;
219+
moved = true;
212220
}
213-
rotation = std::f32::consts::PI;
214-
moved = true;
215-
}
216-
if keyboard_input.just_pressed(KeyCode::Left) {
217-
if game.player.j > 0 {
218-
game.player.j -= 1;
221+
if keyboard_input.pressed(KeyCode::Left) {
222+
if game.player.j > 0 {
223+
game.player.j -= 1;
224+
}
225+
rotation = 0.0;
226+
moved = true;
219227
}
220-
rotation = 0.0;
221-
moved = true;
222-
}
223228

224-
// move on the board
225-
if moved {
226-
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
227-
translation: Vec3::new(
228-
game.player.i as f32,
229-
game.board[game.player.j][game.player.i].height,
230-
game.player.j as f32,
231-
),
232-
rotation: Quat::from_rotation_y(rotation),
233-
..Default::default()
234-
};
229+
// move on the board
230+
if moved {
231+
game.player.move_cooldown.reset();
232+
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
233+
translation: Vec3::new(
234+
game.player.i as f32,
235+
game.board[game.player.j][game.player.i].height,
236+
game.player.j as f32,
237+
),
238+
rotation: Quat::from_rotation_y(rotation),
239+
..Default::default()
240+
};
241+
}
235242
}
236243

237244
// eat the cake!

0 commit comments

Comments
 (0)