Tiny ECS
The intention of this crate is that a basic ECS is provided, where you will be required to exercise a little additional control. This is somewhat due to some limitations, and also due to trying to maintain as little overhead as possible - this means no unneccesary copies/clones.
Where most other ECS crates provide a mechanism for inserting "systems" in to the ECS to run against entities, this one leaves it out - you can think of it as a "system for entity/components". You will need to create external systems; these can be a function, a loop, or anything else.
The basis of this ECS is the use of bitmasks
. Each entity ID is in
practice an internal index number in to an array which contains bitmasks.
The bitmasks themselves keep track of what components the entity has.
For the most part, bitmasks are handled for you, and some helper methods
are available to hide their use, but there are also methods to get the
bitmask for any ID if you are inclined to do some manual management.
Note: defaults to bitmask of u64
size (so 64 components). If you
need a smaller mask the feature bitmask_max_32
can be enabled for
mask of u32
size.
Attention: borrows of ComponentMap
are checked at runtime.
Examples
Init with a capacity
This is good to do if you know the size required as it will prevent many reallocs/moves as data is added. This affects both the entity and component map allocs (they will be equal in length).
use Entities;
let mut entities = new;
Demonstrating use
use Entities;
// These are the "components" we will use
// Initialize the Entity collection
let mut entities = new;
// To create an entity you only need to add the first component using
// a free slot
let entity_1 = entities.add_entity;
assert!;
// And you can add more components to it
// The entity is only considered newly created if no components existed before
entities.add_part;
// To add another entity you need another free slot
let entity_2 = entities.add_entity;
assert!;
assert!;
Access an entities part of type <T>
# use ;
#
# let mut entities = new;
# let entity_1 = entities.add_entity;
# assert!;
// To get access to a part belonging to an entity you need
// first to get the component map created for the part type
// You need to 'anchor' this with a let or the ref is
// dropped before you can use it
let mut components = entities
.
.unwrap;
// You can then use the part by getting a reference
let mut part = components.get_part_mut.unwrap;
assert_eq!;
Check if Entity
contains a part type + remove part
# use Entities;
#
# let mut entities = new;
# let entity_1 = entities.add_entity;
# assert!;
// You can check if an entity contains a part with the type signature
if entities.
assert_eq!;
A system that uses an get_mut()
# use ;
#
# let mut entities = new;
# let entity_1 = entities.add_entity;
# assert!;
// Make a system of some form that takes a `ComponentMap<T>` arg
# let mut components = entities..unwrap;
some_system;
Get components for an entity ID list
# use ;
#
# let mut entities = new;
# let entity_1 = entities.add_entity;
# assert!;
// A system that fetches the components for only the entities you are require
# let mut components = entities..unwrap;
second_system;
A more complex system using ComponentMaps directly
# use ;
#
#
# let mut entities = new;
# let entity_1 = entities.add_entity;
# assert!;
# assert!;
// Or a system handles the `Entities` container directly
other_system;