Struct Entity

Source
pub struct Entity {
    pub id: i32,
    pub components: RefCell<HashMap<TypeId, Box<dyn Any>>>,
    pub fresh: RefCell<bool>,
}

Fields§

§id: i32§components: RefCell<HashMap<TypeId, Box<dyn Any>>>§fresh: RefCell<bool>

Implementations§

Source§

impl Entity

Source

pub fn new(id: i32) -> Entity

Source

pub fn refresh(&self)

Mark this entity as not refreshed. On beginning of next frame new registered components will affect their systems.

Examples found in repository?
examples/process_macro.rs (line 52)
43fn main() {
44    let mut world = World::new();
45
46    {
47        let mut entity_manager = world.entity_manager();
48        let entity = entity_manager.create_entity();
49
50        entity.add_component(Position {x : 0});
51        entity.add_component(Velocity {x : 1});
52        entity.refresh();
53    }
54    world.set_system(MoveSystem);
55    world.update();
56
57    world.update();
58    world.update();
59}
More examples
Hide additional examples
examples/multiple_fields.rs (line 35)
25    fn process_one(&mut self, entity : &mut Entity) {
26        let pos = entity.get_component::<Position>();
27        let mut health = entity.get_component::<Health>();
28
29        if pos.x == 5 {
30            health.hp -= 10;
31            println!("You are in bleeding zone, hp: {}", health.hp);
32        }
33        if health.hp <= 0 {
34            entity.remove_component::<Alive>();
35            entity.refresh();
36        }
37    }
38}
39
40fn main() {
41    let mut world = World::new();
42
43    {
44        let mut manager = world.entity_manager();
45        let entity = manager.create_entity();
46        entity.add_component(Health {hp : 100});
47        entity.add_component(Position {x : 5});
48        entity.add_component(Alive);
49        entity.refresh();
50    }
51    world.set_system(BleedZoneSystem);
52
53    for _ in 0..100 {
54        world.update();
55    }
56}
examples/spawn_system.rs (line 28)
22    fn process_w(&mut self, entity : &mut Entity, world : &mut WorldHandle) {
23        let mut spawn_point = entity.get_component::<SpawnPoint>();
24
25        if spawn_point.count > 0 {
26            let spawned = world.entity_manager.create_entity();
27            spawned.add_component(SomeComponent { _some_data : spawn_point.data.to_string() });
28            spawned.refresh();
29
30            spawn_point.count -= 1;
31        }
32    }
33}
34
35fn main() {
36    let mut world = World::new();
37
38    {
39        let mut w = world.entity_manager();
40        let entity = w.create_entity();
41
42        entity.add_component(SpawnPoint {data : "player", count : 5});
43        entity.refresh();
44    }
45
46    world.set_system(SpawnSystem);
47
48    world.update();
49    world.update();
50}
examples/systems.rs (line 62)
55fn main() {
56    let mut world = World::new();
57
58    {
59        let mut entity_manager = world.entity_manager();
60        let entity = entity_manager.create_entity();
61        entity.add_component(Position {pos : [0.0, 0.0, 0.0]});
62        entity.refresh();
63    }
64
65    // if you have position, you will be drawn
66    world.set_system(DrawerSystem);
67    // except you are dead
68    world.set_system(MoverSystem);
69    // but only if you are dead your corpse will be draw, too
70    world.set_system(DeadDrawerSystem);
71
72    world.update();
73    world.update();
74}
examples/render_gui_system.rs (line 94)
81fn main() {
82    let mut world = World::new();
83    world.set_system(RenderSystem::new());
84    world.set_system(GuiSystem);
85    world.set_system(Glutin2HeavySystem);
86
87    {
88        let mut entity_manager = world.entity_manager();
89
90        {
91            let mesh = entity_manager.create_entity();
92
93            mesh.add_component(Renderable);
94            mesh.refresh();
95        }
96        {
97            let window = entity_manager.create_entity();
98
99            window.add_component(GuiWindow);
100            window.refresh();
101        }
102    }
103    world.update();
104    world.update();
105    world.update();
106}
examples/process_types.rs (line 83)
74fn main() {
75    let mut world = World::new();
76
77    {
78        let mut entity_manager = world.entity_manager();
79        let entity = entity_manager.create_entity();
80
81        entity.add_component(Position {pos : [0.0, 0.0, 0.0]});
82        entity.add_component(Mesh {mesh : "player".to_string()});
83        entity.refresh();
84    }
85
86    {
87        let mut entity_manager = world.entity_manager();
88        let entity = entity_manager.create_entity();
89        entity.add_component(Camera {pos : [0.0, 0.0, 0.0]});
90        entity.refresh();
91    }
92    // will process all entities with Position and Mesh,
93    // and in this process all entities with Camera will be accessable
94    world.set_system(RenderSystem);
95
96
97    // same system, but we will use another implementetion inside it, for processing all entities at once
98    world.set_system(DeferRenderSystem);
99
100    world.update();
101    world.update();
102}
Source

pub fn add_component<T: Any + Component>(&self, component: T)

