fix despawning of scene-based asteroids, turn them back from spheres to scenes

This commit is contained in:
yuni 2024-04-01 23:54:56 +02:00
parent ca0b2d9b96
commit cfbd23f485

View file

@ -11,7 +11,7 @@ const ASTEROID_SIZE: f32 = 100.0;
const STARS_MAX_MAGNITUDE: f32 = 5.5; const STARS_MAX_MAGNITUDE: f32 = 5.5;
const CENTER_WORLD_ON_PLAYER: bool = true; const CENTER_WORLD_ON_PLAYER: bool = true;
const ASTEROIDS_ARE_SPHERES: bool = true; const ASTEROIDS_ARE_SPHERES: bool = false;
const ASSET_ASTEROID1: &str = "models/asteroid.glb#Scene0"; const ASSET_ASTEROID1: &str = "models/asteroid.glb#Scene0";
const ASSET_ASTEROID2: &str = "models/asteroid2.glb#Scene0"; const ASSET_ASTEROID2: &str = "models/asteroid2.glb#Scene0";
@ -36,6 +36,7 @@ impl Plugin for WorldPlugin {
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_systems(Startup, generate_asteroids); app.add_systems(Startup, generate_asteroids);
app.add_systems(Update, handle_cheats); app.add_systems(Update, handle_cheats);
app.add_systems(PostUpdate, handle_despawn);
app.add_systems(Update, spawn_despawn_asteroids); app.add_systems(Update, spawn_despawn_asteroids);
app.add_plugins(PhysicsPlugins::default()); app.add_plugins(PhysicsPlugins::default());
app.add_plugins(MaterialPlugin::<RingMaterial>::default()); app.add_plugins(MaterialPlugin::<RingMaterial>::default());
@ -44,6 +45,7 @@ impl Plugin for WorldPlugin {
app.insert_resource(AsteroidUpdateTimer( app.insert_resource(AsteroidUpdateTimer(
Timer::from_seconds(ASTEROID_UPDATE_INTERVAL, TimerMode::Repeating))); Timer::from_seconds(ASTEROID_UPDATE_INTERVAL, TimerMode::Repeating)));
app.insert_resource(AsteroidDatabase(Vec::new())); app.insert_resource(AsteroidDatabase(Vec::new()));
app.add_event::<DespawnEvent>();
if CENTER_WORLD_ON_PLAYER { if CENTER_WORLD_ON_PLAYER {
// Disable bevy_xpbd's position->transform sync function // Disable bevy_xpbd's position->transform sync function
@ -71,6 +73,9 @@ struct AsteroidData {
pos: DVec3, pos: DVec3,
} }
#[derive(Event)]
pub struct DespawnEvent(Entity);
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
pub struct RingMaterial { pub struct RingMaterial {
alpha_mode: AlphaMode, alpha_mode: AlphaMode,
@ -207,6 +212,7 @@ fn spawn_despawn_asteroids(
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
mut log: ResMut<hud::Log>, mut log: ResMut<hud::Log>,
mut ew_despawn: EventWriter<DespawnEvent>,
) { ) {
if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() { if !timer.0.tick(time.delta()).just_finished() || q_player.is_empty() {
return; return;
@ -230,7 +236,7 @@ fn spawn_despawn_asteroids(
asteroid.entity = None; asteroid.entity = None;
asteroid.is_spawned = false; asteroid.is_spawned = false;
despawned += 1; despawned += 1;
commands.entity(entity).despawn(); ew_despawn.send(DespawnEvent(entity));
break; break;
} }
} }
@ -291,6 +297,15 @@ fn spawn_despawn_asteroids(
} }
} }
fn handle_despawn(
mut commands: Commands,
mut er_despawn: EventReader<DespawnEvent>,
) {
for despawn in er_despawn.read() {
commands.entity(despawn.0).despawn();
}
}
fn handle_cheats( fn handle_cheats(
mut commands: Commands, mut commands: Commands,
key_input: Res<ButtonInput<KeyCode>>, key_input: Res<ButtonInput<KeyCode>>,