Skip to content

Commit 75fc4e4

Browse files
authored
Merge pull request #37 from CleanCut/engine
Rename EngineState -> Engine and engine_state -> engine
2 parents cb2347c + fac82a9 commit 75fc4e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+401
-422
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
### BREAKING CHANGES
55

66
- Logic functions no longer return a `bool` to simplify the learning curve. If you want logic functions to run conditionally, instead track your state in your `GameState` and use it to exit early from your logic function.
7+
- The `EngineState` struct and `engine_state` variables have been renamed to `Engine` and `engine`, respectively, for brevity.
78

89
## [4.0.0] - 2022-01-29
910

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Write your game!
7373
fn main() {
7474
// Create a game
7575
let mut game = Game::new();
76-
// Set up your game. `Game` exposes all of the methods (but not fields) of `EngineState` as well.
76+
// Set up your game. `Game` exposes all of the methods (but not fields) of `Engine` as well.
7777
let sprite = game.add_sprite("player", SpritePreset::RacingCarBlue);
7878
sprite.scale = 2.0;
7979
game.audio_manager.play_music(MusicPreset::Classy8Bit, 1.0);
@@ -86,16 +86,16 @@ Write your game!
8686
}
8787

8888
// Your game logic functions can be named anything, but the first parameter is always a
89-
// `&mut EngineState`, and the second parameter is a mutable reference to your custom game
89+
// `&mut Engine`, and the second parameter is a mutable reference to your custom game
9090
// state struct (`&mut GameState` in this case). The function returns a `bool`.
9191
//
9292
// This function will be run once each frame.
93-
fn game_logic(engine_state: &mut EngineState, game_state: &mut GameState) {
94-
// The `EngineState` contains all sorts of built-in goodies.
93+
fn game_logic(engine: &mut Engine, game_state: &mut GameState) {
94+
// The `Engine` contains all sorts of built-in goodies.
9595
// Get access to the player sprite...
96-
let player = engine_state.sprites.get_mut("player").unwrap();
96+
let player = engine.sprites.get_mut("player").unwrap();
9797
// Rotate the player...
98-
player.rotation += std::f32::consts::PI * engine_state.delta_f32;
98+
player.rotation += std::f32::consts::PI * engine.delta_f32;
9999
// Damage the player if it is out of bounds...
100100
if player.translation.x > 100.0 {
101101
game_state.health -= 1;

examples/collider.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,51 +82,51 @@ fn main() {
8282
game.run(Default::default());
8383
}
8484

85-
fn game_logic(engine_state: &mut EngineState, game_state: &mut GameState) {
86-
let sprite = engine_state.sprites.get_mut("sprite").unwrap();
85+
fn game_logic(engine: &mut Engine, game_state: &mut GameState) {
86+
let sprite = engine.sprites.get_mut("sprite").unwrap();
8787
// Zoom levels
88-
if engine_state.keyboard_state.just_pressed(KeyCode::Key1) {
88+
if engine.keyboard_state.just_pressed(KeyCode::Key1) {
8989
sprite.scale = 1.0;
9090
}
91-
if engine_state.keyboard_state.just_pressed(KeyCode::Key2) {
91+
if engine.keyboard_state.just_pressed(KeyCode::Key2) {
9292
sprite.scale = 2.0;
9393
}
94-
if engine_state.keyboard_state.just_pressed(KeyCode::Key3) {
94+
if engine.keyboard_state.just_pressed(KeyCode::Key3) {
9595
sprite.scale = 3.0;
9696
}
97-
if engine_state.keyboard_state.just_pressed(KeyCode::Key4) {
97+
if engine.keyboard_state.just_pressed(KeyCode::Key4) {
9898
sprite.scale = 4.0;
9999
}
100-
if engine_state.keyboard_state.just_pressed(KeyCode::Key5) {
100+
if engine.keyboard_state.just_pressed(KeyCode::Key5) {
101101
sprite.scale = 5.0;
102102
}
103-
if engine_state.keyboard_state.just_pressed(KeyCode::Key6) {
103+
if engine.keyboard_state.just_pressed(KeyCode::Key6) {
104104
sprite.scale = 6.0;
105105
}
106-
if engine_state.keyboard_state.just_pressed(KeyCode::Key7) {
106+
if engine.keyboard_state.just_pressed(KeyCode::Key7) {
107107
sprite.scale = 7.0;
108108
}
109-
if engine_state.keyboard_state.just_pressed(KeyCode::Key8) {
109+
if engine.keyboard_state.just_pressed(KeyCode::Key8) {
110110
sprite.scale = 8.0;
111111
}
112-
if engine_state.keyboard_state.just_pressed(KeyCode::Key9) {
112+
if engine.keyboard_state.just_pressed(KeyCode::Key9) {
113113
sprite.scale = 9.0;
114114
}
115115
// Rotate
116-
if engine_state.mouse_state.pressed(MouseButton::Right) {
117-
sprite.rotation += engine_state.delta_f32 * 6.0;
116+
if engine.mouse_state.pressed(MouseButton::Right) {
117+
sprite.rotation += engine.delta_f32 * 6.0;
118118
}
119119
// Delete collider
120-
if engine_state.keyboard_state.just_pressed(KeyCode::Delete)
121-
|| engine_state.keyboard_state.just_pressed(KeyCode::Back)
120+
if engine.keyboard_state.just_pressed(KeyCode::Delete)
121+
|| engine.keyboard_state.just_pressed(KeyCode::Back)
122122
{
123123
sprite.collider = Collider::NoCollider;
124124
sprite.collider_dirty = true;
125125
}
126126
// Modify a collider point
127-
if engine_state.mouse_state.just_pressed(MouseButton::Left) {
128-
if let Some(location) = engine_state.mouse_state.location() {
129-
if engine_state
127+
if engine.mouse_state.just_pressed(MouseButton::Left) {
128+
if let Some(location) = engine.mouse_state.location() {
129+
if engine
130130
.keyboard_state
131131
.pressed_any(&[KeyCode::RShift, KeyCode::LShift])
132132
{
@@ -137,20 +137,19 @@ fn game_logic(engine_state: &mut EngineState, game_state: &mut GameState) {
137137
}
138138
}
139139
// Generate a circle collider
140-
if engine_state.keyboard_state.just_pressed_any(&[
141-
KeyCode::Plus,
142-
KeyCode::Equals,
143-
KeyCode::NumpadAdd,
144-
]) {
140+
if engine
141+
.keyboard_state
142+
.just_pressed_any(&[KeyCode::Plus, KeyCode::Equals, KeyCode::NumpadAdd])
143+
{
145144
game_state.circle_radius += 0.5;
146145
}
147-
if engine_state
146+
if engine
148147
.keyboard_state
149148
.just_pressed_any(&[KeyCode::Minus, KeyCode::NumpadSubtract])
150149
{
151150
game_state.circle_radius -= 0.5;
152151
}
153-
if engine_state.keyboard_state.just_pressed_any(&[
152+
if engine.keyboard_state.just_pressed_any(&[
154153
KeyCode::Plus,
155154
KeyCode::Equals,
156155
KeyCode::NumpadAdd,
@@ -161,7 +160,7 @@ fn game_logic(engine_state: &mut EngineState, game_state: &mut GameState) {
161160
sprite.collider = Collider::circle(game_state.circle_radius);
162161
}
163162
// Let the user know whether or not their collider is currently convex
164-
let convex = engine_state.texts.get_mut("convex").unwrap();
163+
let convex = engine.texts.get_mut("convex").unwrap();
165164
const CONVEX_MESSAGE: &str = "Convex!";
166165
const NOT_CONVEX_MESSAGE: &str = "Not a convex polygon. :-(";
167166
if sprite.collider.is_convex() {
@@ -174,7 +173,7 @@ fn game_logic(engine_state: &mut EngineState, game_state: &mut GameState) {
174173
}
175174
}
176175
// Write the collider file
177-
if engine_state.keyboard_state.just_pressed(KeyCode::W) {
176+
if engine.keyboard_state.just_pressed(KeyCode::W) {
178177
if sprite.write_collider() {
179178
println!(
180179
"Successfully wrote the new collider file: {}",

examples/collision.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,47 @@ fn main() {
4141
game.run(());
4242
}
4343

44-
fn logic(engine_state: &mut EngineState, _: &mut ()) {
44+
fn logic(engine: &mut Engine, _: &mut ()) {
4545
// If a collision event happened last frame, print it out and play a sound
46-
for collision_event in engine_state.collision_events.drain(..) {
47-
let text = engine_state.texts.get_mut("collision text").unwrap();
46+
for collision_event in engine.collision_events.drain(..) {
47+
let text = engine.texts.get_mut("collision text").unwrap();
4848
match collision_event.state {
4949
CollisionState::Begin => {
5050
text.value = format!("{:?}", collision_event.pair);
51-
engine_state.audio_manager.play_sfx(SfxPreset::Switch1, 1.0)
51+
engine.audio_manager.play_sfx(SfxPreset::Switch1, 1.0)
5252
}
5353
CollisionState::End => {
5454
text.value = "".into();
55-
engine_state.audio_manager.play_sfx(SfxPreset::Switch2, 1.0)
55+
engine.audio_manager.play_sfx(SfxPreset::Switch2, 1.0)
5656
}
5757
}
5858
}
5959

60-
if let Some(sprite) = engine_state.sprites.get_mut("Player") {
60+
if let Some(sprite) = engine.sprites.get_mut("Player") {
6161
// Use the latest state of the mouse buttons to rotate the sprite
6262
let mut rotation_amount = 0.0;
63-
if engine_state.mouse_state.pressed(MouseButton::Left) {
64-
rotation_amount += ROTATION_SPEED * engine_state.delta_f32;
63+
if engine.mouse_state.pressed(MouseButton::Left) {
64+
rotation_amount += ROTATION_SPEED * engine.delta_f32;
6565
}
66-
if engine_state.mouse_state.pressed(MouseButton::Right) {
67-
rotation_amount -= ROTATION_SPEED * engine_state.delta_f32;
66+
if engine.mouse_state.pressed(MouseButton::Right) {
67+
rotation_amount -= ROTATION_SPEED * engine.delta_f32;
6868
}
6969
sprite.rotation += rotation_amount;
7070

7171
// Use the latest state of the mouse wheel to scale the sprite
72-
if let Some(location) = engine_state.mouse_state.location() {
72+
if let Some(location) = engine.mouse_state.location() {
7373
sprite.translation = location
7474
}
7575

7676
// Mousewheel scales the car
77-
for mouse_wheel in &engine_state.mouse_wheel_events {
77+
for mouse_wheel in &engine.mouse_wheel_events {
7878
sprite.scale *= 1.0 + (0.05 * mouse_wheel.y);
7979
sprite.scale = sprite.scale.clamp(0.1, 4.0);
8080
}
8181
}
8282

8383
// Pressing C toggles sprite collider debug lines
84-
if engine_state.keyboard_state.just_pressed(KeyCode::C) {
85-
engine_state.show_colliders = !engine_state.show_colliders;
84+
if engine.keyboard_state.just_pressed(KeyCode::C) {
85+
engine.show_colliders = !engine.show_colliders;
8686
}
8787
}

examples/game_state.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,18 @@ fn main() {
2828
game.run(initial_game_state);
2929
}
3030

31-
fn logic(engine_state: &mut EngineState, game_state: &mut MyCustomGameStateStuff) {
31+
fn logic(engine: &mut Engine, game_state: &mut MyCustomGameStateStuff) {
3232
// Get mutable references to the variables in the game state that we care about
33-
let race_car = engine_state.sprites.get_mut("Race Car").unwrap();
33+
let race_car = engine.sprites.get_mut("Race Car").unwrap();
3434

3535
// If we aren't turning, then tick the timer until it's time to start turning again
36-
if !game_state.turning
37-
&& game_state
38-
.change_timer
39-
.tick(engine_state.delta)
40-
.just_finished()
41-
{
36+
if !game_state.turning && game_state.change_timer.tick(engine.delta).just_finished() {
4237
game_state.turning = true;
4338
}
4439

4540
// Rotate the player
4641
if game_state.turning {
47-
race_car.rotation += engine_state.delta_f32 * 3.0;
42+
race_car.rotation += engine.delta_f32 * 3.0;
4843
// If the player rotated all the way around, reset direction, stop turning
4944
// TAU == (2 * PI), which is exactly one rotation in radians
5045
if race_car.rotation > TAU {

examples/keyboard_events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() {
2020
game.run(());
2121
}
2222

23-
fn logic(game_state: &mut EngineState, _: &mut ()) {
23+
fn logic(game_state: &mut Engine, _: &mut ()) {
2424
// Get the race car sprite
2525
let race_car = game_state.sprites.get_mut("Race Car").unwrap();
2626

examples/keyboard_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() {
2222
game.run(());
2323
}
2424

25-
fn logic(game_state: &mut EngineState, _: &mut ()) {
25+
fn logic(game_state: &mut Engine, _: &mut ()) {
2626
// Compute how fast we should move, rotate, and scale
2727
let move_amount = 200.0 * game_state.delta_f32;
2828
let rotation_amount = PI * game_state.delta_f32;

examples/level_creator.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ Z - Print out Rust code of current level
6666
game.run(game_state);
6767
}
6868

69-
fn logic(engine_state: &mut EngineState, game_state: &mut GameState) {
69+
fn logic(engine: &mut Engine, game_state: &mut GameState) {
7070
// Gather keyboard input
7171
let mut reset = false;
7272
let mut print_level = false;
7373
let mut print_status = false;
7474
let mut place_sprite = false;
7575
let mut prev_preset = false;
7676
let mut next_preset = false;
77-
for keyboard_event in &engine_state.keyboard_events {
77+
for keyboard_event in &engine.keyboard_events {
7878
if let KeyboardInput {
7979
scan_code: _,
8080
key_code: Some(key_code),
@@ -122,7 +122,7 @@ fn logic(engine_state: &mut EngineState, game_state: &mut GameState) {
122122
println!(
123123
"---------------\n\nuse rusty_engine::prelude::*;\n\nstruct GameState {{}}\n\nfn main() {{\n let mut game = Game::new();\n"
124124
);
125-
for sprite in engine_state.sprites.values() {
125+
for sprite in engine.sprites.values() {
126126
if sprite.label == game_state.current_label {
127127
continue;
128128
}
@@ -137,11 +137,11 @@ fn logic(engine_state: &mut EngineState, game_state: &mut GameState) {
137137
sprite.layer,
138138
);
139139
}
140-
println!("\n game.add_logic(logic);\n game.run(GameState {{}});\n}}\n\nfn logic(engine_state: &mut EngineState, game_state: &mut GameState) {{\n // Game Logic Goes Here\n}}")
140+
println!("\n game.add_logic(logic);\n game.run(GameState {{}});\n}}\n\nfn logic(engine: &mut Engine, game_state: &mut GameState) {{\n // Game Logic Goes Here\n}}")
141141
}
142142

143143
// Handle current sprite that has not yet been placed
144-
if let Some(sprite) = engine_state.sprites.get_mut(&game_state.current_label) {
144+
if let Some(sprite) = engine.sprites.get_mut(&game_state.current_label) {
145145
// Should we print out the status of the sprite?
146146
if print_status {
147147
println!(
@@ -160,11 +160,11 @@ fn logic(engine_state: &mut EngineState, game_state: &mut GameState) {
160160
}
161161

162162
// Handle translation via mouse location
163-
for cursor_moved in &engine_state.mouse_location_events {
163+
for cursor_moved in &engine.mouse_location_events {
164164
sprite.translation = cursor_moved.position;
165165
}
166166
// Handle rotation via mouse clicks
167-
for mouse_button_input in &engine_state.mouse_button_events {
167+
for mouse_button_input in &engine.mouse_button_events {
168168
if mouse_button_input.state != ElementState::Pressed {
169169
break;
170170
}
@@ -181,7 +181,7 @@ fn logic(engine_state: &mut EngineState, game_state: &mut GameState) {
181181
println!("r: {:.8}", sprite.rotation);
182182
}
183183
// Handle scale via mousewheel
184-
for mouse_wheel in &engine_state.mouse_wheel_events {
184+
for mouse_wheel in &engine.mouse_wheel_events {
185185
let scale_amount = if game_state.shift_pressed { 0.01 } else { 0.1 };
186186
if mouse_wheel.y > 0.0 || mouse_wheel.x < 0.0 {
187187
sprite.scale *= 1.0 + scale_amount;
@@ -196,7 +196,7 @@ fn logic(engine_state: &mut EngineState, game_state: &mut GameState) {
196196
// Change sprite to prev/next preset
197197
if prev_preset || next_preset {
198198
let old_sprite = {
199-
engine_state
199+
engine
200200
.sprites
201201
.get_mut(&game_state.current_label)
202202
.unwrap()
@@ -226,19 +226,15 @@ fn logic(engine_state: &mut EngineState, game_state: &mut GameState) {
226226
new_sprite.translation = old_sprite.translation;
227227
new_sprite.rotation = old_sprite.rotation;
228228
new_sprite.scale = old_sprite.scale;
229-
engine_state
230-
.sprites
231-
.insert(new_sprite.label.clone(), new_sprite);
232-
engine_state
233-
.sprites
234-
.remove::<str>(old_sprite.label.as_ref());
229+
engine.sprites.insert(new_sprite.label.clone(), new_sprite);
230+
engine.sprites.remove::<str>(old_sprite.label.as_ref());
235231
println!("{:?}", new_preset);
236232
}
237233

238234
// Place an sprite
239235
if place_sprite {
240236
let mut sprite = {
241-
engine_state
237+
engine
242238
.sprites
243239
.get_mut(&game_state.current_label)
244240
.unwrap()
@@ -248,6 +244,6 @@ fn logic(engine_state: &mut EngineState, game_state: &mut GameState) {
248244
game_state.next_layer += 0.01;
249245
sprite.label = game_state.next_sprite_num.to_string();
250246
game_state.next_sprite_num += 1;
251-
engine_state.sprites.insert(sprite.label.clone(), sprite);
247+
engine.sprites.insert(sprite.label.clone(), sprite);
252248
}
253249
}

0 commit comments

Comments
 (0)