Skip to content

Commit b30bde5

Browse files
committed
fixup: copy from bevy examples
1 parent 0218036 commit b30bde5

File tree

9 files changed

+629
-65
lines changed

9 files changed

+629
-65
lines changed

Cargo.lock

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ log = { version = "*", features = [
1313
"max_level_debug",
1414
"release_max_level_warn",
1515
] }
16+
flume = { version = "0.11.1" }
1617

1718
[profile.dev]
1819
opt-level = 1

crates/e2e/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
[package]
32
name = "dinosaur-e2e"
43
version = "0.0.1"

crates/e2e/tests/game_control.rs renamed to crates/e2e/tests/time_paused_on_start.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use dinosaur_game::app::{AppType, Game};
12
#[test]
23
fn game_time_pause_as_no_focus() {
4+
let test_game = Game::init(AppType::TestNoRender);
35
//let _app = App::new();
46

57
//app.add_plugins((

crates/game/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ build = "build.rs"
77
[dependencies]
88
bevy = { workspace = true }
99
log = { workspace = true }
10+
flume = { workspace = true }

crates/game/src/app.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use crate::{
2+
dino_jump_animation, dino_jump_system, dino_pos_fix_system, game_info,
3+
game_logic::{dino_touched_tree, reset_game},
4+
setup_camera, setup_dino, setup_game_control, setup_ground, setup_tree,
5+
test_functions::{CaptureFramePlugin, ImageCopyPlugin},
6+
tree_move_animation, update_ground, user_control, GameStatus,
7+
};
8+
use bevy::{
9+
app::PluginGroupBuilder,
10+
dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin},
11+
prelude::*,
12+
text::FontSmoothing,
13+
winit::WinitPlugin,
14+
};
15+
16+
pub struct Game {
17+
app: App,
18+
}
19+
20+
/// # AppType: Control App init, plugins and systems
21+
#[derive(Debug, Clone, Copy)]
22+
pub enum AppType {
23+
Normal,
24+
TestNoRender,
25+
}
26+
27+
fn default_plugins(app_type: AppType) -> PluginGroupBuilder {
28+
let primary_window = match app_type {
29+
AppType::Normal => Some(Window {
30+
title: "Dinosaur Game".to_string(),
31+
canvas: Some("#game".to_string()),
32+
fit_canvas_to_parent: true,
33+
..Default::default()
34+
}),
35+
AppType::TestNoRender => None,
36+
};
37+
let plugin = DefaultPlugins.set(WindowPlugin {
38+
primary_window,
39+
..Default::default()
40+
});
41+
match app_type {
42+
AppType::TestNoRender => plugin.disable::<WinitPlugin>(),
43+
AppType::Normal => plugin,
44+
}
45+
}
46+
47+
fn fps_plugin() -> FpsOverlayPlugin {
48+
FpsOverlayPlugin {
49+
config: FpsOverlayConfig {
50+
text_config: TextFont {
51+
font_size: 16.0,
52+
font: default(),
53+
font_smoothing: FontSmoothing::default(),
54+
},
55+
// We can also change color of the overlay
56+
text_color: Color::linear_rgba(0.0, 1.0, 0.0, 1.0),
57+
enabled: true,
58+
},
59+
}
60+
}
61+
62+
impl Game {
63+
pub fn init(app_type: AppType) -> Self {
64+
let mut game = Game { app: App::new() };
65+
game.app
66+
.add_plugins((default_plugins(app_type), fps_plugin()))
67+
.insert_resource(GameStatus { speed: 5, score: 0 })
68+
.insert_resource(ClearColor(Color::srgb(1.0, 1.0, 1.0)))
69+
.add_systems(
70+
Startup,
71+
(
72+
setup_ground,
73+
setup_dino,
74+
setup_camera,
75+
setup_tree,
76+
setup_game_control,
77+
),
78+
)
79+
.add_systems(
80+
Update,
81+
(
82+
update_ground,
83+
dino_jump_system,
84+
(user_control, game_info).chain(),
85+
(dino_pos_fix_system, dino_jump_animation).chain(),
86+
tree_move_animation,
87+
(dino_touched_tree, reset_game).chain(),
88+
),
89+
);
90+
match app_type {
91+
AppType::Normal => {}
92+
AppType::TestNoRender => {
93+
game.app
94+
.add_plugins(ImageCopyPlugin)
95+
.add_plugins(CaptureFramePlugin);
96+
}
97+
};
98+
game
99+
}
100+
101+
pub fn run(mut self) -> AppExit {
102+
self.app.run()
103+
}
104+
}

crates/game/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
pub mod app;
12
mod camera;
23
pub mod components;
34
mod dino;
45
mod game_control;
56
pub mod game_logic;
67
mod ground;
78
mod resources;
9+
pub mod test_functions;
810
mod tree;
911

1012
pub use camera::setup_camera;

crates/game/src/main.rs

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,9 @@
1-
use bevy::{
2-
dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin},
3-
prelude::*,
4-
text::FontSmoothing,
5-
};
6-
use dinosaur_game::{
7-
dino_jump_animation, dino_jump_system, dino_pos_fix_system, game_info,
8-
game_logic::{dino_touched_tree, reset_game},
9-
setup_camera, setup_dino, setup_game_control, setup_ground, setup_tree, tree_move_animation,
10-
update_ground, user_control, GameStatus,
11-
};
1+
use bevy::app::AppExit;
2+
use dinosaur_game::app::{AppType, Game};
123

134
fn main() {
14-
// Using OpenGL backend
15-
let default_plugin = DefaultPlugins.set(WindowPlugin {
16-
primary_window: Some(Window {
17-
title: "Dinosaur Game".to_string(),
18-
canvas: Some("#game".to_string()),
19-
fit_canvas_to_parent: true,
20-
..Default::default()
21-
}),
22-
..Default::default()
23-
});
24-
let exit = App::new()
25-
.add_plugins((
26-
default_plugin,
27-
FpsOverlayPlugin {
28-
config: FpsOverlayConfig {
29-
text_config: TextFont {
30-
// Here we define size of our overlay
31-
font_size: 16.0,
32-
// If we want, we can use a custom font
33-
font: default(),
34-
// We could also disable font smoothing,
35-
font_smoothing: FontSmoothing::default(),
36-
},
37-
// We can also change color of the overlay
38-
text_color: Color::linear_rgba(0.0, 1.0, 0.0, 1.0),
39-
enabled: true,
40-
},
41-
},
42-
))
43-
.insert_resource(GameStatus { speed: 5, score: 0 })
44-
.insert_resource(ClearColor(Color::srgb(1.0, 1.0, 1.0)))
45-
.add_systems(
46-
Startup,
47-
(
48-
setup_ground,
49-
setup_dino,
50-
setup_camera,
51-
setup_tree,
52-
setup_game_control,
53-
),
54-
)
55-
.add_systems(
56-
Update,
57-
(
58-
update_ground,
59-
dino_jump_system,
60-
(user_control, game_info).chain(),
61-
(dino_pos_fix_system, dino_jump_animation).chain(),
62-
tree_move_animation,
63-
(dino_touched_tree, reset_game).chain(),
64-
),
65-
)
66-
.run();
5+
let game = Game::init(AppType::Normal);
6+
let exit = game.run();
677
match exit {
688
AppExit::Success => {}
699
AppExit::Error(_) => panic!("An error occurred while running the app"),

0 commit comments

Comments
 (0)