JacobHawley's picture
download
raw
53.7 kB
diff --git a/packages/core/src/deferred/deferred.ts b/packages/core/src/deferred/deferred.ts new file mode 100644 index 0000000..5efc9f2 --- /dev/null +++ b/packages/core/src/deferred/deferred.ts @@ -0,0 +1,819 @@ +import { $internal } from '../common'; +import { destroyEntity } from '../entity/entity'; +import type { Entity } from '../entity/types'; +import { allocateEntity, releaseEntity } from '../entity/utils/entity-index'; +import { getRelationTargets, hasRelationPair } from '../relation/relation'; +import type { Relation, RelationPair } from '../relation/types'; +import { isRelationPair } from '../relation/utils/is-relation'; +import { addTrait, hasTrait, removeTrait, setTrait } from '../trait/trait'; +import type { ConfigurableTrait, Trait } from '../trait/types'; +import type { World } from '../world/types'; +import type { CommandBuffer, DeferredCommands } from './types'; + +export function createCommandBuffer(): CommandBuffer { + return { + commands: [], + spawnedEntities: new Set(), + destroyedEntities: new Set(), + }; +} + +export function createDeferredCommands(world: World, getBuffer: () => CommandBuffer): DeferredCommands { + return { + spawn(...traits: ConfigurableTrait[]): Entity { + const buffer = getBuffer(); + const ctx = world[$internal]; + const entity = allocateEntity(ctx.entityIndex); + buffer.spawnedEntities.add(entity); + buffer.commands.push({ + type: 'spawn', + entity, + traits, + }); + return entity; + }, + + destroy(entity: Entity): void { + const buffer = getBuffer(); + buffer.destroyedEntities.add(entity); + buffer.commands.push({ + type: 'destroy', + entity, + }); + }, + + add(entity: Entity, ...traits: ConfigurableTrait[]): void { + const buffer = getBuffer(); + buffer.commands.push({ + type: 'add', + entity, + traits, + }); + }, + + remove(entity: Entity, ...traits: (Trait | RelationPair)[]): void { + const buffer = getBuffer(); + buffer.commands.push({ + type: 'remove', + entity, + traits, + }); + }, + + addExclusive(entity: Entity, relationPair: RelationPair): void { + const buffer = getBuffer(); + const pairCtx = relationPair[$internal]; + const relation = pairCtx.relation; + const newTarget = pairCtx.target; + + const existingTargets = getRelationTargets(world, relation, entity); + + for (const existingTarget of existingTargets) { + if (existingTarget !== newTarget) { + buffer.commands.push({ + type: 'remove', + entity, + traits: [relation(existingTarget)], + }); + } + } + + const projectedState = getProjectedEntityState(buffer, entity, world); + if (projectedState) { + for (const traitConfig of projectedState.traitsToAdd) { + if (isRelationPair(traitConfig)) { + const addedPairCtx = traitConfig[$internal]; + if (addedPairCtx.relation === relation && addedPairCtx.target !== newTarget) { + buffer.commands.push({ + type: 'remove', + entity, + traits: [traitConfig], + }); + } + } + } + } + + if (!existingTargets.includes(newTarget as Entity)) { + buffer.commands.push({ + type: 'add', + entity, + traits: [relationPair], + }); + } + }, + + flush(): void { + const buffer = getBuffer(); + flushCommandBuffer(world, buffer); + }, + }; +} + +type ProjectedEntityState = { + traitsToAdd: ConfigurableTrait[]; + traitsToRemove: (Trait | RelationPair)[]; + shouldDestroy: boolean; + isSpawned: boolean; + spawnTraits: ConfigurableTrait[]; +}; + +export function getProjectedEntityState( + buffer: CommandBuffer, + entity: Entity, + world: World +): ProjectedEntityState | null { + const { commands, spawnedEntities, destroyedEntities } = buffer; + + if (commands.length === 0) { + return null; + } + + const isSpawned = spawnedEntities.has(entity); + const isCancelledSpawn = isSpawned && destroyedEntities.has(entity); + + if (isCancelledSpawn) { + return { + traitsToAdd: [], + traitsToRemove: [], + shouldDestroy: true, + isSpawned: true, + spawnTraits: [], + }; + } + + const ops: ProjectedEntityState = { + traitsToAdd: [], + traitsToRemove: [], + shouldDestroy: false, + isSpawned, + spawnTraits: [], + }; + + for (let i = 0; i < commands.length; i++) { + const cmd = commands[i]; + if (cmd.entity !== entity) continue; + + switch (cmd.type) { + case 'spawn': + ops.spawnTraits = [...cmd.traits]; + break; + + case 'destroy': + ops.shouldDestroy = true; + ops.traitsToAdd = []; + ops.traitsToRemove = []; + break; + + case 'add': { + if (ops.shouldDestroy) continue; + + for (const traitConfig of cmd.traits) { + const trait = Array.isArray(traitConfig) ? traitConfig[0] : traitConfig; + const isRelPair = isRelationPair(traitConfig); + + const removeIdx = ops.traitsToRemove.findIndex((t) => { + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(traitConfig, t); + } + return t === trait; + }); + if (removeIdx !== -1) { + ops.traitsToRemove.splice(removeIdx, 1); + } + + const existingIdx = ops.traitsToAdd.findIndex((t) => { + const existingTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(traitConfig, t); + } + return existingTrait === trait; + }); + + if (existingIdx !== -1) { + ops.traitsToAdd[existingIdx] = traitConfig; + } else { + ops.traitsToAdd.push(traitConfig); + } + } + break; + } + + case 'remove': { + if (ops.shouldDestroy) continue; + + for (const trait of cmd.traits) { + const isRelPair = isRelationPair(trait); + + if (isRelPair) { + const pairCtx = trait[$internal]; + if (pairCtx.target === '*') { + const relation = pairCtx.relation; + ops.traitsToAdd = ops.traitsToAdd.filter((t) => { + if (isRelationPair(t)) { + return t[$internal].relation !== relation; + } + const baseTrait = Array.isArray(t) ? t[0] : t; + const traitCtx = baseTrait[$internal]; + return traitCtx.relation !== relation; + }); + ops.spawnTraits = ops.spawnTraits.filter((t) => { + if (isRelationPair(t)) { + return t[$internal].relation !== relation; + } + const baseTrait = Array.isArray(t) ? t[0] : t; + const traitCtx = baseTrait[$internal]; + return traitCtx.relation !== relation; + }); + } + } + + const addIdx = ops.traitsToAdd.findIndex((t) => { + const addedTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + return addedTrait === trait; + }); + + if (addIdx !== -1) { + ops.traitsToAdd.splice(addIdx, 1); + } + + if (!ops.traitsToRemove.some((t) => { + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + return t === trait; + })) { + ops.traitsToRemove.push(trait); + } + } + break; + } + } + } + + return ops; +} + +function areSameRelationPair(a: RelationPair, b: RelationPair): boolean { + const aCtx = a[$internal]; + const bCtx = b[$internal]; + return aCtx.relation === bCtx.relation && aCtx.target === bCtx.target; +} + +function getMergedProjectedState( + world: World, + entity: Entity +): ProjectedEntityState | null { + const ctx = world[$internal]; + const allBuffers = [...ctx.commandBufferStack, ctx.currentCommandBuffer]; + + let mergedState: ProjectedEntityState | null = null; + + for (const buffer of allBuffers) { + const state = getProjectedEntityState(buffer, entity, world); + if (!state) continue; + + if (!mergedState) { + mergedState = { + traitsToAdd: [...state.traitsToAdd], + traitsToRemove: [...state.traitsToRemove], + shouldDestroy: state.shouldDestroy, + isSpawned: state.isSpawned, + spawnTraits: [...state.spawnTraits], + }; + } else { + for (const t of state.traitsToAdd) { + const trait = Array.isArray(t) ? t[0] : t; + const isRelPair = isRelationPair(t); + const existsInAdd = mergedState.traitsToAdd.some((existing) => { + if (isRelPair && isRelationPair(existing)) { + return areSameRelationPair(t, existing); + } + const existingTrait = Array.isArray(existing) ? existing[0] : existing; + return !isRelationPair(existing) && existingTrait === trait; + }); + if (!existsInAdd) { + mergedState.traitsToAdd.push(t); + } + } + for (const t of state.traitsToRemove) { + const isRelPair = isRelationPair(t); + const existsInRemove = mergedState.traitsToRemove.some((existing) => { + if (isRelPair && isRelationPair(existing)) { + return areSameRelationPair(t, existing); + } + return !isRelationPair(existing) && existing === t; + }); + if (!existsInRemove) { + mergedState.traitsToRemove.push(t); + } + } + if (state.shouldDestroy) { + mergedState.shouldDestroy = true; + } + if (state.isSpawned) { + mergedState.isSpawned = true; + mergedState.spawnTraits = [...state.spawnTraits]; + } + } + } + + return mergedState; +} + +export function projectedHasTrait( + world: World, + entity: Entity, + trait: Trait | RelationPair +): boolean | null { + const state = getMergedProjectedState(world, entity); + if (!state) return null; + + if (state.shouldDestroy) { + return false; + } + + const isRelPair = isRelationPair(trait); + + if (isRelPair) { + const pairCtx = trait[$internal]; + const relation = pairCtx.relation; + + for (const removeTrait of state.traitsToRemove) { + if (isRelationPair(removeTrait)) { + const removeCtx = removeTrait[$internal]; + if (removeCtx.relation === relation) { + if (removeCtx.target === '*' || removeCtx.target === pairCtx.target) { + const readdedLater = state.traitsToAdd.some((t) => { + if (isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + return false; + }); + if (!readdedLater) { + return false; + } + } + } + } + } + } else { + const isBeingRemoved = state.traitsToRemove.some((t) => { + if (isRelationPair(t)) return false; + return t === trait; + }); + if (isBeingRemoved) { + const readdedLater = state.traitsToAdd.some((t) => { + const addedTrait = Array.isArray(t) ? t[0] : t; + if (isRelationPair(t)) return false; + return addedTrait === trait; + }); + if (!readdedLater) { + return false; + } + } + } + + const isBeingAdded = state.traitsToAdd.some((t) => { + const addedTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + if (isRelationPair(t)) return false; + return addedTrait === trait; + }); + if (isBeingAdded) { + return true; + } + + if (state.isSpawned) { + const inSpawnTraits = state.spawnTraits.some((t) => { + const spawnTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + if (isRelationPair(t)) return false; + return spawnTrait === trait; + }); + return inSpawnTraits; + } + + return null; +} + +export function projectedGetTrait( + world: World, + entity: Entity, + trait: Trait | RelationPair +): { hasProjection: boolean; value: unknown } { + const state = getMergedProjectedState(world, entity); + if (!state) return { hasProjection: false, value: undefined }; + + if (state.shouldDestroy) { + return { hasProjection: true, value: undefined }; + } + + const isRelPair = isRelationPair(trait); + + if (isRelPair) { + const pairCtx = trait[$internal]; + const relation = pairCtx.relation; + + for (const removeTrait of state.traitsToRemove) { + if (isRelationPair(removeTrait)) { + const removeCtx = removeTrait[$internal]; + if (removeCtx.relation === relation) { + if (removeCtx.target === '*' || removeCtx.target === pairCtx.target) { + const readdedLater = state.traitsToAdd.find((t) => { + if (isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + return false; + }); + if (!readdedLater) { + return { hasProjection: true, value: undefined }; + } + } + } + } + } + } else { + const isBeingRemoved = state.traitsToRemove.some((t) => { + if (isRelationPair(t)) return false; + return t === trait; + }); + if (isBeingRemoved) { + const readdedConfig = state.traitsToAdd.find((t) => { + const addedTrait = Array.isArray(t) ? t[0] : t; + if (isRelationPair(t)) return false; + return addedTrait === trait; + }); + if (!readdedConfig) { + return { hasProjection: true, value: undefined }; + } + } + } + + for (let i = state.traitsToAdd.length - 1; i >= 0; i--) { + const config = state.traitsToAdd[i]; + const configTrait = Array.isArray(config) ? config[0] : config; + + if (isRelPair && isRelationPair(config)) { + if (areSameRelationPair(trait, config)) { + const params = config[$internal].params; + if (params) { + const baseTrait = trait[$internal].relation[$internal].trait; + const defaults = getSchemaDefaultsForTrait(baseTrait); + return { hasProjection: true, value: { ...defaults, ...params } }; + } + return { hasProjection: true, value: {} }; + } + } else if (!isRelationPair(config) && configTrait === trait) { + if (Array.isArray(config) && config[1]) { + const defaults = getSchemaDefaultsForTrait(trait as Trait); + return { hasProjection: true, value: { ...defaults, ...config[1] } }; + } + const defaults = getSchemaDefaultsForTrait(trait as Trait); + return { hasProjection: true, value: defaults }; + } + } + + if (state.isSpawned) { + for (let i = state.spawnTraits.length - 1; i >= 0; i--) { + const config = state.spawnTraits[i]; + const configTrait = Array.isArray(config) ? config[0] : config; + + if (isRelPair && isRelationPair(config)) { + if (areSameRelationPair(trait, config)) { + const params = config[$internal].params; + if (params) { + const baseTrait = trait[$internal].relation[$internal].trait; + const defaults = getSchemaDefaultsForTrait(baseTrait); + return { hasProjection: true, value: { ...defaults, ...params } }; + } + return { hasProjection: true, value: {} }; + } + } else if (!isRelationPair(config) && configTrait === trait) { + if (Array.isArray(config) && config[1]) { + const defaults = getSchemaDefaultsForTrait(trait as Trait); + return { hasProjection: true, value: { ...defaults, ...config[1] } }; + } + const defaults = getSchemaDefaultsForTrait(trait as Trait); + return { hasProjection: true, value: defaults }; + } + } + return { hasProjection: true, value: undefined }; + } + + return { hasProjection: false, value: undefined }; +} + +function getSchemaDefaultsForTrait(trait: Trait): Record<string, unknown> { + const traitCtx = trait[$internal]; + if (traitCtx.type === 'tag') { + return {}; + } + const schema = (trait as { schema?: Record<string, unknown> }).schema; + if (!schema || typeof schema === 'function') { + return {}; + } + const result: Record<string, unknown> = {}; + for (const key in schema) { + result[key] = schema[key]; + } + return result; +} + +export function flushCommandBuffer(world: World, buffer: CommandBuffer): void { + const { commands, spawnedEntities, destroyedEntities } = buffer; + + if (commands.length === 0) { + return; + } + + try { + const ctx = world[$internal]; + const entitiesToCancelSpawn = new Set<Entity>(); + + for (const entity of spawnedEntities) { + if (destroyedEntities.has(entity)) { + entitiesToCancelSpawn.add(entity); + } + } + + type EntityOps = { + traitsToAdd: ConfigurableTrait[]; + traitsToRemove: (Trait | RelationPair)[]; + shouldDestroy: boolean; + isSpawned: boolean; + spawnTraits: ConfigurableTrait[]; + }; + + const entityOperations = new Map<Entity, EntityOps>(); + + function getOps(entity: Entity): EntityOps { + let ops = entityOperations.get(entity); + if (!ops) { + ops = { + traitsToAdd: [], + traitsToRemove: [], + shouldDestroy: false, + isSpawned: spawnedEntities.has(entity), + spawnTraits: [], + }; + entityOperations.set(entity, ops); + } + return ops; + } + + for (let i = 0; i < commands.length; i++) { + const cmd = commands[i]; + const entity = cmd.entity; + + if (entitiesToCancelSpawn.has(entity)) { + continue; + } + + const ops = getOps(entity); + + switch (cmd.type) { + case 'spawn': + ops.spawnTraits = cmd.traits; + break; + + case 'destroy': + ops.shouldDestroy = true; + ops.traitsToAdd = []; + ops.traitsToRemove = []; + break; + + case 'add': { + if (ops.shouldDestroy) continue; + + for (const traitConfig of cmd.traits) { + const trait = Array.isArray(traitConfig) ? traitConfig[0] : traitConfig; + const isRelPair = isRelationPair(traitConfig); + + const removeIdx = ops.traitsToRemove.findIndex((t) => { + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(traitConfig, t); + } + return t === trait; + }); + if (removeIdx !== -1) { + ops.traitsToRemove.splice(removeIdx, 1); + } + + const existingIdx = ops.traitsToAdd.findIndex((t) => { + const existingTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(traitConfig, t); + } + return existingTrait === trait; + }); + + if (existingIdx !== -1) { + ops.traitsToAdd[existingIdx] = traitConfig; + } else { + ops.traitsToAdd.push(traitConfig); + } + } + break; + } + + case 'remove': { + if (ops.shouldDestroy) continue; + + for (const trait of cmd.traits) { + const isRelPair = isRelationPair(trait); + + if (isRelPair) { + const pairCtx = trait[$internal]; + if (pairCtx.target === '*') { + const relation = pairCtx.relation; + ops.traitsToAdd = ops.traitsToAdd.filter((t) => { + if (isRelationPair(t)) { + return t[$internal].relation !== relation; + } + const baseTrait = Array.isArray(t) ? t[0] : t; + const traitCtx = baseTrait[$internal]; + return traitCtx.relation !== relation; + }); + } + } + + const addIdx = ops.traitsToAdd.findIndex((t) => { + const addedTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + return addedTrait === trait; + }); + + if (addIdx !== -1) { + ops.traitsToAdd.splice(addIdx, 1); + } + + const alreadyInRemove = ops.traitsToRemove.some((t) => { + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + return t === trait; + }); + if (!alreadyInRemove) { + ops.traitsToRemove.push(trait); + } + } + break; + } + } + } + + for (const entity of entitiesToCancelSpawn) { + ctx.entityTraits.delete(entity); + releaseEntity(ctx.entityIndex, entity); + } + + for (const [entity, ops] of entityOperations) { + if (entitiesToCancelSpawn.has(entity)) { + continue; + } + + if (ops.isSpawned && !ops.shouldDestroy) { + ctx.entityTraits.set(entity, new Set()); + + for (const query of ctx.notQueries) { + const match = query.check(world, entity); + if (match) query.add(entity); + } + + const allTraits = [...ops.spawnTraits]; + for (const traitConfig of ops.traitsToAdd) { + const trait = Array.isArray(traitConfig) ? traitConfig[0] : traitConfig; + const isRelPair = isRelationPair(traitConfig); + const existingIdx = allTraits.findIndex((t) => { + const existingTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(traitConfig, t); + } + return existingTrait === trait; + }); + if (existingIdx !== -1) { + allTraits[existingIdx] = traitConfig; + } else { + allTraits.push(traitConfig); + } + } + + for (const trait of ops.traitsToRemove) { + const isRelPair = isRelationPair(trait); + const idx = allTraits.findIndex((t) => { + const existingTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + return existingTrait === trait; + }); + if (idx !== -1) { + allTraits.splice(idx, 1); + } + } + + if (allTraits.length > 0) { + addTrait(world, entity, ...allTraits); + } + } else if (!ops.isSpawned && !ops.shouldDestroy) { + if (!world.has(entity)) { + continue; + } + + const finalAdds: ConfigurableTrait[] = []; + const finalRemoves: (Trait | RelationPair)[] = []; + + for (const traitConfig of ops.traitsToAdd) { + const trait = Array.isArray(traitConfig) ? traitConfig[0] : traitConfig; + const isRelPair = isRelationPair(traitConfig); + const hadBefore = isRelPair + ? hasRelationPair(world, entity, traitConfig) + : hasTrait(world, entity, trait as Trait); + const isBeingRemoved = ops.traitsToRemove.some((t) => { + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(traitConfig, t); + } + return t === trait; + }); + + if (!hadBefore && !isBeingRemoved) { + finalAdds.push(traitConfig); + } else if (hadBefore && !isBeingRemoved) { + if (Array.isArray(traitConfig) && traitConfig[1]) { + setTrait(world, entity, trait, traitConfig[1], false); + } + } + } + + for (const trait of ops.traitsToRemove) { + const isRelPair = isRelationPair(trait); + + if (isRelPair) { + const pairCtx = trait[$internal]; + if (pairCtx.target === '*') { + const relation = pairCtx.relation; + const existingTargets = getRelationTargets(world, relation, entity); + for (const existingTarget of existingTargets) { + const specificPair = relation(existingTarget); + const isBeingAdded = ops.traitsToAdd.some((t) => { + if (isRelationPair(t)) { + return areSameRelationPair(specificPair, t); + } + return false; + }); + if (!isBeingAdded) { + finalRemoves.push(specificPair); + } + } + continue; + } + } + + const entityHasTrait = isRelPair + ? hasRelationPair(world, entity, trait) + : hasTrait(world, entity, trait as Trait); + const isBeingAdded = ops.traitsToAdd.some((t) => { + const addedTrait = Array.isArray(t) ? t[0] : t; + if (isRelPair && isRelationPair(t)) { + return areSameRelationPair(trait, t); + } + return addedTrait === trait; + }); + + if (entityHasTrait && !isBeingAdded) { + finalRemoves.push(trait); + } + } + + if (finalRemoves.length > 0) { + removeTrait(world, entity, ...finalRemoves); + } + + if (finalAdds.length > 0) { + addTrait(world, entity, ...finalAdds); + } + } else if (ops.shouldDestroy && !ops.isSpawned) { + if (entity === ctx.worldEntity) { + throw new Error('Koota: Cannot destroy the world entity through deferred commands.'); + } + if (world.has(entity)) { + destroyEntity(world, entity); + } + } + } + } finally { + buffer.commands.length = 0; + buffer.spawnedEntities.clear(); + buffer.destroyedEntities.clear(); + } +} diff --git a/packages/core/src/deferred/index.ts b/packages/core/src/deferred/index.ts new file mode 100644 index 0000000..e4d153c --- /dev/null +++ b/packages/core/src/deferred/index.ts @@ -0,0 +1,2 @@ +export { createCommandBuffer, createDeferredCommands, flushCommandBuffer, projectedHasTrait, projectedGetTrait, getProjectedEntityState } from './deferred'; +export type { CommandBuffer, DeferredCommands } from './types'; diff --git a/packages/core/src/deferred/types.ts b/packages/core/src/deferred/types.ts new file mode 100644 index 0000000..a21f3c5 --- /dev/null +++ b/packages/core/src/deferred/types.ts @@ -0,0 +1,49 @@ +import type { Entity } from '../entity/types'; +import type { RelationPair } from '../relation/types'; +import type { ConfigurableTrait, Trait } from '../trait/types'; + +export type DeferredCommandType = 'spawn' | 'destroy' | 'add' | 'remove'; + +export type DeferredSpawnCommand = { + type: 'spawn'; + entity: Entity; + traits: ConfigurableTrait[]; +}; + +export type DeferredDestroyCommand = { + type: 'destroy'; + entity: Entity; +}; + +export type DeferredAddCommand = { + type: 'add'; + entity: Entity; + traits: ConfigurableTrait[]; +}; + +export type DeferredRemoveCommand = { + type: 'remove'; + entity: Entity; + traits: (Trait | RelationPair)[]; +}; + +export type DeferredCommand = + | DeferredSpawnCommand + | DeferredDestroyCommand + | DeferredAddCommand + | DeferredRemoveCommand; + +export type DeferredCommands = { + spawn(...traits: ConfigurableTrait[]): Entity; + destroy(entity: Entity): void; + add(entity: Entity, ...traits: ConfigurableTrait[]): void; + remove(entity: Entity, ...traits: (Trait | RelationPair)[]): void; + addExclusive(entity: Entity, relationPair: RelationPair): void; + flush(): void; +}; + +export type CommandBuffer = { + commands: DeferredCommand[]; + spawnedEntities: Set<Entity>; + destroyedEntities: Set<Entity>; +}; diff --git a/packages/core/src/entity/entity-methods-patch.ts b/packages/core/src/entity/entity-methods-patch.ts index bc8cd79..a929455 100644 --- a/packages/core/src/entity/entity-methods-patch.ts +++ b/packages/core/src/entity/entity-methods-patch.ts @@ -4,6 +4,7 @@ // that the methods are only called on entities. import { $internal } from '../common'; +import { flushCommandBuffer, projectedGetTrait, projectedHasTrait } from '../deferred/deferred'; import { setChanged } from '../query/modifiers/changed'; import { getFirstRelationTarget, getRelationTargets, hasRelationPair } from '../relation/relation'; import type { Relation, RelationPair } from '../relation/types'; @@ -15,21 +16,42 @@ import type { Entity } from './types'; import { isEntityAlive } from './utils/entity-index'; import { getEntityGeneration, getEntityId } from './utils/pack-entity'; +function flushPendingDeferredForEntity(entity: Entity): void { + const world = getEntityWorld(entity); + const ctx = world[$internal]; + const buffer = ctx.currentCommandBuffer; + + if (buffer.commands.length === 0) return; + + const hasCommandsForEntity = buffer.commands.some(cmd => cmd.entity === entity); + if (hasCommandsForEntity) { + flushCommandBuffer(world, buffer); + } +} + // @ts-expect-error Number.prototype.add = function (this: Entity, ...traits: ConfigurableTrait[]) { + flushPendingDeferredForEntity(this); return addTrait(getEntityWorld(this), this, ...traits); }; // @ts-expect-error Number.prototype.remove = function (this: Entity, ...traits: (Trait | RelationPair)[]) { + flushPendingDeferredForEntity(this); return removeTrait(getEntityWorld(this), this, ...traits); }; // @ts-expect-error Number.prototype.has = function (this: Entity, trait: Trait | RelationPair) { const world = getEntityWorld(this); + + const projected = projectedHasTrait(world, this, trait); + if (projected !== null) { + return projected; + } + if (isRelationPair(trait)) return hasRelationPair(world, this, trait); - return /* @inline @pure */ hasTrait(world, this, trait); + return hasTrait(world, this, trait); }; // @ts-expect-error @@ -44,7 +66,14 @@ Number.prototype.changed = function (this: Entity, trait: Trait) { // @ts-expect-error Number.prototype.get = function (this: Entity, trait: Trait | RelationPair) { - return getTrait(getEntityWorld(this), this, trait); + const world = getEntityWorld(this); + + const projected = projectedGetTrait(world, this, trait); + if (projected.hasProjection) { + return projected.value; + } + + return getTrait(world, this, trait); }; // @ts-expect-error diff --git a/packages/core/src/query/query-result.ts b/packages/core/src/query/query-result.ts index df8c7be..c3e362a 100644 --- a/packages/core/src/query/query-result.ts +++ b/packages/core/src/query/query-result.ts @@ -1,4 +1,5 @@ import { $internal } from '../common'; +import { createCommandBuffer, flushCommandBuffer } from '../deferred'; import type { Entity } from '../entity/types'; import { getEntityId } from '../entity/utils/pack-entity'; import { isRelationPair } from '../relation/utils/is-relation'; @@ -54,120 +55,119 @@ export function createQueryResult<T extends QueryParameter[]>( options: QueryResultOptions = { changeDetection: 'auto' } ) { const state = Array.from({ length: traits.length }); + const ctx = world[$internal]; + + const newBuffer = createCommandBuffer(); + ctx.commandBufferStack.push(ctx.currentCommandBuffer); + ctx.currentCommandBuffer = newBuffer; // Inline all three permutations of updateEach for performance. - if (options.changeDetection === 'auto') { - const changedPairs: [Entity, Trait][] = []; - const atomicSnapshots: any[] = []; - const trackedIndices: number[] = []; - const untrackedIndices: number[] = []; - - getTrackedTraits(traits, world, query, trackedIndices, untrackedIndices); - - for (let i = 0; i < entities.length; i++) { - const entity = entities[i]; - const eid = getEntityId(entity); - - createSnapshotsWithAtomic(eid, traits, stores, state, atomicSnapshots); - callback(state as unknown as InstancesFromParameters<T>, entity, i); - - // Skip if the entity has been destroyed. - if (!world.has(entity)) continue; - - // Commit all changes back to the stores for tracked traits. - for (let j = 0; j < trackedIndices.length; j++) { - const index = trackedIndices[j]; - const trait = traits[index]; - const ctx = trait[$internal]; - const newValue = state[index]; - const store = stores[index]; - - let changed = false; - if (ctx.type === 'aos') { - changed = ctx.fastSetWithChangeDetection(eid, store, newValue); - if (!changed) { - changed = !shallowEqual(newValue, atomicSnapshots[index]); + try { + if (options.changeDetection === 'auto') { + const changedPairs: [Entity, Trait][] = []; + const atomicSnapshots: any[] = []; + const trackedIndices: number[] = []; + const untrackedIndices: number[] = []; + + getTrackedTraits(traits, world, query, trackedIndices, untrackedIndices); + + for (let i = 0; i < entities.length; i++) { + const entity = entities[i]; + const eid = getEntityId(entity); + + createSnapshotsWithAtomic(eid, traits, stores, state, atomicSnapshots); + callback(state as unknown as InstancesFromParameters<T>, entity, i); + + if (!world.has(entity)) continue; + + for (let j = 0; j < trackedIndices.length; j++) { + const index = trackedIndices[j]; + const trait = traits[index]; + const traitCtx = trait[$internal]; + const newValue = state[index]; + const store = stores[index]; + + let changed = false; + if (traitCtx.type === 'aos') { + changed = traitCtx.fastSetWithChangeDetection(eid, store, newValue); + if (!changed) { + changed = !shallowEqual(newValue, atomicSnapshots[index]); + } + } else { + changed = traitCtx.fastSetWithChangeDetection(eid, store, newValue); } - } else { - changed = ctx.fastSetWithChangeDetection(eid, store, newValue); + + if (changed) changedPairs.push([entity, trait] as const); } - // Collect changed traits. - if (changed) changedPairs.push([entity, trait] as const); + for (let j = 0; j < untrackedIndices.length; j++) { + const index = untrackedIndices[j]; + const trait = traits[index]; + const traitCtx = trait[$internal]; + const store = stores[index]; + traitCtx.fastSet(eid, store, state[index]); + } } - // Commit all changes back to the stores for untracked traits. - for (let j = 0; j < untrackedIndices.length; j++) { - const index = untrackedIndices[j]; - const trait = traits[index]; - const ctx = trait[$internal]; - const store = stores[index]; - ctx.fastSet(eid, store, state[index]); + for (let i = 0; i < changedPairs.length; i++) { + const [entity, trait] = changedPairs[i]; + setChanged(world, entity, trait); } - } - - // Trigger change events for each entity that was modified. - for (let i = 0; i < changedPairs.length; i++) { - const [entity, trait] = changedPairs[i]; - setChanged(world, entity, trait); - } - } else if (options.changeDetection === 'always') { - const changedPairs: [Entity, Trait][] = []; - const atomicSnapshots: any[] = []; - - for (let i = 0; i < entities.length; i++) { - const entity = entities[i]; - const eid = getEntityId(entity); - - createSnapshotsWithAtomic(eid, traits, stores, state, atomicSnapshots); - callback(state as unknown as InstancesFromParameters<T>, entity, i); - - // Skip if the entity has been destroyed. - if (!world.has(entity)) continue; - - // Commit all changes back to the stores. - for (let j = 0; j < traits.length; j++) { - const trait = traits[j]; - const ctx = trait[$internal]; - const newValue = state[j]; - - let changed = false; - if (ctx.type === 'aos') { - changed = ctx.fastSetWithChangeDetection(eid, stores[j], newValue); - if (!changed) { - changed = !shallowEqual(newValue, atomicSnapshots[j]); + } else if (options.changeDetection === 'always') { + const changedPairs: [Entity, Trait][] = []; + const atomicSnapshots: any[] = []; + + for (let i = 0; i < entities.length; i++) { + const entity = entities[i]; + const eid = getEntityId(entity); + + createSnapshotsWithAtomic(eid, traits, stores, state, atomicSnapshots); + callback(state as unknown as InstancesFromParameters<T>, entity, i); + + if (!world.has(entity)) continue; + + for (let j = 0; j < traits.length; j++) { + const trait = traits[j]; + const traitCtx = trait[$internal]; + const newValue = state[j]; + + let changed = false; + if (traitCtx.type === 'aos') { + changed = traitCtx.fastSetWithChangeDetection(eid, stores[j], newValue); + if (!changed) { + changed = !shallowEqual(newValue, atomicSnapshots[j]); + } + } else { + changed = traitCtx.fastSetWithChangeDetection(eid, stores[j], newValue); } - } else { - changed = ctx.fastSetWithChangeDetection(eid, stores[j], newValue); - } - // Collect changed traits. - if (changed) changedPairs.push([entity, trait] as const); + if (changed) changedPairs.push([entity, trait] as const); + } } - } - // Trigger change events for each entity that was modified. - for (let i = 0; i < changedPairs.length; i++) { - const [entity, trait] = changedPairs[i]; - setChanged(world, entity, trait); - } - } else if (options.changeDetection === 'never') { - for (let i = 0; i < entities.length; i++) { - const entity = entities[i]; - const eid = getEntityId(entity); - createSnapshots(eid, traits, stores, state); - callback(state as unknown as InstancesFromParameters<T>, entity, i); - - // Skip if the entity has been destroyed. - if (!world.has(entity)) continue; - - // Commit all changes back to the stores. - for (let j = 0; j < traits.length; j++) { - const trait = traits[j]; - const ctx = trait[$internal]; - ctx.fastSet(eid, stores[j], state[j]); + for (let i = 0; i < changedPairs.length; i++) { + const [entity, trait] = changedPairs[i]; + setChanged(world, entity, trait); + } + } else if (options.changeDetection === 'never') { + for (let i = 0; i < entities.length; i++) { + const entity = entities[i]; + const eid = getEntityId(entity); + createSnapshots(eid, traits, stores, state); + callback(state as unknown as InstancesFromParameters<T>, entity, i); + + if (!world.has(entity)) continue; + + for (let j = 0; j < traits.length; j++) { + const trait = traits[j]; + const traitCtx = trait[$internal]; + traitCtx.fastSet(eid, stores[j], state[j]); + } } } + } finally { + flushCommandBuffer(world, newBuffer); + ctx.currentCommandBuffer = ctx.commandBufferStack.pop()!; } return results; diff --git a/packages/core/src/trait/trait.ts b/packages/core/src/trait/trait.ts index b41c550..08c9eee 100644 --- a/packages/core/src/trait/trait.ts +++ b/packages/core/src/trait/trait.ts @@ -281,7 +281,7 @@ export function removeTrait(world: World, entity: Entity, ...traits: (Trait | Re } removeAllRelationTargets(world, relation, entity); - removeTraitFromEntity(world, entity, relationTrait); + removeTraitFromEntity(world, entity, relationTrait, true); return; } @@ -296,7 +296,7 @@ export function removeTrait(world: World, entity: Entity, ...traits: (Trait | Re if (removedIndex === -1) return; if (wasLastTarget) { - removeTraitFromEntity(world, entity, relationTrait); + removeTraitFromEntity(world, entity, relationTrait, true); } } } @@ -493,8 +493,9 @@ export function getTrait(world: World, entity: Entity, trait: Trait | RelationPa /** * Core logic for removing a trait from an entity. + * @param skipSubscriptions - If true, don't fire removeSubscriptions (caller already handled it) */ -function removeTraitFromEntity(world: World, entity: Entity, trait: Trait): void { +function removeTraitFromEntity(world: World, entity: Entity, trait: Trait, skipSubscriptions = false): void { // Exit early if the entity doesn't have the trait if (!hasTrait(world, entity, trait)) return; @@ -502,9 +503,11 @@ function removeTraitFromEntity(world: World, entity: Entity, trait: Trait): void const instance = getTraitInstance(ctx.traitInstances, trait)!; const { generationId, bitflag, queries, trackingQueries } = instance; - // Call remove subscriptions before removing the trait - for (const sub of instance.removeSubscriptions) { - sub(entity); + // Call remove subscriptions before removing the trait (unless caller handled it) + if (!skipSubscriptions) { + for (const sub of instance.removeSubscriptions) { + sub(entity); + } } // Remove bitflag from entity bitmask diff --git a/packages/core/src/world/types.ts b/packages/core/src/world/types.ts index d3a8c1e..7bb5546 100644 --- a/packages/core/src/world/types.ts +++ b/packages/core/src/world/types.ts @@ -1,5 +1,6 @@ import { ActionInstance } from '../actions/types'; import type { $internal } from '../common'; +import type { CommandBuffer, DeferredCommands } from '../deferred'; import type { Entity } from '../entity/types'; import type { createEntityIndex } from '../entity/utils/entity-index'; import type { @@ -43,6 +44,8 @@ export type WorldInternal = { worldEntity: Entity; trackedTraits: Set<Trait>; resetSubscriptions: Set<(world: World) => void>; + commandBufferStack: CommandBuffer[]; + currentCommandBuffer: CommandBuffer; }; export type World = { @@ -50,6 +53,7 @@ export type World = { readonly isInitialized: boolean; readonly entities: Entity[]; readonly traits: Set<Trait>; + readonly deferred: DeferredCommands; [$internal]: WorldInternal; init(...traits: ConfigurableTrait[]): void; spawn(...traits: ConfigurableTrait[]): Entity; diff --git a/packages/core/src/world/world.ts b/packages/core/src/world/world.ts index 586dbca..ab5046e 100644 --- a/packages/core/src/world/world.ts +++ b/packages/core/src/world/world.ts @@ -1,4 +1,6 @@ import { $internal } from '../common'; +import { createCommandBuffer, createDeferredCommands } from '../deferred'; +import type { CommandBuffer } from '../deferred'; import { createEntity, destroyEntity } from '../entity/entity'; import type { Entity } from '../entity/types'; import { createEntityIndex, getAliveEntities, isEntityAlive } from '../entity/utils/entity-index'; @@ -35,6 +37,8 @@ export function createWorld( let isInitialized = false; let lazyTraits: ConfigurableTrait[] | undefined; + const rootCommandBuffer = createCommandBuffer(); + const world = { [$internal]: { entityIndex: createEntityIndex(id), @@ -54,6 +58,8 @@ export function createWorld( worldEntity: null!, trackedTraits: new Set(), resetSubscriptions: new Set(), + commandBufferStack: [] as CommandBuffer[], + currentCommandBuffer: rootCommandBuffer, } as WorldInternal, traits: new Set<Trait>(), @@ -126,6 +132,11 @@ export function createWorld( lazyTraits = undefined; const ctx = world[$internal]; + ctx.currentCommandBuffer.commands.length = 0; + ctx.currentCommandBuffer.spawnedEntities.clear(); + ctx.currentCommandBuffer.destroyedEntities.clear(); + ctx.commandBufferStack.length = 0; + // Destroy all entities so any cleanup is done. world.entities.forEach((entity) => { // Some relations may have caused the entity to be destroyed before @@ -367,6 +378,12 @@ export function createWorld( enumerable: true, }); + const deferred = createDeferredCommands(world, () => world[$internal].currentCommandBuffer); + Object.defineProperty(world, 'deferred', { + get: () => deferred, + enumerable: true, + }); + // Handle initialization based on arguments if ( optionsOrFirstTrait &&
diff --git a/packages/core/src/deferred/deferred.ts b/packages/core/src/deferred/deferred.ts
new file mode 100644
index 0000000..5efc9f2
--- /dev/null
+++ b/packages/core/src/deferred/deferred.ts
@@ -0,0 +1,819 @@
+import { $internal } from '../common';
+import { destroyEntity } from '../entity/entity';
+import type { Entity } from '../entity/types';
+import { allocateEntity, releaseEntity } from '../entity/utils/entity-index';
+import { getRelationTargets, hasRelationPair } from '../relation/relation';
+import type { Relation, RelationPair } from '../relation/types';
+import { isRelationPair } from '../relation/utils/is-relation';
+import { addTrait, hasTrait, removeTrait, setTrait } from '../trait/trait';
+import type { ConfigurableTrait, Trait } from '../trait/types';
+import type { World } from '../world/types';
+import type { CommandBuffer, DeferredCommands } from './types';
+
+export function createCommandBuffer(): CommandBuffer {
+ return {
+ commands: [],
+ spawnedEntities: new Set(),
+ destroyedEntities: new Set(),
+ };
+}
+
+export function createDeferredCommands(world: World, getBuffer: () => CommandBuffer): DeferredCommands {
+ return {
+ spawn(...traits: ConfigurableTrait[]): Entity {
+ const buffer = getBuffer();
+ const ctx = world[$internal];
+ const entity = allocateEntity(ctx.entityIndex);
+ buffer.spawnedEntities.add(entity);
+ buffer.commands.push({
+ type: 'spawn',
+ entity,
+ traits,
+ });
+ return entity;
+ },
+
+ destroy(entity: Entity): void {
+ const buffer = getBuffer();
+ buffer.destroyedEntities.add(entity);
+ buffer.commands.push({
+ type: 'destroy',
+ entity,
+ });
+ },
+
+ add(entity: Entity, ...traits: ConfigurableTrait[]): void {
+ const buffer = getBuffer();
+ buffer.commands.push({
+ type: 'add',
+ entity,
+ traits,
+ });
+ },
+
+ remove(entity: Entity, ...traits: (Trait | RelationPair)[]): void {
+ const buffer = getBuffer();
+ buffer.commands.push({
+ type: 'remove',
+ entity,
+ traits,
+ });
+ },
+
+ addExclusive(entity: Entity, relationPair: RelationPair): void {
+ const buffer = getBuffer();
+ const pairCtx = relationPair[$internal];
+ const relation = pairCtx.relation;
+ const newTarget = pairCtx.target;
+
+ const existingTargets = getRelationTargets(world, relation, entity);
+
+ for (const existingTarget of existingTargets) {
+ if (existingTarget !== newTarget) {
+ buffer.commands.push({
+ type: 'remove',
+ entity,
+ traits: [relation(existingTarget)],
+ });
+ }
+ }
+
+ const projectedState = getProjectedEntityState(buffer, entity, world);
+ if (projectedState) {
+ for (const traitConfig of projectedState.traitsToAdd) {
+ if (isRelationPair(traitConfig)) {
+ const addedPairCtx = traitConfig[$internal];
+ if (addedPairCtx.relation === relation && addedPairCtx.target !== newTarget) {
+ buffer.commands.push({
+ type: 'remove',
+ entity,
+ traits: [traitConfig],
+ });
+ }
+ }
+ }

Xet Storage Details

Size:
53.7 kB
·
Xet hash:
cad2b53e1c2aee7445dd3aef7e8d08bd71123d21dc863b1c6353e21de62d352f

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.