Skip to content

Commit

Permalink
feat: speed up trees (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovebaihezi authored Nov 2, 2024
1 parent b90dd0c commit acf646e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
17 changes: 2 additions & 15 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
17 changes: 10 additions & 7 deletions src/game_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -13,7 +13,10 @@ use bevy::{
window::Window,
};

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

fn base_node() -> NodeBundle {
NodeBundle {
Expand Down Expand Up @@ -174,20 +177,20 @@ pub fn game_info(
mut text_query: Query<(&mut Text, &GameControl)>,
dino_query: Query<&Dino>,
diagnostics: Res<DiagnosticsStore>,
mut score: Local<u64>,
mut status: ResMut<GameStatus>,
time: Res<Time<Virtual>>,
) {
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 => {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use bevy::prelude::*;

#[derive(Debug, Default, Resource)]
pub struct GameStatus {
pub score: u64,
pub speed: u64,
}
16 changes: 10 additions & 6 deletions src/tree.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -35,20 +38,21 @@ pub fn tree_move_animation(
mut tree_query: Query<(&mut Transform, &mut Tree)>,
time: Res<Time<Virtual>>,
window: Query<&Window>,
mut status: ResMut<GameStatus>,
) {
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)
};
}
}

0 comments on commit acf646e

Please sign in to comment.