use bevy_ecs::{
prelude::{Component, Entity, World},
world::Command,
};
use anyhow::anyhow;
use backtrace::Backtrace;
use std::sync::Arc;
use crate::{MiscellaneousFailure, UnhandledErrors, UnusedTarget};
#[derive(Component, Default)]
pub(crate) struct Detached(bool);
impl Detached {
pub fn is_detached(&self) -> bool {
self.0
}
}
pub(crate) struct Detach {
pub(crate) target: Entity,
}
impl Command for Detach {
fn apply(self, world: &mut World) {
let backtrace;
if let Some(mut session_mut) = world.get_entity_mut(self.target) {
if let Some(mut detached) = session_mut.get_mut::<Detached>() {
detached.0 = true;
session_mut.remove::<UnusedTarget>();
return;
} else {
backtrace = Backtrace::new();
}
} else {
backtrace = Backtrace::new();
}
let failure = MiscellaneousFailure {
error: Arc::new(anyhow!("Unable to detach target {:?}", self.target)),
backtrace: Some(backtrace),
};
world
.get_resource_or_insert_with(UnhandledErrors::default)
.miscellaneous
.push(failure);
}
}