From 79f115c6ef1c4febb09b4d46c3ae88e32b0c9e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 17 Feb 2025 16:10:36 +0300 Subject: [PATCH] perf: :zap: fish counter following performance improved --- src/lib.rs | 9 +++++---- src/ui.rs | 16 +++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1f3c081..c44d013 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,9 @@ use bevy::{ app::{App, PluginGroup, Startup, Update}, diagnostic::FrameTimeDiagnosticsPlugin, + prelude::IntoSystemConfigs, utils::default, - window::{PresentMode, Window, WindowPlugin, WindowTheme}, + window::{Window, WindowPlugin, WindowTheme}, DefaultPlugins, }; use camera::UserCamera; @@ -15,7 +16,7 @@ mod player; mod ui; mod water; -pub const CHARACTER_SPEED: f32 = 300.0; +pub const CHARACTER_SPEED: f32 = 1000.0; pub const CAMERA_DECAY_RATE: f32 = 3.0; pub const FISHING_AREA_RADIUS: f32 = 100.0; @@ -24,7 +25,7 @@ pub fn start_game() { .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { title: "Fisher ~Tahinli".to_owned(), - present_mode: PresentMode::AutoVsync, + // present_mode: PresentMode::Immediate, //Vsync off window_theme: Some(WindowTheme::Dark), ..default() }), @@ -47,7 +48,7 @@ pub fn start_game() { ( FPSText::update, UserCamera::update, - Player::r#move, + Player::r#move.before(FishCounterText::update), Water::fishing, FishCounterText::update, ), diff --git a/src/ui.rs b/src/ui.rs index 547a0de..aa1350c 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,3 +1,5 @@ +use std::sync::{Arc, LazyLock, RwLock}; + use bevy::{ diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}, prelude::*, @@ -5,6 +7,9 @@ use bevy::{ use crate::player::Player; +const FISH_COUNTER_OFFSET: Vec3 = Vec3::new(50.0, 50.0, 1.0); +static FISH_COUNT: LazyLock>> = LazyLock::new(|| Arc::new(RwLock::new(0))); + #[derive(Debug, Component)] pub struct FPSText; @@ -63,12 +68,13 @@ impl FishCounterText { let player = player_query.get_single().unwrap(); let player_position = player_transform_query.get_single().unwrap(); - let mut new_fish_counter_text_position = player_position.translation.truncate().extend(1.0); - new_fish_counter_text_position.x += 50.0; - new_fish_counter_text_position.y += 50.0; - + let mut new_fish_counter_text_position = player_position.translation.truncate().extend(0.0); + new_fish_counter_text_position += FISH_COUNTER_OFFSET; fish_counter_text_transform.translation = new_fish_counter_text_position; - *fish_counter_text = format!("Fish Count = {}", player.fish_count).into(); + if *FISH_COUNT.read().unwrap() != player.fish_count { + *FISH_COUNT.write().unwrap() = player.fish_count; + *fish_counter_text = format!("Fish Count = {}", player.fish_count).into(); + } } }