Skip to content

Commit 189c0f8

Browse files
committed
revert most of the examples
1 parent 2dc9111 commit 189c0f8

File tree

4 files changed

+64
-57
lines changed

4 files changed

+64
-57
lines changed

examples/keyboard_state.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,41 @@ fn logic(game_state: &mut Engine, _: &mut ()) {
3333

3434
// Handle keyboard input
3535
let ks = &mut game_state.keyboard_state;
36+
if ks.pressed_any(&[KeyCode::A, KeyCode::Left]) {
37+
race_car.translation.x -= move_amount;
38+
}
39+
if ks.pressed_any(&[KeyCode::D, KeyCode::Right, KeyCode::E]) {
40+
race_car.translation.x += move_amount;
41+
}
42+
if ks.pressed_any(&[KeyCode::O, KeyCode::Down, KeyCode::S]) {
43+
race_car.translation.y -= move_amount;
44+
}
45+
if ks.pressed_any(&[KeyCode::W, KeyCode::Up, KeyCode::Comma]) {
46+
race_car.translation.y += move_amount;
47+
}
48+
if ks.pressed_any(&[KeyCode::Z, KeyCode::Semicolon]) {
49+
race_car.rotation += rotation_amount;
50+
}
51+
if ks.pressed_any(&[KeyCode::C, KeyCode::J]) {
52+
race_car.rotation -= rotation_amount;
53+
}
54+
if ks.pressed_any(&[KeyCode::Plus, KeyCode::Equals]) {
55+
race_car.scale *= 1.0 + scale_amount;
56+
}
57+
if ks.pressed_any(&[KeyCode::Minus, KeyCode::Underline]) {
58+
race_car.scale *= 1.0 - scale_amount;
59+
}
3660

37-
ks //
38-
.pressed_any_do(&[KeyCode::A, KeyCode::Left], |_| {
39-
race_car.translation.x -= move_amount;
40-
})
41-
.pressed_any_do(&[KeyCode::D, KeyCode::Right, KeyCode::E], |_| {
42-
race_car.translation.x += move_amount;
43-
})
44-
.pressed_any_do(&[KeyCode::O, KeyCode::Down, KeyCode::S], |_| {
45-
race_car.translation.y -= move_amount;
46-
})
47-
.pressed_any_do(&[KeyCode::W, KeyCode::Up, KeyCode::Comma], |_| {
48-
race_car.translation.y += move_amount;
49-
})
50-
.pressed_any_do(&[KeyCode::Z, KeyCode::Semicolon], |_| {
51-
race_car.rotation += rotation_amount;
52-
})
53-
.pressed_any_do(&[KeyCode::C, KeyCode::J], |_| {
54-
race_car.rotation -= rotation_amount;
55-
})
56-
.pressed_any_do(&[KeyCode::Plus, KeyCode::Equals], |_| {
57-
race_car.scale *= 1.0 + scale_amount;
58-
})
59-
.pressed_any_do(&[KeyCode::Minus, KeyCode::Underline], |_| {
60-
race_car.scale *= 1.0 - scale_amount;
61-
});
61+
// If you prefer a more functional style, there are also methods ending in `_do` that accept a
62+
// closure to perform your logic with and are chainable, like this:
63+
//
64+
// ks.pressed_any_do(&[KeyCode::A, KeyCode::Left], |_| {
65+
// race_car.translation.x -= move_amount;
66+
// })
67+
// .pressed_any_do(&[KeyCode::D, KeyCode::Right, KeyCode::E], |_| {
68+
// race_car.translation.x += move_amount;
69+
// })
70+
// ...etc
6271

6372
// Clamp the scale to a certain range so the scaling is reasonable
6473
race_car.scale = race_car.scale.clamp(0.1, 3.0);

examples/mouse_state.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,12 @@ fn logic(engine: &mut Engine, _: &mut ()) {
4343
if let Some(sprite) = engine.sprites.get_mut("Race Car") {
4444
// Use the latest state of the mouse buttons to rotate the sprite
4545
let mut rotation_amount = 0.0;
46-
47-
engine
48-
.mouse_state
49-
.pressed_do(MouseButton::Left, |_| {
50-
rotation_amount += ROTATION_SPEED * engine.delta_f32;
51-
})
52-
.pressed_do(MouseButton::Right, |_| {
53-
rotation_amount -= ROTATION_SPEED * engine.delta_f32;
54-
});
55-
46+
if engine.mouse_state.pressed(MouseButton::Left) {
47+
rotation_amount += ROTATION_SPEED * engine.delta_f32;
48+
}
49+
if engine.mouse_state.pressed(MouseButton::Right) {
50+
rotation_amount -= ROTATION_SPEED * engine.delta_f32;
51+
}
5652
sprite.rotation += rotation_amount;
5753

5854
// Use the latest state of the mouse wheel to scale the sprite

examples/scenarios/extreme_drivers_ed.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -860,21 +860,19 @@ fn logic(engine: &mut Engine, game_state: &mut GameState) {
860860
{
861861
use KeyCode::*;
862862
// Acceleration input
863-
engine
864-
.keyboard_state
865-
.pressed_any_do(&[W, Up, Comma], |_| {
866-
acceleration += 1.0;
867-
})
868-
.pressed_any_do(&[S, Down, O], |_| {
869-
acceleration -= 1.0;
870-
})
871-
// Rotation/Turning input
872-
.pressed_any_do(&[A, Left], |_| {
873-
rotation += 1.0;
874-
})
875-
.pressed_any_do(&[D, Right, E], |_| {
876-
rotation -= 1.0;
877-
});
863+
if engine.keyboard_state.pressed_any(&[W, Up, Comma]) {
864+
acceleration += 1.0;
865+
}
866+
if engine.keyboard_state.pressed_any(&[S, Down, O]) {
867+
acceleration -= 1.0;
868+
}
869+
// Rotation/Turning input
870+
if engine.keyboard_state.pressed_any(&[A, Left]) {
871+
rotation += 1.0;
872+
}
873+
if engine.keyboard_state.pressed_any(&[D, Right, E]) {
874+
rotation -= 1.0;
875+
}
878876
}
879877
let mut velocity_magnitude = game_state.velocity.length();
880878
velocity_magnitude += (acceleration * ACCELERATION_RATE) * engine.delta_f32;

examples/scenarios/road_race.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,18 @@ fn game_logic(engine: &mut Engine, game_state: &mut GameState) {
7272

7373
// Collect keyboard input
7474
let mut direction = 0.0;
75-
engine
75+
if engine
7676
.keyboard_state
77-
.pressed_any_do(&[KeyCode::Up, KeyCode::W, KeyCode::Comma], |_| {
78-
direction += 1.0;
79-
})
80-
.pressed_any_do(&[KeyCode::Down, KeyCode::S, KeyCode::O], |_| {
81-
direction -= 1.0;
82-
});
77+
.pressed_any(&[KeyCode::Up, KeyCode::W, KeyCode::Comma])
78+
{
79+
direction += 1.0;
80+
}
81+
if engine
82+
.keyboard_state
83+
.pressed_any(&[KeyCode::Down, KeyCode::S, KeyCode::O])
84+
{
85+
direction -= 1.0;
86+
}
8387

8488
// Move the player sprite
8589
let player1 = engine.sprites.get_mut("player1").unwrap();

0 commit comments

Comments
 (0)