a tiny entity-component-system library
-
less dependencies
-
fast to build
-
easy to use
Hello World
// a function-system
add components into world
you can use spawn method to add any types that implemented Bundle or Component trait into world
use Command;
let mut world = new;
world.spawn;
world.spawn;
you can derive Bundle and Component trait easily
Component is just a tag, it could be implemented by any type
use Component;
all of the field of Bundle should implement Component
use Bundle;
Component defaults to the following type implementation
- usize,u8,u16,u32,u64
- isize,i8,i16,i32,i64
- (),bool,&'static str
Bundle defaults to all tuple implementations that contain only types thatComponent trait implemented
such as:
- (usize,&str)
query components in world
use Query directly
use Query;
use Command;
let mut world = new;
// add two `Bundle` into world
world.spawn;
world.spawn;
// create a `Query` to get all i32'a refence
let query = new;
for item in query
// this code will print:
// 12345
// 54321
or use system
use ;
use Command;
// use command to do spawn
// note: you cant use conflict Query in one system, or the program will panic when you add system into world
// a example of conflict Query: Query<&i32> and Query<&mut i32>
world.add_startup_syste;
world.add_startup_syste;
world.run_until;
Query is a type that receiver two generic
WorldFetch is used to fetch component in world
it could be the (im)mutable reference of Component,or a tuple that contains only WorldFetch
WorldFilter is used to fetch bundle in world
it could be
-
All or All<(Component1,Component1,...)> to filter bundle that contains all of components
-
OneOf or OneOf<(Component1,Component1,...)> to filter bundle that contains at least one of components
-
Not or Not<(Component1,Component1,...)> to filter bundle that doesnot contain any one of components
Example
-
Query<&i32>will query all components that containi32component, and give immutable references ofi32in iterator -
use
Query<&mut i32>to let iterator give mutable references ofi32in iterator -
use
Query<&i32,All<&str>>will query all components that containi32and&str, and give immutable references ofi32in iterator -
use
Query<&i32,AnyOf<(&str,MyComponent)>>will query all components that containi32and one of&strandMyComponent, and give immutable references ofi32in iterator -
use
Query<&i32,All<&str>>will query all components that containi32and dont contain&str, and give immutable references ofi32in iterator
Iterator
you can use for to get the result of the query
the type of fetch is WorldFetch
for fetch in query
you can use for with .into_eiter() method to get the result of the query,and the Entity of result
for e in query.into_eiter
the type of e is EBundle
you can deref e to get the result of query, use .entity() method to get the Entity of result
Entity could be used to remove the bundle that queried
commands.remove;
resources
Resources are stored in the world type by type
use ResManager;
let mut world = new;
// get resources usize, and init it to 1
world..get_or_init;
assert_eq!;
System
only function-system supported in this version
all types that implemented SystemParm trait could be the parm of the function-system
follow types impled SystemParm trait
you can find them in tecs::world mod
| type | usage | note |
|---|---|---|
| Res | to get resources of type T in world | cant use same Res in one system |
| Resources | to get any type of resources in world | cant use be used with any Res in one system |
| Query<F,Q> | to query components in world | cant use conflict query in one system, like Query<&T> and Query<&mut T> |
| Commands | to add and remove bundle into world | use spawn_many() method to spawn many bundle with the same type quickly |
to run a system,you need to add system into world by using .add_system() method or .add_startup_system() method fist
- all startup_systems will only run once
- systems run pre loop
to run systems in world,you can
-
use
.startup()method to run all startup_systems -
use
.run_once()method to run all systems once(dont include startup_systems) -
use
.run()method to run all systems many times, this method will not return. -
use
.run_until(f)method to run all systems many times, the loop will be break whenfreturntrue;