Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ecs/core/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types.js'
7 changes: 7 additions & 0 deletions src/ecs/core/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @import { Constructor } from '../../reflect/index.js' */

/**
* @template {unknown[]} T
* @typedef {{[K in keyof T]:Constructor<T[K]>}} TupleConstructor
*/
export default {}
1 change: 1 addition & 0 deletions src/ecs/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './archetype/index.js'
export * from './query/index.js'
export * from './core/index.js'
export * from './registry.js'
export * from './schedule/index.js'
export * from './typestore.js'
Expand Down
8 changes: 2 additions & 6 deletions src/ecs/query/query.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @import { TupleConstructor } from '../core/index.js'*/
/** @import { TableId, TableRow } from '../typedef/index.js'*/
/** @import { Constructor, TypeId } from '../../reflect/index.js'*/
/** @import { TypeId } from '../../reflect/index.js'*/

import { Entity } from '../entities/index.js'
import { World } from '../registry.js'
Expand Down Expand Up @@ -307,9 +308,4 @@ function filter(archetype, filters) {
* @param {[...T]} components1
* @param {[...T]} components2
* @returns {void}
*/

/**
* @template {unknown[]} T
* @typedef {{[K in keyof T]:Constructor<T[K]>}} TupleConstructor
*/
52 changes: 52 additions & 0 deletions src/ecs/registry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @import { Constructor, TypeId } from '../reflect/index.js'*/
/** @import { TupleConstructor } from './core/index.js'*/
/** @import { ArchetypeId, TableId, TableRow } from './typedef/index.js'*/

import { Table, Tables } from './tables/index.js'
Expand Down Expand Up @@ -197,6 +198,47 @@ export class World {
this.callInsertComponentHook(entity, existingIds)
}

/**
* @template {unknown[]} T
* @param {Entity} entity
* @param {TupleConstructor<T>} components
*/
remove(entity, components) {
const location = this.entities.get(entity.index)

if (!location) {
return
}

const { archetypeId: oldArchetypeId, index, tableId: oldTableId } = location
const oldArchetype = this.archetypes.get(oldArchetypeId)
const oldTable = this.tables.get(oldTableId)

if (!oldTable) return

const removedIds = (components.map((c) => typeid(c)))
const existingIds = oldArchetype.types
const newIds = removeElements(existingIds, removedIds)
const [newTableId, newTable, newArchetypeId] = this.resolve(newIds)

this.callRemoveComponentHook(entity, removedIds)

const newIndex = oldTable.moveTo(newTable, index)
const swapped = /** @type {Entity | null}*/ (oldTable.get(typeid(Entity), index))

location.tableId = newTableId
location.archetypeId = newArchetypeId
location.index = newIndex

if (swapped) {
const swappedlocation = /** @type {EntityLocation} */ (this.entities.get(swapped.index))

swappedlocation.index = index
}

this.callInsertComponentHook(entity, newIds)
}

/**
* @template {{}[]} T
* @param {[...T][]} entities
Expand Down Expand Up @@ -379,4 +421,14 @@ export class World {
*/
function deduplicate(newIds) {
return [...new Set(newIds)]
}

/**
* @private
* @param {readonly TypeId[]} arr
* @param {readonly TypeId[]} remove
* @returns {TypeId[]}
*/
function removeElements(arr, remove) {
return arr.filter((e) => !remove.includes(e))
}