forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchildren.rs
More file actions
43 lines (36 loc) · 1.14 KB
/
Copy pathchildren.rs
File metadata and controls
43 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use bevy_ecs::{
component::Component,
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
reflect::{ReflectComponent, ReflectMapEntities},
};
use bevy_reflect::Reflect;
use smallvec::SmallVec;
use std::ops::Deref;
/// Contains references to the child entities of this entity
#[derive(Component, Default, Clone, Debug, Reflect)]
#[reflect(Component, MapEntities)]
pub struct Children(pub(crate) SmallVec<[Entity; 8]>);
impl MapEntities for Children {
fn map_entities(&mut self, entity_map: &EntityMap) -> Result<(), MapEntitiesError> {
for entity in self.0.iter_mut() {
*entity = entity_map.get(*entity)?;
}
Ok(())
}
}
impl Children {
/// Builds and returns a [`Children`] component with the given entities
pub fn with(entity: &[Entity]) -> Self {
Self(SmallVec::from_slice(entity))
}
/// Swaps the child at `a_index` with the child at `b_index`
pub fn swap(&mut self, a_index: usize, b_index: usize) {
self.0.swap(a_index, b_index);
}
}
impl Deref for Children {
type Target = [Entity];
fn deref(&self) -> &Self::Target {
&self.0[..]
}
}