pub struct EntityData(/* private fields */);Expand description
Holds all the data an entity contains, its components for example.
Implementations§
Source§impl EntityData
impl EntityData
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new EntityData
Sourcepub fn add<C: Component>(&mut self, c: C)
pub fn add<C: Component>(&mut self, c: C)
Add a component to the entity
Examples found in repository?
examples/schedule.rs (line 36)
24fn add_bullets(world: &mut World) {
25 let bullets = [
26 (Position(4, 7), Velocity(3, 0)),
27 (Position(0, 0), Velocity(0, 0)),
28 (Position(5, -8), Velocity(0, -5)),
29 (Position(12, 0), Velocity(-1, 0)),
30 ];
31
32 for (position, velocity) in bullets {
33 let entity = world.spawn();
34 let entity = world.get_entity_mut(entity).unwrap();
35
36 entity.add(Bullet);
37 entity.add(position);
38 entity.add(velocity);
39 }
40}More examples
examples/stress.rs (line 17)
6fn main() {
7 const ENTIIES: u64 = 1_000_000;
8 let time = SystemTime::now();
9
10 let mut world = World::new();
11
12 #[allow(dead_code)]
13 struct Number(u64);
14
15 for n in 0..=ENTIIES {
16 let id = world.spawn();
17 world.get_entity_mut(id).unwrap().add(Number(n));
18 }
19
20 for e in &Query::query_world(
21 &world,
22 QueryFilter::new()
23 .with_requires(&[component_id!(Number)])
24 .with_func(Some(|id, world| {
25 world.get_entity(id).unwrap().get::<Number>().unwrap().0 % 147 == 0
26 })),
27 ) {
28 world.despawn(e);
29 }
30
31 println!(
32 "spawning and filtering {ENTIIES} entities took {}ms",
33 time.elapsed().unwrap().as_millis()
34 );
35}examples/basic.rs (line 22)
4fn main() {
5 let mut world = World::new();
6
7 struct Name(String);
8 struct Age(u8);
9 struct Human;
10
11 let people = [
12 (Name("Alice".to_owned()), Age(20)),
13 (Name("Bob".to_owned()), Age(26)),
14 (Name("Cathrine".to_owned()), Age(33)),
15 (Name("Dennis".to_owned()), Age(10)),
16 ];
17
18 for (name, age) in people {
19 let entity = world.spawn();
20 let entity = world.get_entity_mut(entity).unwrap();
21
22 entity.add(name);
23 entity.add(age);
24 entity.add(Human);
25 }
26
27 {
28 let entity = world.spawn();
29 let entity = world.get_entity_mut(entity).unwrap();
30
31 entity.add(Name("Jupiter".to_owned()));
32 }
33
34 for e in &Query::query_world(
35 &world,
36 QueryFilter {
37 requires: vec![
38 component_id!(Name),
39 component_id!(Age),
40 component_id!(Human),
41 ],
42 ..Default::default()
43 },
44 ) {
45 let e = world.get_entity_mut(e).unwrap();
46 let age = e.get::<Age>().map(|x| Age(x.0)).unwrap();
47 let name = e.get_mut::<Name>().unwrap();
48
49 name.0 = name.0.to_uppercase();
50 println!("Found human named {}, who is {} years old", name.0, age.0);
51 }
52
53 for e in &Query::query_world(
54 &world,
55 QueryFilter {
56 requires: vec![component_id!(Name)],
57 ..Default::default()
58 },
59 ) {
60 let entity = world.get_entity(e).unwrap();
61 let name = entity.get::<Name>().unwrap();
62
63 println!("This entity (#{e}) has a name: {}", name.0);
64 }
65}Sourcepub fn get<C: Component>(&self) -> Option<&C>
pub fn get<C: Component>(&self) -> Option<&C>
Get a reference to the component of type C
§Examples
use xuko_ecs::prelude::*;
let mut world = World::new();
{
let id = world.spawn();
let entity_ref = world.get_entity(id).unwrap();
if let Some(c) = entity_ref.get::<i32>() {
// do stuff with `c`
}
}Examples found in repository?
examples/schedule.rs (line 54)
42fn move_entities(world: &mut World) {
43 let entities = Query::query_world(
44 world,
45 QueryFilter {
46 requires: vec![component_id!(Position), component_id!(Velocity)],
47 ..Default::default()
48 },
49 );
50
51 for e in &entities {
52 let entity = world.get_entity_mut(e).unwrap();
53
54 let velocity = entity.get::<Velocity>().cloned().unwrap();
55 let position = entity.get_mut::<Position>().unwrap();
56
57 position.0 += velocity.0;
58 position.1 += velocity.1;
59
60 println!("Something that has a position: {position:?}");
61 }
62}More examples
examples/stress.rs (line 25)
6fn main() {
7 const ENTIIES: u64 = 1_000_000;
8 let time = SystemTime::now();
9
10 let mut world = World::new();
11
12 #[allow(dead_code)]
13 struct Number(u64);
14
15 for n in 0..=ENTIIES {
16 let id = world.spawn();
17 world.get_entity_mut(id).unwrap().add(Number(n));
18 }
19
20 for e in &Query::query_world(
21 &world,
22 QueryFilter::new()
23 .with_requires(&[component_id!(Number)])
24 .with_func(Some(|id, world| {
25 world.get_entity(id).unwrap().get::<Number>().unwrap().0 % 147 == 0
26 })),
27 ) {
28 world.despawn(e);
29 }
30
31 println!(
32 "spawning and filtering {ENTIIES} entities took {}ms",
33 time.elapsed().unwrap().as_millis()
34 );
35}examples/basic.rs (line 46)
4fn main() {
5 let mut world = World::new();
6
7 struct Name(String);
8 struct Age(u8);
9 struct Human;
10
11 let people = [
12 (Name("Alice".to_owned()), Age(20)),
13 (Name("Bob".to_owned()), Age(26)),
14 (Name("Cathrine".to_owned()), Age(33)),
15 (Name("Dennis".to_owned()), Age(10)),
16 ];
17
18 for (name, age) in people {
19 let entity = world.spawn();
20 let entity = world.get_entity_mut(entity).unwrap();
21
22 entity.add(name);
23 entity.add(age);
24 entity.add(Human);
25 }
26
27 {
28 let entity = world.spawn();
29 let entity = world.get_entity_mut(entity).unwrap();
30
31 entity.add(Name("Jupiter".to_owned()));
32 }
33
34 for e in &Query::query_world(
35 &world,
36 QueryFilter {
37 requires: vec![
38 component_id!(Name),
39 component_id!(Age),
40 component_id!(Human),
41 ],
42 ..Default::default()
43 },
44 ) {
45 let e = world.get_entity_mut(e).unwrap();
46 let age = e.get::<Age>().map(|x| Age(x.0)).unwrap();
47 let name = e.get_mut::<Name>().unwrap();
48
49 name.0 = name.0.to_uppercase();
50 println!("Found human named {}, who is {} years old", name.0, age.0);
51 }
52
53 for e in &Query::query_world(
54 &world,
55 QueryFilter {
56 requires: vec![component_id!(Name)],
57 ..Default::default()
58 },
59 ) {
60 let entity = world.get_entity(e).unwrap();
61 let name = entity.get::<Name>().unwrap();
62
63 println!("This entity (#{e}) has a name: {}", name.0);
64 }
65}Sourcepub fn get_mut<C: Component>(&mut self) -> Option<&mut C>
pub fn get_mut<C: Component>(&mut self) -> Option<&mut C>
Get a mutable reference to the component of type C
Examples found in repository?
examples/schedule.rs (line 55)
42fn move_entities(world: &mut World) {
43 let entities = Query::query_world(
44 world,
45 QueryFilter {
46 requires: vec![component_id!(Position), component_id!(Velocity)],
47 ..Default::default()
48 },
49 );
50
51 for e in &entities {
52 let entity = world.get_entity_mut(e).unwrap();
53
54 let velocity = entity.get::<Velocity>().cloned().unwrap();
55 let position = entity.get_mut::<Position>().unwrap();
56
57 position.0 += velocity.0;
58 position.1 += velocity.1;
59
60 println!("Something that has a position: {position:?}");
61 }
62}More examples
examples/basic.rs (line 47)
4fn main() {
5 let mut world = World::new();
6
7 struct Name(String);
8 struct Age(u8);
9 struct Human;
10
11 let people = [
12 (Name("Alice".to_owned()), Age(20)),
13 (Name("Bob".to_owned()), Age(26)),
14 (Name("Cathrine".to_owned()), Age(33)),
15 (Name("Dennis".to_owned()), Age(10)),
16 ];
17
18 for (name, age) in people {
19 let entity = world.spawn();
20 let entity = world.get_entity_mut(entity).unwrap();
21
22 entity.add(name);
23 entity.add(age);
24 entity.add(Human);
25 }
26
27 {
28 let entity = world.spawn();
29 let entity = world.get_entity_mut(entity).unwrap();
30
31 entity.add(Name("Jupiter".to_owned()));
32 }
33
34 for e in &Query::query_world(
35 &world,
36 QueryFilter {
37 requires: vec![
38 component_id!(Name),
39 component_id!(Age),
40 component_id!(Human),
41 ],
42 ..Default::default()
43 },
44 ) {
45 let e = world.get_entity_mut(e).unwrap();
46 let age = e.get::<Age>().map(|x| Age(x.0)).unwrap();
47 let name = e.get_mut::<Name>().unwrap();
48
49 name.0 = name.0.to_uppercase();
50 println!("Found human named {}, who is {} years old", name.0, age.0);
51 }
52
53 for e in &Query::query_world(
54 &world,
55 QueryFilter {
56 requires: vec![component_id!(Name)],
57 ..Default::default()
58 },
59 ) {
60 let entity = world.get_entity(e).unwrap();
61 let name = entity.get::<Name>().unwrap();
62
63 println!("This entity (#{e}) has a name: {}", name.0);
64 }
65}Sourcepub fn keys(&self) -> Vec<ComponentId>
pub fn keys(&self) -> Vec<ComponentId>
Get a list of all the types of components this entity has
Sourcepub fn has_component<C: Component>(&self) -> bool
pub fn has_component<C: Component>(&self) -> bool
Check whether this map has component C
Trait Implementations§
Source§impl Default for EntityData
impl Default for EntityData
Source§fn default() -> EntityData
fn default() -> EntityData
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for EntityData
impl !RefUnwindSafe for EntityData
impl Send for EntityData
impl Sync for EntityData
impl Unpin for EntityData
impl !UnwindSafe for EntityData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more