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,
and in some use cases you will need to use downcast_ref
or downcast_mut
.
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.
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 partmaps allocs (they will be equal in size).
use Entities;
let mut entities = new;
Demonstrating use
use Any;
use ;
use HashMap;
use ;
// 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 part using
// a free slot
let entity_1 = entities.get_free_slot.unwrap;
entities.add_part;
// And you can add more parts to it
// The entity is only considered newly created if no parts existed before
entities.add_part;
// To add another entity you need another free slot
let entity_2 = entities.get_free_slot.unwrap;
entities.add_part;
entities.add_part;
// To get access to a part belonging to an entity you need
// first to get the partmap created for the part type
// This requires the part type signature
if entities.
// You can check if an entity contains a part with the type signature
if entities.
assert_eq!;
// Make a system of some form that takes a `RefMut<PartMap>`
some_system;
// Or create a system that fetches the parts for only the entities
// you are interested in.
second_system;
// Or a system handles the `Entities` container directly
other_system;