Skip to content

Commit

Permalink
🎨 classic: Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
krispya committed May 27, 2024
1 parent f20b06b commit bc61ac8
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions packages/classic/src/entity/Entity.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
Expand Down

0 comments on commit bc61ac8

Please sign in to comment.