pub struct World<S> { /* private fields */ }Expand description
A container for managing entities and their components.
§Examples
use checs::{ComponentVec, IntoQuery, LendingIterator, Storage, World};
#[derive(Default, Storage)]
struct Storage {
strs: ComponentVec<&'static str>,
ints: ComponentVec<i32>,
}
let mut world = World::<Storage>::new();
let e0 = world.spawn_with(("ninety-nine", 99));
let e1 = world.spawn_with(("zero",));
let e2 = world.spawn_with(("forty-two", 42));
let e3 = world.spawn_with((1,));
let e4 = world.spawn_with(("seventeen", 17));
let storage = world.storage_mut();
let mut query = (&storage.strs, &mut storage.ints).into_query();
assert_eq!(query.next(), Some((e0, (&"ninety-nine", &mut 99))));
assert_eq!(query.next(), Some((e2, (&"forty-two", &mut 42))));
assert_eq!(query.next(), Some((e4, (&"seventeen", &mut 17))));
assert_eq!(query.next(), None);Implementations§
Source§impl<S> World<S>
impl<S> World<S>
Sourcepub fn new() -> Selfwhere
S: Default,
pub fn new() -> Selfwhere
S: Default,
Constructs a new World.
Examples found in repository?
22fn main() {
23 let mut world = world::World::<Storage>::new();
24
25 // Create entities with initial components.
26 // Either manually...
27 let player = world.spawn();
28 world.insert(player, Position { x: 0, y: 0 });
29 world.insert(player, Visible);
30 world.insert(player, Health(80));
31
32 // ...or by using the `spawn` macro...
33 let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34 let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36 // ...or by using `spawn_with`.
37 let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39 let storage = world.storage_mut();
40
41 // Create a query over all entities that have `Position`, `Health`, and
42 // `Visible` components.
43 let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45 query.for_each(|(_, (_, h, _))| {
46 h.0 += 10;
47 });
48
49 println!();
50
51 let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53 query.for_each(|(e, (p, h, v))| {
54 println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55 });
56}Sourcepub fn storage_mut(&mut self) -> &mut S
pub fn storage_mut(&mut self) -> &mut S
Returns a mutable reference to the component storage.
Examples found in repository?
22fn main() {
23 let mut world = world::World::<Storage>::new();
24
25 // Create entities with initial components.
26 // Either manually...
27 let player = world.spawn();
28 world.insert(player, Position { x: 0, y: 0 });
29 world.insert(player, Visible);
30 world.insert(player, Health(80));
31
32 // ...or by using the `spawn` macro...
33 let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34 let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36 // ...or by using `spawn_with`.
37 let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39 let storage = world.storage_mut();
40
41 // Create a query over all entities that have `Position`, `Health`, and
42 // `Visible` components.
43 let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45 query.for_each(|(_, (_, h, _))| {
46 h.0 += 10;
47 });
48
49 println!();
50
51 let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53 query.for_each(|(e, (p, h, v))| {
54 println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55 });
56}Sourcepub fn spawn(&mut self) -> Entity
pub fn spawn(&mut self) -> Entity
Creates a new entity.
Examples found in repository?
22fn main() {
23 let mut world = world::World::<Storage>::new();
24
25 // Create entities with initial components.
26 // Either manually...
27 let player = world.spawn();
28 world.insert(player, Position { x: 0, y: 0 });
29 world.insert(player, Visible);
30 world.insert(player, Health(80));
31
32 // ...or by using the `spawn` macro...
33 let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34 let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36 // ...or by using `spawn_with`.
37 let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39 let storage = world.storage_mut();
40
41 // Create a query over all entities that have `Position`, `Health`, and
42 // `Visible` components.
43 let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45 query.for_each(|(_, (_, h, _))| {
46 h.0 += 10;
47 });
48
49 println!();
50
51 let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53 query.for_each(|(e, (p, h, v))| {
54 println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55 });
56}Sourcepub fn spawn_with<T>(&mut self, value: T) -> Entitywhere
Self: InsertMany<T>,
pub fn spawn_with<T>(&mut self, value: T) -> Entitywhere
Self: InsertMany<T>,
Creates a new entity with initial components.
Examples found in repository?
22fn main() {
23 let mut world = world::World::<Storage>::new();
24
25 // Create entities with initial components.
26 // Either manually...
27 let player = world.spawn();
28 world.insert(player, Position { x: 0, y: 0 });
29 world.insert(player, Visible);
30 world.insert(player, Health(80));
31
32 // ...or by using the `spawn` macro...
33 let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34 let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36 // ...or by using `spawn_with`.
37 let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39 let storage = world.storage_mut();
40
41 // Create a query over all entities that have `Position`, `Health`, and
42 // `Visible` components.
43 let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45 query.for_each(|(_, (_, h, _))| {
46 h.0 += 10;
47 });
48
49 println!();
50
51 let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53 query.for_each(|(e, (p, h, v))| {
54 println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55 });
56}Sourcepub fn despawn(&mut self, entity: Entity) -> Result<(), Error>where
S: RemoveAll,
pub fn despawn(&mut self, entity: Entity) -> Result<(), Error>where
S: RemoveAll,
Removes an entity and its components.
§Errors
The method internally calls entity::Allocator::free, so it might return any of its
errors.
Sourcepub fn despawn_all<T>(&mut self) -> Result<(), Error>
pub fn despawn_all<T>(&mut self) -> Result<(), Error>
Removes all entities that have the specified component T.
Also removes all of their other components.
§Errors
The method internally calls entity::Allocator::free, so it might return any of its
errors.
Sourcepub fn despawn_entities<'a, I>(&'a mut self, entities: I) -> Result<(), Error>
pub fn despawn_entities<'a, I>(&'a mut self, entities: I) -> Result<(), Error>
Removes all entities in the iterator from the world.
Also removes all of their components.
§Errors
The method internally calls entity::Allocator::free, so it might return any of its
errors.
Sourcepub fn get<T>(&self, entity: Entity) -> Option<&T>where
S: AsRef<ComponentVec<T>>,
pub fn get<T>(&self, entity: Entity) -> Option<&T>where
S: AsRef<ComponentVec<T>>,
Returns a reference to the entity’s component, or None if the entity does not have that
component.
Sourcepub fn get_mut<T>(&mut self, entity: Entity) -> Option<&mut T>where
S: AsMut<ComponentVec<T>>,
pub fn get_mut<T>(&mut self, entity: Entity) -> Option<&mut T>where
S: AsMut<ComponentVec<T>>,
Returns a mutable reference to an entity’s component, or None if the entity does not
have that component.
Sourcepub fn insert<T>(&mut self, entity: Entity, value: T) -> Option<T>where
S: AsMut<ComponentVec<T>>,
pub fn insert<T>(&mut self, entity: Entity, value: T) -> Option<T>where
S: AsMut<ComponentVec<T>>,
Adds a component to the entity.
If the entity already had that component its value is updated and the old value is
returned.
Examples found in repository?
22fn main() {
23 let mut world = world::World::<Storage>::new();
24
25 // Create entities with initial components.
26 // Either manually...
27 let player = world.spawn();
28 world.insert(player, Position { x: 0, y: 0 });
29 world.insert(player, Visible);
30 world.insert(player, Health(80));
31
32 // ...or by using the `spawn` macro...
33 let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
34 let _trap = checs::spawn!(world, Position { x: 2, y: 1 });
35
36 // ...or by using `spawn_with`.
37 let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));
38
39 let storage = world.storage_mut();
40
41 // Create a query over all entities that have `Position`, `Health`, and
42 // `Visible` components.
43 let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();
44
45 query.for_each(|(_, (_, h, _))| {
46 h.0 += 10;
47 });
48
49 println!();
50
51 let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();
52
53 query.for_each(|(e, (p, h, v))| {
54 println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
55 });
56}