From acf646ef2e05d9fa87a58c10fe86a0b250a7ddd6 Mon Sep 17 00:00:00 2001 From: Lqxc <55340837+lovebaihezi@users.noreply.github.com> Date: Sat, 2 Nov 2024 06:27:50 -0400 Subject: [PATCH] feat: speed up trees (#35) --- src/components.rs | 17 ++--------------- src/game_control.rs | 17 ++++++++++------- src/lib.rs | 2 ++ src/main.rs | 3 ++- src/resources.rs | 7 +++++++ src/tree.rs | 16 ++++++++++------ 6 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 src/resources.rs diff --git a/src/components.rs b/src/components.rs index 13f38cb..9840962 100644 --- a/src/components.rs +++ b/src/components.rs @@ -56,22 +56,9 @@ pub enum GameControl { pub struct Ground; #[derive(Component, Default)] -pub struct Tree { - succeeded: usize, -} +pub struct Tree {} impl Tree { - pub fn dino_passed(&mut self) { - self.succeeded += 1; - } - pub fn over(&mut self) { - self.succeeded = 0; - } - pub fn ready(&mut self) { - self.succeeded = 0; - } + pub fn ready(&mut self) {} pub fn start(&mut self) {} - pub fn speed(&mut self) -> f64 { - (self.succeeded as f64 + 2.2).ln() - } } diff --git a/src/game_control.rs b/src/game_control.rs index e7c5d93..77e4873 100644 --- a/src/game_control.rs +++ b/src/game_control.rs @@ -3,8 +3,8 @@ use bevy::{ diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}, input::ButtonInput, prelude::{ - BuildChildren, Commands, KeyCode, Local, MouseButton, NodeBundle, Query, Res, ResMut, - TextBundle, Touches, + BuildChildren, Commands, KeyCode, MouseButton, NodeBundle, Query, Res, ResMut, TextBundle, + Touches, }, text::{Text, TextStyle}, time::{Time, Virtual}, @@ -13,7 +13,10 @@ use bevy::{ window::Window, }; -use crate::components::{Dino, GameControl}; +use crate::{ + components::{Dino, GameControl}, + GameStatus, +}; fn base_node() -> NodeBundle { NodeBundle { @@ -174,20 +177,20 @@ pub fn game_info( mut text_query: Query<(&mut Text, &GameControl)>, dino_query: Query<&Dino>, diagnostics: Res, - mut score: Local, + mut status: ResMut, time: Res>, ) { if !time.is_paused() { - *score += 1; + status.score += 1; } for dino in dino_query.iter() { if dino.is_over() { - *score = 0; + status.score = 0; } for (mut text, game_control) in text_query.iter_mut() { match game_control { GameControl::Score => { - let value: u64 = *score >> 3; + let value: u64 = status.score >> 3; text.sections[0].value = format!("{value:012}"); } GameControl::FPS => { diff --git a/src/lib.rs b/src/lib.rs index 601791a..3ff894b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,10 +4,12 @@ mod dino; mod game_control; pub mod game_logic; mod ground; +mod resources; mod tree; pub use camera::setup_camera; pub use dino::*; pub use game_control::*; pub use ground::*; +pub use resources::*; pub use tree::*; diff --git a/src/main.rs b/src/main.rs index a788e38..0f53ade 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,12 +3,13 @@ use dinosaur::{ 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, + update_ground, user_control, GameStatus, }; fn main() { let exit = App::new() .add_plugins((DefaultPlugins, FrameTimeDiagnosticsPlugin)) + .insert_resource(GameStatus { speed: 5, score: 0 }) .insert_resource(ClearColor(Color::srgb(1.0, 1.0, 1.0))) .add_systems( Startup, diff --git a/src/resources.rs b/src/resources.rs new file mode 100644 index 0000000..4924123 --- /dev/null +++ b/src/resources.rs @@ -0,0 +1,7 @@ +use bevy::prelude::*; + +#[derive(Debug, Default, Resource)] +pub struct GameStatus { + pub score: u64, + pub speed: u64, +} diff --git a/src/tree.rs b/src/tree.rs index 7ba7c4d..4d0700e 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -1,13 +1,16 @@ use bevy::{ color::Color, math::{Vec2, Vec3}, - prelude::{default, Commands, Query, Res, Transform}, + prelude::{default, Commands, Query, Res, ResMut, Transform}, sprite::{Sprite, SpriteBundle}, time::{Time, Virtual}, window::Window, }; -use crate::components::{Tree, TREE_WIDTH}; +use crate::{ + components::{Tree, TREE_WIDTH}, + GameStatus, +}; pub fn setup_tree(mut commands: Commands, window: Query<&Window>) { let window = window.single(); @@ -35,20 +38,21 @@ pub fn tree_move_animation( mut tree_query: Query<(&mut Transform, &mut Tree)>, time: Res>, window: Query<&Window>, + mut status: ResMut, ) { if time.is_paused() { return; } let window = window.single(); let window_width = window.width(); - for (mut transform, mut tree) in tree_query.iter_mut() { + for (mut transform, _) in tree_query.iter_mut() { transform.translation.x = if transform.translation.x < -window_width * 0.8 / 2.0 { - tree.dino_passed(); + status.speed += 2; window_width * 0.8 / 2.0 } else { + let more_hard_speed = (status.speed as f32).log10(); transform.translation.x - - time.delta_seconds() - * (window_width / 3.0 + (TREE_WIDTH / 2.0) * tree.speed() as f32) + - time.delta_seconds() * (window_width / 3.0 + (TREE_WIDTH / 2.0) * more_hard_speed) }; } }