feat: impl proper entity handles

Signed-off-by: Schmarni <marnistromer@gmail.com>
This commit is contained in:
Schmarni
2025-10-28 23:02:24 +01:00
parent ec468b6752
commit 65c426f981
6 changed files with 59 additions and 38 deletions

View File

@@ -1,3 +1,6 @@
use std::ops::Deref;
use std::sync::Arc;
use bevy::prelude::*;
use super::bevy_channel::{BevyChannel, BevyChannelReader};
@@ -16,16 +19,28 @@ fn despawn(mut cmds: Commands, mut reader: ResMut<BevyChannelReader<Entity>>) {
}
}
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct EntityHandle(Arc<EntityHandleInner>);
impl EntityHandle {
pub fn get(&self) -> Entity {
self.0.0
}
pub fn new(entity: Entity) -> Self {
Self(EntityHandleInner(entity).into())
}
}
impl Deref for EntityHandle {
type Target = Entity;
fn deref(&self) -> &Self::Target {
&self.0.0
}
}
static DESTROY: BevyChannel<Entity> = BevyChannel::new();
#[derive(Deref, DerefMut, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct EntityHandle(pub Entity);
impl Drop for EntityHandle {
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct EntityHandleInner(Entity);
impl Drop for EntityHandleInner {
fn drop(&mut self) {
DESTROY.send(self.0);
}
}
impl From<Entity> for EntityHandle {
fn from(value: Entity) -> Self {
Self(value)
}
}