Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add integration tests 1/N #49

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .env.example
Empty file.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ wasm/
/.zed

.vscode\launch.json

.env
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ log = { version = "*", features = [
"release_max_level_warn",
] }
ruzstd = { version = "^0.7.3" }
flume = { version = "0.11.1" }
dotenv = "0.15.0"

[profile.dev]
opt-level = 1
Expand Down
1 change: 0 additions & 1 deletion crates/e2e/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[package]
name = "dinosaur-e2e"
version = "0.0.1"
Expand Down
50 changes: 0 additions & 50 deletions crates/e2e/tests/game_control.rs

This file was deleted.

6 changes: 6 additions & 0 deletions crates/e2e/tests/render_to_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[test]
fn render_game_to_image() {
// let test_game = Game::init(AppType::RenderToImageTesting);
// let exit = test_game.run();
// assert!(exit.is_success());
}
2 changes: 2 additions & 0 deletions crates/game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ build = "build.rs"
[dependencies]
bevy = { workspace = true }
log = { workspace = true }
flume = { workspace = true }
dotenv = { workspace = true }
120 changes: 120 additions & 0 deletions crates/game/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use std::time::Duration;

use crate::{
dino_jump_animation, dino_jump_system, dino_pos_fix_system, game_info,
game_logic::{dino_touched_tree, reset_game},
normal_app_setup, setup_dino, setup_game_control, setup_ground, setup_tree,
test_functions::{render_to_image_setup, CaptureFramePlugin, ImageCopyPlugin, SceneController},
tree_move_animation, update_ground, user_control, GameStatus, SpeedControlInfo,
};
use bevy::{
app::{PluginGroupBuilder, ScheduleRunnerPlugin},
dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin},
prelude::*,
text::FontSmoothing,
winit::WinitPlugin,
};

pub struct Game {
app: App,
}

/// # AppType: Control App init, plugins and systems
#[derive(Debug, Clone, Copy)]
pub enum AppType {
Normal,
RenderToImageTesting,
}

fn default_plugins(app_type: AppType) -> PluginGroupBuilder {
let primary_window = match app_type {
AppType::Normal => Some(Window {
title: "Dinosaur Game".to_string(),
canvas: Some("#game".to_string()),
fit_canvas_to_parent: true,
..Default::default()
}),
AppType::RenderToImageTesting => None,
};
let plugin = DefaultPlugins.set(WindowPlugin {
primary_window,
..Default::default()
});
match app_type {
AppType::RenderToImageTesting => plugin
.disable::<WinitPlugin>()
.set(ImagePlugin::default_nearest()),
AppType::Normal => plugin,
}
}

fn fps_plugin() -> FpsOverlayPlugin {
FpsOverlayPlugin {
config: FpsOverlayConfig {
text_config: TextFont {
font_size: 16.0,
font: default(),
font_smoothing: FontSmoothing::default(),
},
// We can also change color of the overlay
text_color: Color::linear_rgba(0.0, 1.0, 0.0, 1.0),
enabled: true,
},
}
}

impl Game {
pub fn init(app_type: AppType) -> Self {
let mut game = Game { app: App::new() };
game.app
.add_plugins((default_plugins(app_type), fps_plugin()))
.insert_resource(GameStatus { speed: 5, score: 0 })
.insert_resource(ClearColor(Color::srgb(1.0, 1.0, 1.0)))
.insert_resource(SpeedControlInfo {
speed_increment: 100,
max_game_speed: u64::MAX,
})
.add_systems(
Startup,
(
setup_ground,
setup_dino,
normal_app_setup,
setup_tree,
setup_game_control,
),
)
.add_systems(
Update,
(
update_ground,
dino_jump_system,
(user_control, game_info).chain(),
(dino_pos_fix_system, dino_jump_animation).chain(),
tree_move_animation,
(dino_touched_tree, reset_game).chain(),
),
);
match app_type {
AppType::Normal => {
game.app.add_systems(Startup, normal_app_setup);
}
AppType::RenderToImageTesting => {
game.app
.add_systems(Startup, render_to_image_setup)
.add_plugins(ImageCopyPlugin)
.add_plugins(CaptureFramePlugin)
.add_plugins(ScheduleRunnerPlugin::run_loop(
// Run 60 times per second.
Duration::from_secs_f64(1.0 / 60.0),
))
.init_resource::<SceneController>();
}
};
game
}

pub fn run(mut self) -> AppExit {
self.app.run()
}
}
2 changes: 1 addition & 1 deletion crates/game/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::{Camera2d, Commands};

pub fn setup_camera(mut commands: Commands) {
pub fn normal_app_setup(mut commands: Commands) {
// Spawn the camera
commands.spawn(Camera2d);
}
4 changes: 3 additions & 1 deletion crates/game/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
pub mod app;
mod camera;
pub mod components;
mod dino;
mod game_control;
pub mod game_logic;
mod ground;
mod resources;
pub mod test_functions;
mod tree;

pub use camera::setup_camera;
pub use camera::normal_app_setup;
pub use dino::*;
pub use game_control::*;
pub use ground::*;
Expand Down
72 changes: 4 additions & 68 deletions crates/game/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,9 @@
use bevy::{
dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin},
prelude::*,
text::FontSmoothing,
};
use dinosaur_game::{
dino_jump_animation, dino_jump_system, dino_pos_fix_system, game_info,
game_logic::{dino_touched_tree, reset_game},
setup_camera, setup_dino, setup_game_control, setup_ground, setup_tree, tree_move_animation,
update_ground, user_control, GameStatus, SpeedControlInfo,
};
use bevy::app::AppExit;
use dinosaur_game::app::{AppType, Game};

fn main() {
// Using OpenGL backend
let default_plugin = DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Dinosaur Game".to_string(),
canvas: Some("#game".to_string()),
fit_canvas_to_parent: true,
..Default::default()
}),
..Default::default()
});
let exit = App::new()
.add_plugins((
default_plugin,
FpsOverlayPlugin {
config: FpsOverlayConfig {
text_config: TextFont {
// Here we define size of our overlay
font_size: 16.0,
// If we want, we can use a custom font
font: default(),
// We could also disable font smoothing,
font_smoothing: FontSmoothing::default(),
},
// We can also change color of the overlay
text_color: Color::linear_rgba(0.0, 1.0, 0.0, 1.0),
enabled: true,
},
},
))
.insert_resource(GameStatus { speed: 5, score: 0 })
.insert_resource(SpeedControlInfo {
speed_increment: 100,
max_game_speed: u64::MAX,
})
.insert_resource(ClearColor(Color::srgb(1.0, 1.0, 1.0)))
.add_systems(
Startup,
(
setup_ground,
setup_dino,
setup_camera,
setup_tree,
setup_game_control,
),
)
.add_systems(
Update,
(
update_ground,
dino_jump_system,
(user_control, game_info).chain(),
(dino_pos_fix_system, dino_jump_animation).chain(),
tree_move_animation,
(dino_touched_tree, reset_game).chain(),
),
)
.run();
let game = Game::init(AppType::Normal);
let exit = game.run();
match exit {
AppExit::Success => {}
AppExit::Error(_) => panic!("An error occurred while running the app"),
Expand Down
Loading
Loading