MCPcopy Create free account
hub / github.com/esengine/esengine / forEach

Method forEach

packages/core/src/ECS/Core/Query/CompiledQuery.ts:147–171  ·  view source on GitHub ↗

* 遍历所有匹配的实体及其组件 * * Iterate all matching entities with their components. * * @param callback 回调函数,接收实体和组件实例 | Callback receiving entity and component instances * * @example * ```typescript * query.forEach((entity, position, velocity) => { * position.x

(
        callback: (entity: Entity, ...components: InstanceTypes<T>) => void
    )

Source from the content-addressed store, hash-verified

145 * ```
146 */
147 public forEach(
148 callback: (entity: Entity, ...components: InstanceTypes<T>) => void
149 ): void {
150 const entities = this.entities;
151 const componentTypes = this._componentTypes;
152 const typeCount = componentTypes.length;
153
154 for (let i = 0, len = entities.length; i < len; i++) {
155 const entity = entities[i]!;
156 const components: Component[] = new Array(typeCount);
157
158 // 获取所有组件
159 for (let j = 0; j < typeCount; j++) {
160 const component = entity.getComponent(componentTypes[j]!);
161 if (!component) {
162 // 组件不存在,跳过这个实体
163 continue;
164 }
165 components[j] = component;
166 }
167
168 // 调用回调
169 callback(entity, ...(components as InstanceTypes<T>));
170 }
171 }
172
173 /**
174 * 遍历自指定 epoch 以来发生变更的实体

Callers 3

toArrayMethod · 0.95
mapMethod · 0.95
filterMethod · 0.95

Calls 2

callbackFunction · 0.85
getComponentMethod · 0.45

Tested by

no test coverage detected