From be55433376f01f5027d23c064a474512d1d71f5a Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 2 Apr 2024 05:59:33 +0200 Subject: [PATCH] update asteroids only when changing cell --- src/world.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/world.rs b/src/world.rs index b0063cb..7143caa 100644 --- a/src/world.rs +++ b/src/world.rs @@ -8,7 +8,7 @@ use std::collections::HashMap; use std::f32::consts::PI; use fastrand; -const ASTEROID_UPDATE_INTERVAL: f32 = 1.0; // seconds +const ASTEROID_UPDATE_INTERVAL: f32 = 0.1; // seconds const ASTEROID_SIZE_FACTOR: f32 = 3.0; const STARS_MAX_MAGNITUDE: f32 = 5.5; @@ -182,9 +182,11 @@ fn spawn_despawn_asteroids( mut db: ResMut, asset_server: Res, mut q_asteroid: Query<(Entity, &mut Visibility), With>, + mut last_player_cell: Local, mut log: ResMut, ) { if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() { + //if q_player.is_empty() { return; } let player = q_player.get_single().unwrap(); @@ -193,6 +195,11 @@ fn spawn_despawn_asteroids( (player.y / ASTEROID_SPAWN_STEP).round() as i64, (player.z / ASTEROID_SPAWN_STEP).round() as i64, ); + if *last_player_cell == player_cell { + return; + } + *last_player_cell = player_cell; + let mut spawned = 0; let mut spawned_near = 0; let mut despawned = 0; @@ -202,6 +209,7 @@ fn spawn_despawn_asteroids( let step: f64 = ASTEROID_SPAWN_STEP; let stepmax: i64 = (view_radius / step) as i64; + // Despawn far asteroids let x_min = player_cell.x - stepmax; let x_max = player_cell.x + stepmax; let y_min = player_cell.y - stepmax;