#ecs #game #associations #iteration #supporting #插入删除 #单类型

ecs-tiny

一个支持实体和组件插入/删除、关联和单类型迭代的轻量级ECS

4个版本 (2个破坏性更新)

0.3.0 2024年6月29日
0.2.0 2024年6月6日
0.1.1 2024年5月16日
0.1.0 2024年5月16日

#463 in 游戏开发

每月 50 次下载

MIT 协议

24KB
272

ecs-tiny

crates.io doc.rs

一个支持实体和组件插入/删除、关联和单类型迭代的轻量级ECS。

用法

// Create new ecs instance and inserts new entity:

let mut ecs = ecs_tiny::ECS::new();

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

// Register new component type:

ecs.register::<i32>().unwrap();
ecs.register::<()>().unwrap();

// Inserts new component associated with specified entity:

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

// Iterates over all components associated with specified entity:

for comp in ecs.iter_comp_mut_by_entity::<i32>(entity_key0).unwrap() {
    *comp += 1;
}

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

for comp in ecs.iter_comp_mut::<i32>().unwrap() {
    *comp += 1;
}

// Removes specified component:

ecs.remove_comp::<i32>(comp_key0).unwrap();

// Removes specified entity:

ecs.remove_entity(entity_key1).unwrap();

依赖项

~0.7–1MB
~14K SLoC