From bbad89e1fbd82ec84af54537516f8a19c25951c8 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 1 Apr 2024 20:52:57 +0200 Subject: [PATCH] WIP despawning asteroids... for some reason, scenes can't despawn --- src/world.rs | 113 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 90 insertions(+), 23 deletions(-) diff --git a/src/world.rs b/src/world.rs index 722b1e4..9e9e203 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,4 +1,4 @@ -use crate::{actor, nature, settings}; +use crate::{actor, nature, settings, hud}; use bevy::prelude::*; use bevy::render::render_resource::{AsBindGroup, ShaderRef}; use bevy::math::DVec3; @@ -6,11 +6,12 @@ use bevy_xpbd_3d::prelude::*; use bevy_xpbd_3d::plugins::sync::SyncConfig; use std::f32::consts::PI; -const ASTEROID_UPDATE_INTERVAL: f32 = 1.0; // seconds +const ASTEROID_UPDATE_INTERVAL: f32 = 0.1; // seconds const ASTEROID_SIZE: f32 = 100.0; const STARS_MAX_MAGNITUDE: f32 = 5.5; const CENTER_WORLD_ON_PLAYER: bool = true; +const ASTEROIDS_ARE_SPHERES: bool = true; const ASSET_ASTEROID1: &str = "models/asteroid.glb#Scene0"; const ASSET_ASTEROID2: &str = "models/asteroid2.glb#Scene0"; @@ -59,13 +60,15 @@ impl Plugin for WorldPlugin { #[derive(Resource)] struct AsteroidUpdateTimer(Timer); #[derive(Resource)] struct AsteroidDatabase(Vec); +#[derive(Component)] struct Asteroid; + struct AsteroidData { + entity: Option, is_spawned: bool, + spawned_once: bool, class: u8, size: f32, - x: f64, - y: f64, - z: f64, + pos: DVec3, } #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] @@ -94,7 +97,6 @@ pub fn setup( mut meshes: ResMut>, mut materials: ResMut>, mut materials_custom: ResMut>, - asset_server: Res, ) { // Generate starmap let sphere_handle = meshes.add(Sphere::new(1.0)); @@ -177,10 +179,14 @@ fn generate_asteroids( let wobble: f64 = dist/2.0; let (i, j, k) = (i as f64, j as f64, k as f64); db.0.push(AsteroidData { + entity: None, is_spawned: false, - x: player_x + offset + dist * i + wobble * (j+k/pi).sin() * (k+j/pi).cos(), - y: player_y + offset + dist * j + wobble * (k+i/pi).sin() * (i+k/pi).cos(), - z: player_z + offset + dist * k + wobble * (i+j/pi).sin() * (j+i/pi).cos(), + spawned_once: false, + pos: DVec3::new( + player_x + offset + dist * i + wobble * (j+k/pi).sin() * (k+j/pi).cos(), + player_y + offset + dist * j + wobble * (k+i/pi).sin() * (i+k/pi).cos(), + player_z + offset + dist * k + wobble * (i+j/pi).sin() * (j+i/pi).cos(), + ), size: ASTEROID_SIZE, class: (((i+j+k) as i32) % 2) as u8, }); @@ -194,45 +200,102 @@ fn spawn_despawn_asteroids( mut timer: ResMut, mut db: ResMut, mut commands: Commands, + q_player: Query<&Position, With>, + mut meshes: ResMut>, + mut q_asteroid: Query<(Entity, &mut Visibility), With>, + mut materials: ResMut>, asset_server: Res, + mut log: ResMut, ) { - if !timer.0.tick(time.delta()).just_finished() { + if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() { return; } + let player = q_player.get_single().unwrap(); + let mut spawned = 0; + let mut despawned = 0; for asteroid in &mut db.0 { - if asteroid.is_spawned { + let dist = player.distance(asteroid.pos); + let should_spawn = dist < 10000.0; + if should_spawn == asteroid.is_spawned { + continue; // Nothing to do + } + if !should_spawn { + // Despawn + if let Some(entity) = asteroid.entity { + for (ent, mut vis) in &mut q_asteroid { + if ent == entity { + *vis = Visibility::Hidden; + asteroid.entity = None; + asteroid.is_spawned = false; + despawned += 1; + commands.entity(entity).despawn(); + break; + } + } + } continue; } - asteroid.is_spawned = true; - let asset = match asteroid.class { - 0 => ASSET_ASTEROID1, - _ => ASSET_ASTEROID2, - }; - commands.spawn(( + + // Spawn + let mut entity_commands = commands.spawn(( actor::Actor::default(), RigidBody::Dynamic, AngularVelocity(DVec3::new(0.1, 0.1, 0.03)), LinearVelocity(DVec3::new(0.0, 0.0, 0.35)), Collider::sphere(1.0), - SceneBundle { + Rotation::from(Quat::from_rotation_y(-PI / 3.)), + Position::new(asteroid.pos), + Asteroid, + )); + if ASTEROIDS_ARE_SPHERES { + let sphere_handle = meshes.add(Sphere::default().mesh().uv(128, 128)); + let sphere_material_handle = materials.add(StandardMaterial { + base_color: Color::rgb(0.4, 0.4, 0.4), + perceptual_roughness: 1.0, + metallic: 0.0, + ..default() + }); + entity_commands.insert(PbrBundle { + mesh: sphere_handle, + material: sphere_material_handle, transform: Transform { scale: Vec3::splat(asteroid.size), ..default() }, - scene: asset_server.load(asset), ..default() - }, - Rotation::from(Quat::from_rotation_y(-PI / 3.)), - Position::from_xyz(asteroid.x, asteroid.y, asteroid.z), - )); + }); + } + else { + let asset = match asteroid.class { + 0 => ASSET_ASTEROID1, + _ => ASSET_ASTEROID2, + }; + entity_commands.insert(SceneBundle { + scene: asset_server.load(asset), + transform: Transform { + scale: Vec3::splat(asteroid.size), + ..default() + }, + ..default() + }); + } + asteroid.entity = Some(entity_commands.id()); + spawned += 1; + asteroid.spawned_once = true; + asteroid.is_spawned = true; + } + if spawned != 0 || despawned != 0 { + log.notice(format!("spawned: {spawned}, despawned: {despawned}")); } } fn handle_cheats( + mut commands: Commands, key_input: Res>, mut q_player: Query<(&Transform, &mut Position, &mut LinearVelocity), With>, mut q_life: Query<(&mut actor::LifeForm, ), With>, + q_entities: Query, Without)>, settings: ResMut, ) { if !settings.dev_mode || q_player.is_empty() || q_life.is_empty() { @@ -259,6 +322,10 @@ fn handle_cheats( v.0 += DVec3::from(trans.rotation * Vec3::new(0.0, 0.0, -1000.0)); } if key_input.pressed(settings.key_cheat_adrenaline_zero) { + // NOTE: temporarily added despawn all in here + for entity in &q_entities { + commands.entity(entity).despawn(); + } lifeform.adrenaline = 0.0; } if key_input.pressed(settings.key_cheat_adrenaline_mid) {