From bc61ac8f86d995802f68537cd074fe5e2ea5af9d Mon Sep 17 00:00:00 2001 From: Kris Baumgartner Date: Sun, 26 May 2024 19:33:44 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20classic:=20Entity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/classic/src/entity/Entity.ts | 40 +++++++++++++++++---------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/classic/src/entity/Entity.ts b/packages/classic/src/entity/Entity.ts index 1a9f0ea1..d1ee7f18 100644 --- a/packages/classic/src/entity/Entity.ts +++ b/packages/classic/src/entity/Entity.ts @@ -1,4 +1,10 @@ -import { queries, query, queryAddEntity, queryCheckEntity, queryRemoveEntity } from '../query/Query.js'; +import { + queries, + query, + queryAddEntity, + queryCheckEntity, + queryRemoveEntity, +} from '../query/Query.js'; import { resizeWorlds, worlds } from '../world/World.js'; import { $localEntities, @@ -12,7 +18,14 @@ import { TODO } from '../utils/types.js'; import { $notQueries, $queries } from '../query/symbols.js'; import { Pair, Wildcard } from '../relation/Relation.js'; import { addComponent, defineComponent, removeComponent } from '../component/Component.js'; -import { $autoRemoveSubject, $isPairComponent, $onTargetRemoved, $pairTarget, $relation, $relationTargetEntities } from '../relation/symbols.js'; +import { + $autoRemoveSubject, + $isPairComponent, + $onTargetRemoved, + $pairTarget, + $relation, + $relationTargetEntities, +} from '../relation/symbols.js'; let defaultSize = 100000; @@ -83,14 +96,14 @@ export const flushRemovedEntities = (world: World) => { }; // TODO: definePrefab? -export const Prefab = defineComponent() -export const addPrefab = (world:World) => { +export const Prefab = defineComponent(); +export const addPrefab = (world: World) => { const eid = addEntity(world); addComponent(world, Prefab, eid); return eid; -} +}; /** * Adds a new entity to the specified world. @@ -140,32 +153,30 @@ export const removeEntity = (world: World, eid: number) => { // check to see if this entity is a relation target at all if (world[$relationTargetEntities].has(eid)) { - // if it is, iterate over all subjects with any relation to this eid for (const subject of query(world, [Pair(Wildcard, eid)])) { // TODO: can we avoid this check? (subject may have been removed already) if (!entityExists(world, subject)) { - continue + continue; } // remove the wildcard association with the subject for this entity - removeComponent(world, Pair(Wildcard, eid), subject) + removeComponent(world, Pair(Wildcard, eid), subject); // iterate all relations that the subject has to this entity for (const component of world[$entityComponents].get(subject)!) { - // TODO: can we avoid this check? (subject may have been removed by this loop already) if (!component[$isPairComponent] || !entityExists(world, subject)) { - continue + continue; } - const relation = component[$relation] + const relation = component[$relation]; if (component[$pairTarget] === eid) { - removeComponent(world, component, subject) + removeComponent(world, component, subject); if (relation[$autoRemoveSubject]) { - removeEntity(world, subject) + removeEntity(world, subject); } if (relation[$onTargetRemoved]) { - relation[$onTargetRemoved](world, subject, eid) + relation[$onTargetRemoved](world, subject, eid); } } } @@ -178,7 +189,6 @@ export const removeEntity = (world: World, eid: number) => { queryRemoveEntity(world, q, eid); }); - // Free the entity if (world[$manualEntityRecycling]) recycled.push(eid); else removed.push(eid);