有没有办法从 Rust legion ECS 系统中查看(并从中获取实体)世界?

Is there a way to see (and get an Entity from) the world from a rust legion ECS system?

我正在学习使用 Specs ECS, and I'm trying to implement it using the legion ECS 的 Rust 教程。我喜欢军团,一切都很顺利,直到我遇到问题。

我不确定如何表述我的问题。我想做的是创建一个系统来迭代每个实体,例如ComponentA 和 ComponentB,但这也会检查实体是否有 ComponentC,如果是的话,会做一些特殊的事情。

我可以使用规范(示例代码)这样​​做:

// Uses Specs
pub struct SystemA {}

impl<'a> System<'a> for SystemA {
    type SystemData = ( Entities<'a>,
                        WriteStorage<'a, ComponentA>, 
                        ReadStorage<'a, ComponentB>,
                        ReadStorage<'a, ComponentC>);

    fn run(&mut self, data : Self::SystemData) {
        let (entities, mut compA, compB, compC) = data;

        // Finding all entities with ComponentA and ComponentB
        for (ent, compA, compB) in (&entities, &mut compA, &compB).join() {
            // Do stuff with compA and compB

            // Check if Entity also has ComponentC
            let c : Option<&ComponentC> = compC.get(ent);

            if let Some(c) = c {
                // Do something special with entity if it also has ComponentC
            }
        }
    }
}

我很难将其转化为使用 legion(目前使用最新的 0.4.0 版本)。我不知道如何获取当前实体拥有的其他组件。这是我的代码:

#[system(for_each)]
pub fn systemA(entity: &Entity, compA: &mut compA, compB: &mut ComponentB) {
    // Do stuff with compA and compB

    // How do I check if entity has compC here?

}

上述系统中的实体仅包含其 ID。我如何在没有世界的情况下访问该实体的组件列表?或者有没有办法在军团系统中访问世界?或任何其他方式来实现与 Specs 版本相同的功能?

谢谢!

您可以使用 Option<...> 作为可选组件。

#[system(for_each)]
pub fn systemA(entity: &Entity, compA: &mut compA, compB: &mut ComponentB, compC: Option<&ComponentC>) {
    ...
    if let Some(compC) = compC {
        // this entity has compC
        ...