Skip to content

Commit

Permalink
fixup: update bevy to use latest stable release (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovebaihezi authored Dec 1, 2024
1 parent 21c9618 commit 0218036
Show file tree
Hide file tree
Showing 12 changed files with 1,129 additions and 626 deletions.
1,391 changes: 1,025 additions & 366 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ resolver = "2"
members = ["crates/e2e", "crates/game"]

[workspace.dependencies]
bevy = { version = "0.14.2", features = ["wayland"] }
bevy = { version = "0.15.0", features = [
"wayland",
"bevy_remote",
"bevy_dev_tools",
"serialize",
] }
log = { version = "*", features = [
"max_level_debug",
"release_max_level_warn",
Expand Down
2 changes: 0 additions & 2 deletions crates/e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ edition = "2021"
build = "build.rs"

[dependencies]
bevy = { version = "*", default-features = false }
log = { workspace = true }
dinosaur-game = { path = "../game" }
6 changes: 1 addition & 5 deletions crates/e2e/tests/game_control.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use bevy::prelude::*;

// use dinosaur_game::GameStatus;

#[test]
fn game_time_pause_as_no_focus() {
let _app = App::new();
//let _app = App::new();

//app.add_plugins((
// DefaultPlugins.set(ScheduleRunnerPlugin::run_once()),
Expand Down
4 changes: 2 additions & 2 deletions crates/game/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::{Camera2dBundle, Commands};
use bevy::prelude::{Camera2d, Commands};

pub fn setup_camera(mut commands: Commands) {
// Spawn the camera
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);
}
1 change: 0 additions & 1 deletion crates/game/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ impl Dino {

#[derive(Component)]
pub enum GameControl {
FPS,
Score,
}

Expand Down
13 changes: 5 additions & 8 deletions crates/game/src/dino.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{
prelude::{
default, Commands, EventReader, KeyCode, MouseButton, Query, Res, Touches, Transform, With,
},
sprite::{Sprite, SpriteBundle},
sprite::Sprite,
time::{Time, Virtual},
window::WindowResized,
};
Expand All @@ -14,15 +14,12 @@ use crate::components::{Dino, DINO_SIZE, DINO_WIDTH, JUMP_HIGH};

pub fn setup_dino(mut commands: Commands) {
commands.spawn((
SpriteBundle {
sprite: Sprite {
color: Color::srgb(0.05, 0.05, 0.05),
custom_size: Some(DINO_SIZE),
..default()
},
transform: Transform::from_translation(Vec3::new(0.0, DINO_WIDTH / 2.0 / 0.618, 0.0)),
Sprite {
color: Color::srgb(0.05, 0.05, 0.05),
custom_size: Some(DINO_SIZE),
..default()
},
Transform::from_translation(Vec3::new(0.0, DINO_WIDTH / 2.0 / 0.618, 0.0)),
Dino::default(),
));
}
Expand Down
172 changes: 9 additions & 163 deletions crates/game/src/game_control.rs
Original file line number Diff line number Diff line change
@@ -1,154 +1,24 @@
use bevy::{
color::Color,
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
dev_tools::fps_overlay::FpsOverlayConfig,
input::ButtonInput,
prelude::{
BuildChildren, Commands, KeyCode, MouseButton, NodeBundle, Query, Res, ResMut, TextBundle,
Touches,
},
text::{Text, TextStyle},
prelude::{Commands, KeyCode, MouseButton, Query, Res, ResMut, Touches},
time::{Time, Virtual},
ui::Style,
utils::default,
window::Window,
};

use crate::{
components::{Dino, GameControl},
GameStatus,
};

fn base_node() -> NodeBundle {
NodeBundle {
style: Style {
display: bevy::ui::Display::Flex,
width: bevy::ui::Val::Vw(100.0),
height: bevy::ui::Val::Vh(100.0),
align_items: bevy::ui::AlignItems::Center,
justify_content: bevy::ui::JustifyContent::SpaceBetween,
flex_direction: bevy::ui::FlexDirection::Column,
..default()
},
..default()
}
}

fn game_info_bundle() -> TextBundle {
const GAME_VERSION: &str = concat!(
"game_version: ",
env!("CARGO_PKG_VERSION"),
"-",
env!("GIT_HASH")
);

const BUILD_DATE: &str = concat!("build on ", env!("BUILD_DATE"));

let game_info = format!("{}\n{}", BUILD_DATE, GAME_VERSION);

TextBundle {
style: Style {
align_self: bevy::ui::AlignSelf::Center,
..default()
},
text: Text::from_section(
game_info,
TextStyle {
color: Color::srgba(0.0, 0.0, 0.0, 1.0),
font_size: 12.0,
..default()
},
),
..default()
}
}
use crate::{components::Dino, GameStatus};

fn branch_boundle() -> TextBundle {
let branch = env!("GIT_BRANCH");
TextBundle {
style: Style {
align_self: bevy::ui::AlignSelf::Center,
..default()
},
text: Text::from_section(
branch,
TextStyle {
color: Color::srgba(0.0, 0.0, 0.0, 1.0),
font_size: 12.0,
..default()
},
),
..default()
}
}

fn fps_bundle() -> (TextBundle, GameControl) {
(
TextBundle {
style: Style {
align_self: bevy::ui::AlignSelf::Center,
..default()
},
text: Text::from_section(
"ERROR",
TextStyle {
color: Color::srgba(0.0, 0.0, 0.0, 0.96),
..default()
},
),
..default()
},
GameControl::FPS,
)
}

fn score_bundle() -> (TextBundle, GameControl) {
(
TextBundle {
style: Style {
align_self: bevy::ui::AlignSelf::Center,
..default()
},
text: Text::from_section(
"ERROR",
TextStyle {
color: Color::srgba(0.0, 0.0, 0.0, 0.96),
..default()
},
),
..default()
},
GameControl::Score,
)
pub fn setup_game_control(commands: Commands, mut time: ResMut<Time<Virtual>>) {
time.pause();
_ = commands;
}

fn banner() -> NodeBundle {
NodeBundle {
style: Style {
width: bevy::ui::Val::Vw(100.0),
height: bevy::ui::Val::Auto,
align_items: bevy::ui::AlignItems::Center,
justify_content: bevy::ui::JustifyContent::SpaceAround,
display: bevy::ui::Display::Flex,
..default()
},
..default()
pub fn show_fps_overlay(input: Res<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOverlayConfig>) {
if input.just_pressed(KeyCode::F1) {
overlay.enabled = !overlay.enabled;
}
}

pub fn setup_game_control(mut commands: Commands, mut time: ResMut<Time<Virtual>>) {
time.pause();
commands.spawn(base_node()).with_children(|parent| {
parent.spawn(banner()).with_children(|parent| {
parent.spawn(fps_bundle());
parent.spawn(score_bundle());
});
parent.spawn(banner()).with_children(|parent| {
parent.spawn(game_info_bundle());
parent.spawn(branch_boundle());
});
});
}

pub fn user_control(
mut time: ResMut<Time<Virtual>>,
mut dino_query: Query<&mut Dino>,
Expand All @@ -174,9 +44,7 @@ pub fn user_control(
}

pub fn game_info(
mut text_query: Query<(&mut Text, &GameControl)>,
dino_query: Query<&Dino>,
diagnostics: Res<DiagnosticsStore>,
mut status: ResMut<GameStatus>,
time: Res<Time<Virtual>>,
) {
Expand All @@ -187,27 +55,5 @@ pub fn game_info(
if dino.is_over() {
status.score = 0;
}
for (mut text, game_control) in text_query.iter_mut() {
match game_control {
GameControl::Score => {
let value: u64 = status.score >> 3;
text.sections[0].value = format!("{value:012}");
}
GameControl::FPS => {
let (fps, avg, smoothed) = diagnostics
.get(&FrameTimeDiagnosticsPlugin::FPS)
.map(|x| {
(
x.value().unwrap_or_default(),
x.average().unwrap_or_default(),
x.smoothed().unwrap_or_default(),
)
})
.unwrap_or_default();
let fps_info = format!("{fps:.0}|{avg:.0}|{smoothed:.0}");
text.sections[0].value = fps_info;
}
}
}
}
}
13 changes: 5 additions & 8 deletions crates/game/src/ground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::{
color::Color,
math::{Vec2, Vec3},
prelude::{default, Commands, EventReader, Query, Transform, With},
sprite::{Sprite, SpriteBundle},
sprite::Sprite,
window::{Window, WindowResized},
};

Expand All @@ -16,15 +16,12 @@ pub fn setup_ground(mut commands: Commands, window: Query<&Window>) {
let window_width = window.width();

commands.spawn((
SpriteBundle {
sprite: Sprite {
color: Color::srgba(0.0, 0.0, 0.0, 0.95),
custom_size: Some(Vec2::new(window_width * 0.8, 1.0)),
..default()
},
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
Sprite {
color: Color::srgba(0.0, 0.0, 0.0, 0.95),
custom_size: Some(Vec2::new(window_width * 0.8, 1.0)),
..default()
},
Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
Ground,
));
}
Expand Down
25 changes: 23 additions & 2 deletions crates/game/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use bevy::{diagnostic::FrameTimeDiagnosticsPlugin, prelude::*};
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},
Expand All @@ -18,7 +22,24 @@ fn main() {
..Default::default()
});
let exit = App::new()
.add_plugins((default_plugin, FrameTimeDiagnosticsPlugin))
.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(ClearColor(Color::srgb(1.0, 1.0, 1.0)))
.add_systems(
Expand Down
23 changes: 10 additions & 13 deletions crates/game/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::{
color::Color,
math::{Vec2, Vec3},
prelude::{default, Commands, Query, Res, ResMut, Transform},
sprite::{Sprite, SpriteBundle},
sprite::Sprite,
time::{Time, Virtual},
window::Window,
};
Expand All @@ -17,19 +17,16 @@ pub fn setup_tree(mut commands: Commands, window: Query<&Window>) {
let window_width = window.width();

commands.spawn((
SpriteBundle {
sprite: Sprite {
color: Color::srgb(0.35, 0.35, 0.35),
custom_size: Some(Vec2::new(TREE_WIDTH, TREE_WIDTH / 0.618)),
..default()
},
transform: Transform::from_translation(Vec3::new(
window_width - TREE_WIDTH,
TREE_WIDTH / 2.0 / 0.618,
0.0,
)),
Sprite {
color: Color::srgb(0.35, 0.35, 0.35),
custom_size: Some(Vec2::new(TREE_WIDTH, TREE_WIDTH / 0.618)),
..default()
},
Transform::from_translation(Vec3::new(
window_width - TREE_WIDTH,
TREE_WIDTH / 2.0 / 0.618,
0.0,
)),
Tree::default(),
));
}
Expand All @@ -52,7 +49,7 @@ pub fn tree_move_animation(
} else {
let more_hard_speed = (status.speed as f32).log10();
transform.translation.x
- time.delta_seconds() * (window_width / 3.0 + (TREE_WIDTH / 2.0) * more_hard_speed)
- time.delta_secs() * (window_width / 3.0 + (TREE_WIDTH / 2.0) * more_hard_speed)
};
}
}
Loading

0 comments on commit 0218036

Please sign in to comment.