From 7e531d42a80133d6d5b5acfd45e251a13ef42664 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Thu, 26 Jun 2025 19:59:31 +0200 Subject: [PATCH] you can now despawn entities --- src/debug.rs | 4 +-- src/debug/debug_event.rs | 15 +++++++++-- src/game/debug.rs | 58 +++++++++++++++++++++++++++++++++++++--- startup.tx | 2 +- 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/src/debug.rs b/src/debug.rs index 10d5330..74fde4e 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -2,7 +2,7 @@ use std::{fs, path::PathBuf}; use bevy::prelude::*; -use bevy_inspector_egui::bevy_egui::EguiPlugin; +use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin}; use clap::Subcommand; use console::exec_script; @@ -36,7 +36,7 @@ pub fn plugin(app: &mut App) { .in_set(DebugSet), ) .add_systems(Startup, startup_file.pipe(exec_script)) - .add_plugins(console::plugin); + .add_plugins((console::plugin /* WorldInspectorPlugin::default() */,)); } fn start_game( diff --git a/src/debug/debug_event.rs b/src/debug/debug_event.rs index 0c530cc..4f10cc4 100644 --- a/src/debug/debug_event.rs +++ b/src/debug/debug_event.rs @@ -50,11 +50,22 @@ pub enum DebugEvent { /// Debug mode cannot be disabled. Just restart the game lmao. /// /// Current debug mode behaviour: - /// - Nothing + /// - You can load gtlf scenes #[command(name = "enable-debug", aliases = ["debug-enable"])] EnableDebugMode, /// Load a gltf scene. + /// + /// Auto-reloading isn't possible due to engine reasons, see: + /// https://github.com/bevyengine/bevy/pull/18358 #[command(name = "load-scene", aliases = ["load-file", "load"])] - LoadGltf { asset_path: String }, + LoadGltf { + /// The asset path of the item. + asset_path: String, + /// Id for the item, so it can easily be selected using other debug tools. + id: Option, + }, + + /// Despawn an entity by string id. Uniqueness of Ids isn't guaranteed. + Despawn { id: String }, } diff --git a/src/game/debug.rs b/src/game/debug.rs index 20a7fce..9e2dcfe 100644 --- a/src/game/debug.rs +++ b/src/game/debug.rs @@ -1,14 +1,28 @@ -use bevy::{asset::AssetPath, prelude::*}; +use std::path::PathBuf; + +use bevy::{ + asset::{AssetPath, io::AssetSourceEvent}, + prelude::*, +}; use crate::debug::{ConsoleLog, DebugEvent, DebugMode}; pub fn plugin(app: &mut App) { app.add_systems( Update, - handle_load_gltf.run_if(in_state(DebugMode::Enabled)), + ( + handle_load_gltf.run_if(in_state(DebugMode::Enabled)), + handle_despawn, + ), ); } +#[derive(Component, Clone)] +struct Id(String); + +#[derive(Component)] +struct HasPath(String); + fn handle_load_gltf( mut c: Commands, mut dbg_reader: EventReader, @@ -16,11 +30,47 @@ fn handle_load_gltf( asset_server: Res, ) { for ev in dbg_reader.read() { - if let DebugEvent::LoadGltf { asset_path } = ev { + if let DebugEvent::LoadGltf { asset_path, id } = ev { + logger.write(&format!("Loading {asset_path}...")); let scene_handle = asset_server .load(GltfAssetLabel::Scene(0).from_asset(AssetPath::from(asset_path.clone()))); - c.spawn(SceneRoot(scene_handle)); + let mut scene = c.spawn((SceneRoot(scene_handle), HasPath(asset_path.clone()))); + + if let Some(id) = id { + scene.insert(Id(id.clone())); + } + logger.write( + "Should be loaded now. If nothing happened, check if you might've typoed the path.", + ); + } + } +} + +fn handle_despawn( + mut c: Commands, + mut dbg_reader: EventReader, + mut logger: ResMut, + entities: Query<(Entity, &Id)>, +) { + for ev in dbg_reader.read() { + if let DebugEvent::Despawn { id: target_id } = ev { + let mut count = 0; + entities + .iter() + .filter_map(|(entity, Id(id))| (id == target_id).then_some(entity)) + .for_each(|entity| { + count += 1; + c.entity(entity).despawn() + }); + + if count > 0 { + logger.write(&format!("Despawned {count} entities.")); + } else { + logger.err(&format!( + "Couldn't find any entities to despawn with id {target_id:?}." + )) + } } } } diff --git a/startup.tx b/startup.tx index 2ce0900..57bfa59 100644 --- a/startup.tx +++ b/startup.tx @@ -2,4 +2,4 @@ log "hello from startup script!!" enable-debug start-game -load-scene gltf/test.glb +load-scene gltf/test.glb test