Skip to main content

concinnity_core/components/
entity_target.rs

1// src/components/entity_target.rs
2
3use crate::ecs::Entity;
4use crate::ecs::asset_id::AssetId;
5
6/// How a runtime request addresses the entity it acts on. Authored placements
7/// are named, so a producer holding no live handle addresses them by asset name
8/// and the applying system resolves it. Logic that already holds an entity --
9/// one it spawned, one a query yielded -- addresses it directly, which is the
10/// only way to reach an entity that never had a name.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum EntityTarget {
13    /// Addressed by the placement's authored name.
14    Name(AssetId),
15    /// Addressed by a live entity handle.
16    Entity(Entity),
17}
18
19impl Default for EntityTarget {
20    fn default() -> Self {
21        EntityTarget::Name(AssetId::default())
22    }
23}
24
25impl From<AssetId> for EntityTarget {
26    fn from(name: AssetId) -> Self {
27        EntityTarget::Name(name)
28    }
29}
30
31impl From<Entity> for EntityTarget {
32    fn from(entity: Entity) -> Self {
33        EntityTarget::Entity(entity)
34    }
35}
36
37impl EntityTarget {
38    /// The asset name this target addresses, if it is name-addressed.
39    pub fn name(self) -> Option<AssetId> {
40        match self {
41            EntityTarget::Name(name) => Some(name),
42            EntityTarget::Entity(_) => None,
43        }
44    }
45}