1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! To run this code, clone the rusty_engine repository and run the command:
//!
//! cargo run --release --example game_state
use std::f32::consts::TAU;
use rusty_engine::prelude::*;
// You can name your game state struct whatever you want, and it can contain many different types as
// its fields
struct MyCustomGameStateStuff {
// Use a timer to tell when to start/stop turning
change_timer: Timer,
// Usa a boolean track of whether we are currently turning or not
turning: bool,
}
fn main() {
let mut game = Game::new();
let _ = game.add_sprite("Race Car", SpritePreset::RacingCarGreen);
let initial_game_state = MyCustomGameStateStuff {
change_timer: Timer::from_seconds(1.0, true),
turning: false,
};
game.add_logic(logic);
game.run(initial_game_state);
}
fn logic(engine: &mut Engine, game_state: &mut MyCustomGameStateStuff) {
// Get mutable references to the variables in the game state that we care about
let race_car = engine.sprites.get_mut("Race Car").unwrap();
// If we aren't turning, then tick the timer until it's time to start turning again
if !game_state.turning && game_state.change_timer.tick(engine.delta).just_finished() {
game_state.turning = true;
}
// Rotate the player
if game_state.turning {
race_car.rotation += engine.delta_f32 * 3.0;
// If the player rotated all the way around, reset direction, stop turning
// TAU == (2 * PI), which is exactly one rotation in radians
if race_car.rotation > TAU {
race_car.rotation = 0.0;
game_state.turning = false;
}
}
}