Examples found in repository?
examples/render_gui_system.rs (line 57)
55    fn on_created(&mut self, entity_manager : &mut EntityManager) {
56        let data_entity = entity_manager.create_entity();
57        data_entity.add_component(RenderData {facade : self.facade.clone()});
58    }
59    fn process_one(&mut self, _ : &mut Entity) {
60        self.facade.borrow_mut().draw_something("triangles triangles");
61    }
62}
63
64pub struct GuiSystem;
65
66impl System for GuiSystem {
67    fn aspect(&self) -> Aspect {
68        aspect_all!(GuiWindow)
69    }
70    fn data_aspects(&self) -> Vec<Aspect> {
71        vec![aspect_all!(RenderData, HeavyGuiData)]
72    }
73
74    fn process_d(&mut self, _ : &mut Entity, data : &mut DataList) {
75        let render_data = data.unwrap_entity().get_component::<RenderData>();
76        let _gui_data   = data.unwrap_entity().get_component::<HeavyGuiData>();
77        render_data.facade.borrow_mut().draw_something("gui gui gui");
78    }
79}
80
81fn main() {
82    let mut world = World::new();
83    world.set_system(RenderSystem::new());
84    world.set_system(GuiSystem);
85    world.set_system(Glutin2HeavySystem);
86
87    {
88        let mut entity_manager = world.entity_manager();
89
90        {
91            let mesh = entity_manager.create_entity();
92
93            mesh.add_component(Renderable);
94            mesh.refresh();
95        }
96        {
97            let window = entity_manager.create_entity();
98
99            window.add_component(GuiWindow);
100            window.refresh();
101        }
102    }
103    world.update();
104    world.update();
105    world.update();
106}
More examples
Hide additional examples
examples/process_macro.rs (line 50)
43fn main() {
44    let mut world = World::new();
45
46    {
47        let mut entity_manager = world.entity_manager();
48        let entity = entity_manager.create_entity();
49
50        entity.add_component(Position {x : 0});
51        entity.add_component(Velocity {x : 1});
52        entity.refresh();
53    }
54    world.set_system(MoveSystem);
55    world.update();
56
57    world.update();
58    world.update();
59}
examples/multiple_fields.rs (line 46)
40fn main() {
41    let mut world = World::new();
42
43    {
44        let mut manager = world.entity_manager();
45        let entity = manager.create_entity();
46        entity.add_component(Health {hp : 100});
47        entity.add_component(Position {x : 5});
48        entity.add_component(Alive);
49        entity.refresh();
50    }
51    world.set_system(BleedZoneSystem);
52
53    for _ in 0..100 {
54        world.update();
55    }
56}
examples/spawn_system.rs (line 27)
22    fn process_w(&mut self, entity : &mut Entity, world : &mut WorldHandle) {
23        let mut spawn_point = entity.get_component::<SpawnPoint>();
24
25        if spawn_point.count > 0 {
26            let spawned = world.entity_manager.create_entity();
27            spawned.add_component(SomeComponent { _some_data : spawn_point.data.to_string() });
28            spawned.refresh();
29
30            spawn_point.count -= 1;
31        }
32    }
33}
34
35fn main() {
36    let mut world = World::new();
37
38    {
39        let mut w = world.entity_manager();
40        let entity = w.create_entity();
41
42        entity.add_component(SpawnPoint {data : "player", count : 5});
43        entity.refresh();
44    }
45
46    world.set_system(SpawnSystem);
47
48    world.update();
49    world.update();
50}
examples/systems.rs (line 61)
55fn main() {
56    let mut world = World::new();
57
58    {
59        let mut entity_manager = world.entity_manager();
60        let entity = entity_manager.create_entity();
61        entity.add_component(Position {pos : [0.0, 0.0, 0.0]});
62        entity.refresh();
63    }
64
65    // if you have position, you will be drawn
66    world.set_system(DrawerSystem);
67    // except you are dead
68    world.set_system(MoverSystem);
69    // but only if you are dead your corpse will be draw, too
70    world.set_system(DeadDrawerSystem);
71
72    world.update();
73    world.update();
74}
examples/process_types.rs (line 81)
74fn main() {
75    let mut world = World::new();
76
77    {
78        let mut entity_manager = world.entity_manager();
79        let entity = entity_manager.create_entity();
80
81        entity.add_component(Position {pos : [0.0, 0.0, 0.0]});
82        entity.add_component(Mesh {mesh : "player".to_string()});
83        entity.refresh();
84    }
85
86    {
87        let mut entity_manager = world.entity_manager();
88        let entity = entity_manager.create_entity();
89        entity.add_component(Camera {pos : [0.0, 0.0, 0.0]});
90        entity.refresh();
91    }
92    // will process all entities with Position and Mesh,
93    // and in this process all entities with Camera will be accessable
94    world.set_system(RenderSystem);
95
96
97    // same system, but we will use another implementetion inside it, for processing all entities at once
98    world.set_system(DeferRenderSystem);
99
100    world.update();
101    world.update();
102}
Source

pub fn remove_component<T: Any>(&self)

