#ecs #game #associations #supporting #iteration #i32 #insertion-removal

ecs-tiny

A minimal ECS supporting entity and component insertion/removal, association, and single-type iteration

3 unstable releases

new 0.2.0 Jun 6, 2024
0.1.1 May 16, 2024
0.1.0 May 16, 2024

#375 in Game dev

Download history 227/week @ 2024-05-16 15/week @ 2024-05-23

242 downloads per month

MIT license

25KB
172 lines

ecs-tiny

crates.io doc.rs

A minimal ECS supporting entity and component insertion/removal, association, and single-type iteration.

Usages

#[derive(Debug, Clone, PartialEq, Eq, strum_macros::EnumDiscriminants)]
#[strum_discriminants(name(CompKind))]
#[strum_discriminants(derive(Hash))]
enum Comp {
    I32(i32),
    Unit(()),
}

// Create new ecs instance and inserts new entity:

let mut ecs = ecs_tiny::ECS::<Comp, CompKind>::new();

let entity_key0 = ecs.insert_entity();
let entity_key1 = ecs.insert_entity();

// Inserts new component associated with specified entity:

let comp_key0 = ecs.insert_comp(entity_key0, Comp::I32(42)).unwrap();
let comp_key1 = ecs.insert_comp(entity_key0, Comp::I32(63)).unwrap();
let comp_key2 = ecs.insert_comp(entity_key1, Comp::I32(42)).unwrap();
let comp_key3 = ecs.insert_comp(entity_key1, Comp::Unit(())).unwrap();

// Iterates over all components associated with specified entity:

for comp in ecs.iter_comp_mut_by_entity(entity_key0, CompKind::I32).unwrap() {
    if let Comp::I32(comp) = comp {
        *comp += 1;
    }
}

// Iterates over all components of specified type (single type only):

for comp in ecs.iter_comp_mut(CompKind::I32).unwrap() {
    if let Comp::I32(comp) = comp {
        *comp += 1;
    }
}

// Removes specified component:

ecs.remove_comp(comp_key0).unwrap();

// Removes specified entity:

ecs.remove_entity(entity_key1).unwrap();

Dependencies

~1MB
~15K SLoC