Crates.io | xecs |
lib.rs | xecs |
version | 0.5.10 |
source | src |
created_at | 2021-08-22 20:11:53.703646 |
updated_at | 2022-04-08 17:31:01.585777 |
description | An Entity-Component-System library |
homepage | |
repository | https://github.com/xstater/xecs/ |
max_upload_size | |
id | 440834 |
size | 171,217 |
An Entity-Component-System library
// Define two components struct
// Component is Send + Sync + 'static
#[derive(Debug,Copy)]
struct Position{
x : f32,
y : f32
};
struct Hidden;
// create an empty world
let mut world = World::new();
// generate 10 entities
for _ in 0..10 {
let x = random();
lety = random();
// andomly generate the positions
world.create_entity()
.attach(Position { x,y });
}
// print all postions
for pos in world.query::<&Position>() {
print!("{:?}",pos)
}
// filter some entities need to be hidden
let ids = world.query::<&Position>()
.with_id()
.filter(|(_,_)|random())
.map(|(id,_)|id)
.collect::<Vec<_>>();
// attach hidden to id
for id in ids {
world.attach_component(id,Hidden);
}
// make a full-owning group to accelerate the query
world.make_group(full_owning::<Hidden,Position>());
// only print postions with id that is not hidden
for (id,data) in world.query::<&Position,Without<&Hidden>>() {
print!("{}:{:?}",id,data);
}
Entity in XECS is just an number ID.In XECS, it's just a
NonZeroUsize.
The ID is allocated from 1 by world automatically. The id=0
represents a recycled ID without any other flags through Option<EntityId>
.
When you call world.create_entity()
, an ID will be allocated automatically.
If you call world.remove_entity(id)
, this ID will be a pit. If the
next world.create_entity()
is called, it will allocate this ID to fill
the pit.Thanks to sparse set, it's still fast to
iterate all components no matter how random of ID
Because Component is just T : Send + Sync
.
World can use RwLock to
ensure the borrow check relations of all components.And World can also
be Send + Sync
.Therefore,the all other states of world can be guarded
by RwLock.So we can use world in concurrency environment by RwLock<World>
.
System is a Stream with World as Context. Because Stream is not stable in std, XECS use futures::Stream instead.
Because system is just an async trait, you need a wrapper of runtime from tokio or async-std