Remove component of given type from entity Be carefull, if this component is borrowed at this moment, it will not be really deleted.

Examples found in repository?
examples/multiple_fields.rs (line 34)
25    fn process_one(&mut self, entity : &mut Entity) {
26        let pos = entity.get_component::<Position>();
27        let mut health = entity.get_component::<Health>();
28
29        if pos.x == 5 {
30            health.hp -= 10;
31            println!("You are in bleeding zone, hp: {}", health.hp);
32        }
33        if health.hp <= 0 {
34            entity.remove_component::<Alive>();
35            entity.refresh();
36        }
37    }
Source

pub fn has_component<T: Any>(&self) -> bool

Source

pub fn get_component<T: Any + Component>(&self) -> ComponentGuard<'_, T>

Move component from entity to CompoentGuard. In general case, it behaves like &mut T. While component is borrowed, second get_component() with same type will cause panic

Examples found in repository?
examples/systems.rs (line 25)
24    fn process_one(&mut self, entity : &mut Entity) {
25        let pos = entity.get_component::<Position>();
26        println!("{}", pos.pos[0]);
27    }
28}
29
30pub struct DeadDrawerSystem;
31impl System for DeadDrawerSystem {
32    fn aspect(&self) -> Aspect {
33        Aspect::all::<Position>().except::<Dead>()
34    }
35    fn process_one(&mut self, entity : &mut Entity) {
36        let pos = entity.get_component::<Position>();
37        println!("is dead {}", pos.pos[0]);
38    }
39}
40
41pub struct MoverSystem;
42impl System for MoverSystem {
43    fn aspect(&self) -> Aspect {
44        Aspect::all2::<Position, Dead>()
45    }
46
47    fn process_one(&mut self, entity : &mut Entity) {
48        let mut pos = entity.get_component::<Position>();
49        pos.pos[0] += 0.1;
50        println!("Moved! {}", pos.pos[0]);
51    }
More examples
Hide additional examples
examples/render_gui_system.rs (line 75)
74    fn process_d(&mut self, _ : &mut Entity, data : &mut DataList) {
75        let render_data = data.unwrap_entity().get_component::<RenderData>();
76        let _gui_data   = data.unwrap_entity().get_component::<HeavyGuiData>();
77        render_data.facade.borrow_mut().draw_something("gui gui gui");
78    }
examples/process_types.rs (line 36)
34    fn process_d(&mut self, entity : &mut Entity, data : &mut DataList) {
35        let cam = data.unwrap_entity();
36        let cam = cam.get_component::<Camera>();
37
38        let pos = entity.get_component::<Position>();
39        let mesh = entity.get_component::<Mesh>();
40
41        println!("{}, {}, seen from camera pos: {:?}", mesh.mesh, pos.pos[0], cam.pos);
42    }
43}
44
45pub struct DeferRenderSystem;
46
47impl System for DeferRenderSystem {
48    fn aspect(&self) -> Aspect {
49        Aspect::all2::<Position, Mesh>()
50    }
51    fn data_aspects(&self) -> Vec<Aspect> {
52        vec![Aspect::all::<Camera>()]
53    }
54    fn process_all(&mut self,
55                   entities : &mut Vec<&mut Entity>,
56                   _   : &mut WorldHandle,
57                   data   : &mut DataList) {
58        entities.sort_by(|e1, e2| {
59            let defer1 = e1.get_component::<DeferMesh>();
60            let defer2 = e2.get_component::<DeferMesh>();
61            defer1.order.cmp(&defer2.order)
62        });
63        for entity in entities {
64            let cam = data.unwrap_entity();
65            let cam = cam.get_component::<Camera>();
66
67            let mesh = entity.get_component::<Mesh>();
68
69            println!("{}, seen from camera pos: {:?}", mesh.mesh, cam.pos);
70        }
71    }
examples/multiple_fields.rs (line 26)
25    fn process_one(&mut self, entity : &mut Entity) {
26        let pos = entity.get_component::<Position>();
27        let mut health = entity.get_component::<Health>();
28
29        if pos.x == 5 {
30            health.hp -= 10;
31            println!("You are in bleeding zone, hp: {}", health.hp);
32        }
33        if health.hp <= 0 {
34            entity.remove_component::<Alive>();
35            entity.refresh();
36        }
37    }
examples/spawn_system.rs (line 23)
22    fn process_w(&mut self, entity : &mut Entity, world : &mut WorldHandle) {
23        let mut spawn_point = entity.get_component::<SpawnPoint>();
24
25        if spawn_point.count > 0 {
26            let spawned = world.entity_manager.create_entity();
27            spawned.add_component(SomeComponent { _some_data : spawn_point.data.to_string() });
28            spawned.refresh();
29
30            spawn_point.count -= 1;
31        }
32    }

Auto Trait Implementations§

§

impl !Freeze for Entity

§

impl !RefUnwindSafe for Entity

§

impl !Send for Entity

§

impl !Sync for Entity

§

impl Unpin for Entity

§

impl !UnwindSafe for Entity

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.