Skip to content

Advanced toecs

toyboot4e edited this page Mar 25, 2022 · 6 revisions

Advanced toecs

User ergonomics

Non-system function should prefer &ComponentPool<T> to &Comp<T> or &CompMut<T>

Both Comp<T> and CompMut<T> implements AsRef<ComponentPool<T>>, so this is a useful tip:

pub fn walk_entity(
    entity: Entity,
    pos: mdl::Pos,
    dir: mdl::Dir8,
    body: &mut ComponentPool<mdl::Body>,
    view_body: &mut ComponentPool<ViewBody>,
    stage: &res::Stage,
) {
    /* ~~~~ */
}

API over world data

Entity hierarchy API automatically borrows their context automatically:

#[derive(GatBorrowWorld, Debug)]
pub struct HieMut<'w> {
    root: Res<'w, tfm::RootNode>,
    node: CompMut<'w, tfm::Node>,
}

pub fn update_hierarchy_system(
    hie: &HieMut,
    /* ~~~~ */
) { /* ~~~~ */ }

Named component set

A.k.a. component bundle:

#[derive(Debug, Default, ComponentSet)]
pub struct NodeSet {
    pub node: Node,
    pub local: tfm::LocalTransform,
    pub global: tfm::GlobalTransform,
}

world.insert_set(entity, NodeSet { /* ~~ */ }

Component set is a wide term

#[derive(Debug, Componnet)]
A;

// set of component sets is a component set
world.insert_set(entity, ((
    // set of components is a component set
    (A, A ,A),
    // component is a component set
    A,
));

Advanced systems

Insert resources returned by a system

let resource = world.run(system);
world.insert(resource);

Operate on the world from a System

Use Commands.

Systems that take user parameters

Use const generics:

pub fn render_map_layers<const I1: i32, const I2: i32>(mut draw: Draw, vp: Res<Camera2d>, stage: Res<Stage>) {
    /* ~~~~ */
}

world.run(render_map_layers::<0, 10>);

Make use of World::borrow:

pub fn ctrl_entity_system(
    entity,
    (input, vi, mut body, mut view_body, stage, interact, mut cutscene): (
        Res<Input>,
        Res<res::VInput>,
        CompMut<mdl::Body>,
        CompMut<view::actor::ViewBody>,
        Res<res::Stage>,
        Comp<mdl::Interact>,
        ResMut<CutsceneState>,
    ),
) { /* ~~~ */

let entity = world.spawn( /* ~~~ */ ):
ctrl_entity_system(entity, &mut world);