mirror of
https://codeberg.org/schrottkatze/mgd2-tram-championships.git
synced 2025-07-01 17:27:38 +00:00
you can now despawn entities
This commit is contained in:
parent
07e6d8b8c6
commit
7e531d42a8
4 changed files with 70 additions and 9 deletions
|
@ -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(
|
||||
|
|
|
@ -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<String>,
|
||||
},
|
||||
|
||||
/// Despawn an entity by string id. Uniqueness of Ids isn't guaranteed.
|
||||
Despawn { id: String },
|
||||
}
|
||||
|
|
|
@ -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_despawn,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Component, Clone)]
|
||||
struct Id(String);
|
||||
|
||||
#[derive(Component)]
|
||||
struct HasPath(String);
|
||||
|
||||
fn handle_load_gltf(
|
||||
mut c: Commands,
|
||||
mut dbg_reader: EventReader<DebugEvent>,
|
||||
|
@ -16,11 +30,47 @@ fn handle_load_gltf(
|
|||
asset_server: Res<AssetServer>,
|
||||
) {
|
||||
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<DebugEvent>,
|
||||
mut logger: ResMut<ConsoleLog>,
|
||||
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:?}."
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,4 +2,4 @@ log "hello from startup script!!"
|
|||
|
||||
enable-debug
|
||||
start-game
|
||||
load-scene gltf/test.glb
|
||||
load-scene gltf/test.glb test
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue