update asteroids only when changing cell

This commit is contained in:
yuni 2024-04-02 05:59:33 +02:00
parent 3ea2f3bc3a
commit be55433376

View file

@ -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<ActiveAsteroids>,
asset_server: Res<AssetServer>,
mut q_asteroid: Query<(Entity, &mut Visibility), With<Asteroid>>,
mut last_player_cell: Local<I64Vec3>,
mut log: ResMut<hud::Log>,
) {
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;