diff --git a/README.md b/README.md index 3ee2e23..10b1008 100644 --- a/README.md +++ b/README.md @@ -34,13 +34,13 @@ import clamp from 'clamp' // generates a new entity component system -const world = ECS.createWorld() +const world = ECS.addWorld() // set up the player -const PLAYER = ECS.createEntity(world) -ECS.addComponentToEntity(world, PLAYER, 'position', { x: 15, y: 23 }) -ECS.addComponentToEntity(world, PLAYER, 'moveable', { dx: 0, dy: 0 }) +const PLAYER = ECS.addEntity(world) +ECS.addComponent(world, PLAYER, 'position', { x: 15, y: 23 }) +ECS.addComponent(world, PLAYER, 'moveable', { dx: 0, dy: 0 }) // update entity velocity based on key pressed @@ -165,13 +165,13 @@ This is typically what you want, but if you want to immediately remove a compone ```javascript const componentName = 'aabb' -const world = ECS.createWorld() -const entity = ECS.createEntity(world) -ECS.addComponentToEntity(world, entity, componentName) +const world = ECS.addWorld() +const entity = ECS.addEntity(world) +ECS.addComponent(world, entity, componentName) const deferredRemoval = false // by default this is true. setting it to false immediately removes the component -ECS.removeComponentFromEntity(world, entity, componentName, deferredRemoval) +ECS.removeComponent(world, entity, componentName, deferredRemoval) ECS.getEntities(world, [ componentName ]).length // because we are not deferring the removal, length === 0 ``` @@ -185,9 +185,9 @@ This is typically what you want, but if you want to immediately remove an entity ```javascript -const world = ECS.createWorld() -const entity = ECS.createEntity(world) -ECS.addComponentToEntity(world, entity, 'test_component') +const world = ECS.addWorld() +const entity = ECS.addEntity(world) +ECS.addComponent(world, entity, 'test_component') const deferredRemoval = false // by default this is true. setting it to false immediately removes the component @@ -202,9 +202,9 @@ ECS.getEntities(world, [ 'test_component' ]).length // because we are not defer ECS will generate a unique integer id for every entity created in a world: ```javascript -const world = ECS.createWorld() -const e = ECS.createEntity(world) -const e2 = ECS.createEntity(world) +const world = ECS.addWorld() +const e = ECS.addEntity(world) +const e2 = ECS.addEntity(world) const id1 = ECS.getEntityId(world, e) const id2 = ECS.getEntityId(world, e2) @@ -243,20 +243,20 @@ Typescript and named exports are also supported: ```typescript import { default as ECS, - createWorld, - createEntity, - addComponentToEntity, + addWorld, + addEntity, + addComponent, getEntities, addSystem, SystemFunction, SystemUpdateFunction } from 'ecs' -const world = createWorld() +const world = addWorld() -const PLAYER = createEntity(world); -addComponentToEntity(world, PLAYER, 'position', { x: 15, y: 23 }) -addComponentToEntity(world, PLAYER, 'moveable', { dx: 0, dy: 0 }) +const PLAYER = addEntity(world); +addComponent(world, PLAYER, 'position', { x: 15, y: 23 }) +addComponent(world, PLAYER, 'moveable', { dx: 0, dy: 0 }) const movementSystem: SystemFunction = function (world) { const onUpdate: SystemUpdateFunction = function (dt) { diff --git a/ecs.js b/ecs.js index aec25a9..4867d03 100644 --- a/ecs.js +++ b/ecs.js @@ -2,9 +2,6 @@ import * as ComponentSet from './component-set.js' import removeItems from 'remove-array-items' -const now = (typeof performance === 'undefined') ? (() => Date.now()) : (() => performance.now()) - - /** * @typedef { 'added' | 'removed' } ListenerType */ @@ -435,9 +432,9 @@ export function preFixedUpdate (world, dt) { for (let i=0; i < world.systems.length; i++) { world.stats.currentSystem = i const system = world.systems[i] - const start = now() + const start = performance.now() system.onPreFixedUpdate(dt) - world.stats.systems[i].timeElapsed += (now() - start) + world.stats.systems[i].timeElapsed += (performance.now() - start) } } @@ -451,9 +448,9 @@ export function fixedUpdate (world, dt) { for (let i=0; i < world.systems.length; i++) { world.stats.currentSystem = i const system = world.systems[i] - const start = now() + const start = performance.now() system.onFixedUpdate(dt) - world.stats.systems[i].timeElapsed += (now() - start) + world.stats.systems[i].timeElapsed += (performance.now() - start) } } @@ -466,9 +463,9 @@ export function postFixedUpdate (world, dt) { for (let i=0; i < world.systems.length; i++) { world.stats.currentSystem = i const system = world.systems[i] - const start = now() + const start = performance.now() system.onPostFixedUpdate(dt) - world.stats.systems[i].timeElapsed += (now() - start) + world.stats.systems[i].timeElapsed += (performance.now() - start) } } @@ -482,9 +479,9 @@ export function preUpdate (world, dt) { for (let i=0; i < world.systems.length; i++) { world.stats.currentSystem = i const system = world.systems[i] - const start = now() + const start = performance.now() system.onPreUpdate(dt) - world.stats.systems[i].timeElapsed += (now() - start) + world.stats.systems[i].timeElapsed += (performance.now() - start) } } @@ -497,9 +494,9 @@ export function update (world, dt) { for (let i=0; i < world.systems.length; i++) { world.stats.currentSystem = i const system = world.systems[i] - const start = now() + const start = performance.now() system.onUpdate(dt) - world.stats.systems[i].timeElapsed += (now() - start) + world.stats.systems[i].timeElapsed += (performance.now() - start) } } @@ -512,9 +509,9 @@ export function postUpdate (world, dt) { for (let i=0; i < world.systems.length; i++) { world.stats.currentSystem = i const system = world.systems[i] - const start = now() + const start = performance.now() system.onPostUpdate(dt) - world.stats.systems[i].timeElapsed += (now() - start) + world.stats.systems[i].timeElapsed += (performance.now() - start) } } @@ -697,5 +694,11 @@ export default { update, preUpdate, postUpdate, - cleanup + cleanup, + + // aliases. shorter == nicer :) + addWorld: createWorld, + addEntity: createEntity, + addComponent: addComponentToEntity, + removeComponent: removeComponentFromEntity } diff --git a/examples/events/app.js b/examples/events/app.js index 228032b..98887b3 100644 --- a/examples/events/app.js +++ b/examples/events/app.js @@ -20,7 +20,7 @@ const Global = { async function main () { // init - Global.world = ECS.createWorld() + Global.world = ECS.addWorld() // add all of the game's systems ECS.addSystem(Global.world, widgetSpawnerSystem) @@ -67,8 +67,8 @@ main() function createWidgetEntity (world) { - const w = ECS.createEntity(world) - ECS.addComponentToEntity(world, w, 'widget', { + const w = ECS.addEntity(world) + ECS.addComponent(world, w, 'widget', { frameTTL: 60, // after 60 frames this widget should be removed }) } diff --git a/test/addComponentToEntity.js b/test/addComponent.js similarity index 69% rename from test/addComponentToEntity.js rename to test/addComponent.js index 3af65a6..3894ed7 100644 --- a/test/addComponentToEntity.js +++ b/test/addComponent.js @@ -2,21 +2,21 @@ import ECS from '../ecs.js' import tap from 'tap' -const w = ECS.createWorld() +const w = ECS.addWorld() -const e = ECS.createEntity(w) +const e = ECS.addEntity(w) -ECS.addComponentToEntity(w, e, 'position', { x: 4, y: 12 }) +ECS.addComponent(w, e, 'position', { x: 4, y: 12 }) tap.same(w.entities, [ { position: { x: 4, y: 12 } } ]) // adds are not lost when a system queries for a component before any are present { - const w = ECS.createWorld() - const e = ECS.createEntity(w) + const w = ECS.addWorld() + const e = ECS.addEntity(w) - ECS.addComponentToEntity(w, e, 'dupe') + ECS.addComponent(w, e, 'dupe') ECS.cleanup(w) diff --git a/test/createEntity.js b/test/addEntity.js similarity index 58% rename from test/createEntity.js rename to test/addEntity.js index 1fd59c1..e1f65e9 100644 --- a/test/createEntity.js +++ b/test/addEntity.js @@ -2,8 +2,8 @@ import ECS from '../ecs.js' import tap from 'tap' -const w = ECS.createWorld() +const w = ECS.addWorld() -const e = ECS.createEntity(w) +const e = ECS.addEntity(w) tap.same(w.entities, [ e ]) diff --git a/test/addSystem.js b/test/addSystem.js index 028b468..15277f1 100644 --- a/test/addSystem.js +++ b/test/addSystem.js @@ -2,7 +2,7 @@ import ECS from '../ecs.js' import tap from 'tap' -const w = ECS.createWorld() +const w = ECS.addWorld() function boringSystem (world) { diff --git a/test/createWorld.js b/test/addWorld.js similarity index 95% rename from test/createWorld.js rename to test/addWorld.js index f976cde..775fb52 100644 --- a/test/createWorld.js +++ b/test/addWorld.js @@ -2,7 +2,7 @@ import ECS from '../ecs.js' import tap from 'tap' -const w = ECS.createWorld() +const w = ECS.addWorld() tap.same(w, { entityIds: new Map(), diff --git a/test/exports.js b/test/exports.js index 5ce0768..1b42152 100644 --- a/test/exports.js +++ b/test/exports.js @@ -41,3 +41,9 @@ tap.ok(allECS.default.preUpdate); tap.ok(allECS.default.update); tap.ok(allECS.default.postUpdate); tap.notOk(allECS.default.emptyListeners); + +// aliased functions +tap.ok(allECS.default.addWorld); +tap.ok(allECS.default.addEntity); +tap.ok(allECS.default.addComponent); +tap.ok(allECS.default.removeComponent); diff --git a/test/getEntities.js b/test/getEntities.js index 4e4adba..f157346 100644 --- a/test/getEntities.js +++ b/test/getEntities.js @@ -2,7 +2,7 @@ import ECS from '../ecs.js' import tap from 'tap' -const w = ECS.createWorld() +const w = ECS.addWorld() const entities = ECS.getEntities(w, [ 'position' ]) @@ -10,9 +10,9 @@ tap.equal(w.filters.position.length, 0, 'querying a filter creates it the first -const e = ECS.createEntity(w) +const e = ECS.addEntity(w) -ECS.addComponentToEntity(w, e, 'position', { x: 4, y: 12 }) +ECS.addComponent(w, e, 'position', { x: 4, y: 12 }) tap.same(w.filters.position, [ { position: { x: 4, y: 12 } } ], 'new entities are added to existing filters') @@ -22,16 +22,16 @@ ECS.getEntities(w, [ 'health' ]) tap.equal(w.filters.health.length, 0, 'new filter starts out empty') -const e2 = ECS.createEntity(w) -ECS.addComponentToEntity(w, e2, 'health', { max: 10, current: 8 }) +const e2 = ECS.addEntity(w) +ECS.addComponent(w, e2, 'health', { max: 10, current: 8 }) tap.same(w.filters.health, [ e2 ], 'when adding components to entities makes them match existing filters, add them') -const e3 = ECS.createEntity(w) -ECS.addComponentToEntity(w, e3, 'a', { val: 10 }) -ECS.addComponentToEntity(w, e3, 'b', { val: 20 }) +const e3 = ECS.addEntity(w) +ECS.addComponent(w, e3, 'a', { val: 10 }) +ECS.addComponent(w, e3, 'b', { val: 20 }) tap.equal(1, ECS.getEntities(w, [ 'a', 'b' ]).length, 'order of components in filter does not matter') tap.equal(1, ECS.getEntities(w, [ 'b', 'a' ]).length, 'order of components in fitler does not matter') diff --git a/test/getEntitiesNotModifier.js b/test/getEntitiesNotModifier.js index 6a2155a..b775362 100644 --- a/test/getEntitiesNotModifier.js +++ b/test/getEntitiesNotModifier.js @@ -2,14 +2,14 @@ import ECS from '../ecs.js' import tap from 'tap' -const w = ECS.createWorld() +const w = ECS.addWorld() -const e = ECS.createEntity(w) -ECS.addComponentToEntity(w, e, 'position', { x: 4, y: 12 }) +const e = ECS.addEntity(w) +ECS.addComponent(w, e, 'position', { x: 4, y: 12 }) -const e2 = ECS.createEntity(w) -ECS.addComponentToEntity(w, e2, 'position', { x: 10, y: 0 }) -ECS.addComponentToEntity(w, e2, 'flimflam', { color: 'orange' }) +const e2 = ECS.addEntity(w) +ECS.addComponent(w, e2, 'position', { x: 10, y: 0 }) +ECS.addComponent(w, e2, 'flimflam', { color: 'orange' }) // find all entities that have position but not flimflam const entitiesNotted = ECS.getEntities(w, [ 'position', '!flimflam' ]) @@ -18,21 +18,21 @@ tap.same(entitiesNotted[0], e) // adding and then removing, then re-adding a component on an entity should filter correctly -const w2 = ECS.createWorld() +const w2 = ECS.addWorld() -const e3 = ECS.createEntity(w2) -ECS.addComponentToEntity(w, e3, 'a') +const e3 = ECS.addEntity(w2) +ECS.addComponent(w, e3, 'a') const result = ECS.getEntities(w2, [ 'a', '!b' ]) tap.equal(result.length, 1) tap.same(result[0], e3) -ECS.addComponentToEntity(w2, e3, 'b') +ECS.addComponent(w2, e3, 'b') const result2 = ECS.getEntities(w2, [ 'a', '!b' ]) tap.equal(result2.length, 0) -ECS.removeComponentFromEntity(w2, e3, 'b') +ECS.removeComponent(w2, e3, 'b') ECS.cleanup(w2) diff --git a/test/getEntity.js b/test/getEntity.js index aa964b4..f613809 100644 --- a/test/getEntity.js +++ b/test/getEntity.js @@ -3,7 +3,7 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() const h = ECS.getEntity(w, [ 'hero' ]) @@ -12,10 +12,10 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) - ECS.addComponentToEntity(w, e, 'hero') + const e = ECS.addEntity(w) + ECS.addComponent(w, e, 'hero') const h = ECS.getEntity(w, [ 'hero' ]) @@ -24,15 +24,15 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) - ECS.addComponentToEntity(w, e, 'hero') - ECS.addComponentToEntity(w, e, 'a') + const e = ECS.addEntity(w) + ECS.addComponent(w, e, 'hero') + ECS.addComponent(w, e, 'a') - const e2 = ECS.createEntity(w) - ECS.addComponentToEntity(w, e2, 'hero') - ECS.addComponentToEntity(w, e2, 'b') + const e2 = ECS.addEntity(w) + ECS.addComponent(w, e2, 'hero') + ECS.addComponent(w, e2, 'b') const h = ECS.getEntity(w, [ 'hero' ]) diff --git a/test/id.js b/test/id.js index c3ef7be..2bfa8f7 100644 --- a/test/id.js +++ b/test/id.js @@ -3,11 +3,11 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e1 = ECS.createEntity(w) - const e2 = ECS.createEntity(w) - const e3 = ECS.createEntity(w) + const e1 = ECS.addEntity(w) + const e2 = ECS.addEntity(w) + const e3 = ECS.addEntity(w) tap.equal(ECS.getEntityId(w, e1), 1, 'generates an integer id') tap.equal(ECS.getEntityId(w, e2), 2, 'generates an integer id') @@ -21,9 +21,9 @@ import tap from 'tap' // immediate entity removal works as expected { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e1 = ECS.createEntity(w) + const e1 = ECS.addEntity(w) tap.equal(ECS.getEntityId(w, e1), 1, 'generates an integer id') tap.equal(e1, ECS.getEntityById(w, 1), 'retrieves entity by id') @@ -40,9 +40,9 @@ import tap from 'tap' // deferred entity removal works as expected { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e1 = ECS.createEntity(w) + const e1 = ECS.addEntity(w) tap.equal(ECS.getEntityId(w, e1), 1, 'generates an integer id') tap.equal(e1, ECS.getEntityById(w, 1), 'retrieves entity by id') @@ -65,11 +65,11 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e1 = ECS.createEntity(w) - const e2 = ECS.createEntity(w) - const e3 = ECS.createEntity(w) + const e1 = ECS.addEntity(w) + const e2 = ECS.addEntity(w) + const e3 = ECS.addEntity(w) const deferredRemoval = true @@ -79,7 +79,7 @@ import tap from 'tap' ECS.cleanup(w) - const e4 = ECS.createEntity(w) + const e4 = ECS.addEntity(w) tap.equal(ECS.getEntityId(w, e4), 4, 'entity id still increments despite entity removal') } diff --git a/test/listeners.js b/test/listeners.js index f416462..1d0e7ed 100644 --- a/test/listeners.js +++ b/test/listeners.js @@ -3,13 +3,13 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) - ECS.addComponentToEntity(w, e, 'a') - ECS.addComponentToEntity(w, e, 'b') - ECS.addComponentToEntity(w, e, 'c') + ECS.addComponent(w, e, 'a') + ECS.addComponent(w, e, 'b') + ECS.addComponent(w, e, 'c') const s = new Set() s.add(e) @@ -24,9 +24,9 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) ECS.cleanup(w) @@ -45,9 +45,9 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) ECS.cleanup(w) diff --git a/test/listenersNotModifier.js b/test/listenersNotModifier.js index 287e006..c5226fc 100644 --- a/test/listenersNotModifier.js +++ b/test/listenersNotModifier.js @@ -3,11 +3,11 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) - ECS.addComponentToEntity(w, e, 'a') + ECS.addComponent(w, e, 'a') ECS.removeEntity(w, e) ECS.cleanup(w) diff --git a/test/removeComponentFromEntity.js b/test/removeComponent.js similarity index 61% rename from test/removeComponentFromEntity.js rename to test/removeComponent.js index 7282260..287708d 100644 --- a/test/removeComponentFromEntity.js +++ b/test/removeComponent.js @@ -3,15 +3,15 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) - ECS.addComponentToEntity(w, e, 'position', { x: 4, y: 12 }) - ECS.addComponentToEntity(w, e, 'health', { health: 34 }) + ECS.addComponent(w, e, 'position', { x: 4, y: 12 }) + ECS.addComponent(w, e, 'health', { health: 34 }) - const e2 = ECS.createEntity(w) - ECS.addComponentToEntity(w, e2, 'health', { health: 34 }) + const e2 = ECS.addEntity(w) + ECS.addComponent(w, e2, 'health', { health: 34 }) const entities = ECS.getEntities(w, [ 'position' ]) @@ -20,7 +20,7 @@ import tap from 'tap' tap.equal(entities.length, 1) tap.equal(entities2.length, 1) - ECS.removeComponentFromEntity(w, e, 'position') + ECS.removeComponent(w, e, 'position') ECS.cleanup(w) const entities3 = ECS.getEntities(w, [ 'position' ]) @@ -37,16 +37,16 @@ import tap from 'tap' // while iterating over entities, removing a component from an unvisited entity still gets processed // because the removal is defferred until the cleanup step - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) - ECS.addComponentToEntity(w, e, 'position', 'p3') + const e = ECS.addEntity(w) + ECS.addComponent(w, e, 'position', 'p3') - const e2 = ECS.createEntity(w) - ECS.addComponentToEntity(w, e2, 'position', 'p4') + const e2 = ECS.addEntity(w) + ECS.addComponent(w, e2, 'position', 'p4') - const e3 = ECS.createEntity(w) - ECS.addComponentToEntity(w, e3, 'position', 'p5') + const e3 = ECS.addEntity(w) + ECS.addComponent(w, e3, 'position', 'p5') let i = 0 const processed = { } @@ -56,7 +56,7 @@ import tap from 'tap' // while processing the first entity in the list, remove the 2nd entity if (i == 0) - ECS.removeComponentFromEntity(w, e2, 'position') + ECS.removeComponent(w, e2, 'position') i++ } @@ -66,17 +66,17 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e8 = ECS.createEntity(w) + const e8 = ECS.addEntity(w) - ECS.addComponentToEntity(w, e8, 'dupe', { abc: true }) + ECS.addComponent(w, e8, 'dupe', { abc: true }) ECS.cleanup(w) - ECS.removeComponentFromEntity(w, e8, 'dupe') - ECS.removeComponentFromEntity(w, e8, 'dupe') - ECS.removeComponentFromEntity(w, e8, 'dupe') + ECS.removeComponent(w, e8, 'dupe') + ECS.removeComponent(w, e8, 'dupe') + ECS.removeComponent(w, e8, 'dupe') tap.same(ECS.getEntities(w, [ 'dupe' ], 'removed'), [ e8 ], 'multiple removals only appear once in the removed list') @@ -89,17 +89,17 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) - ECS.addComponentToEntity(w, e, 'aabb', { abc: true }) - ECS.addComponentToEntity(w, e, 'transform') + ECS.addComponent(w, e, 'aabb', { abc: true }) + ECS.addComponent(w, e, 'transform') tap.equal(ECS.getEntities(w, [ 'aabb' ]).length, 1) const deferredRemoval = false - ECS.removeComponentFromEntity(w, e, 'aabb', deferredRemoval) + ECS.removeComponent(w, e, 'aabb', deferredRemoval) tap.equal(ECS.getEntities(w, [ 'aabb' ]).length, 0) tap.same(w.entities, [ { transform: {} } ], 'immediately remove component from entity') @@ -110,15 +110,15 @@ import tap from 'tap' // removing component named 'A' should not break filter for component 'AB' { - const w = ECS.createWorld() - const e = ECS.createEntity(w) + const w = ECS.addWorld() + const e = ECS.addEntity(w) - ECS.addComponentToEntity(w, e, 'tilda') - ECS.addComponentToEntity(w, e, 'matilda') + ECS.addComponent(w, e, 'tilda') + ECS.addComponent(w, e, 'matilda') tap.equal(ECS.getEntities(w, [ 'matilda' ]).length, 1) const deferredRemoval = false - ECS.removeComponentFromEntity(w, e, 'tilda', deferredRemoval) + ECS.removeComponent(w, e, 'tilda', deferredRemoval) tap.equal(ECS.getEntities(w, [ 'matilda' ]).length, 1) } @@ -128,15 +128,15 @@ import tap from 'tap' // if we immediately remove an entity (non-deferred removal) then we re-process the // world.deferredRemovals.components strings and shift up all the idx values > the idx of the removed entity - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) - const e2 = ECS.createEntity(w) + const e2 = ECS.addEntity(w) - ECS.addComponentToEntity(w, e2, 'hitHero') + ECS.addComponent(w, e2, 'hitHero') - ECS.removeComponentFromEntity(w, e2, 'hitHero') + ECS.removeComponent(w, e2, 'hitHero') ECS.removeEntity(w, e, false) diff --git a/test/removeEntity.js b/test/removeEntity.js index a0cfb76..af6d735 100644 --- a/test/removeEntity.js +++ b/test/removeEntity.js @@ -3,9 +3,9 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) tap.equal(w.entities.length, 1) @@ -25,15 +25,15 @@ import tap from 'tap' { - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) ECS.getEntities(w, [ 'a' ]) // filter 1 ECS.getEntities(w, [ 'a', 'b' ]) - ECS.addComponentToEntity(w, e, 'a', { a: 23 }) - ECS.addComponentToEntity(w, e, 'b', { b: 64 }) + ECS.addComponent(w, e, 'a', { a: 23 }) + ECS.addComponent(w, e, 'b', { b: 64 }) tap.equal(w.filters['a'].length, 1) tap.equal(w.filters['a,b'].length, 1) @@ -49,16 +49,16 @@ import tap from 'tap' // while iterating over entities, removing an unvisited entity still gets processed // because the removal is defferred until the cleanup step { - const w2 = ECS.createWorld() + const w2 = ECS.addWorld() - const e3 = ECS.createEntity(w2) - ECS.addComponentToEntity(w2, e3, 'position', 'e3') + const e3 = ECS.addEntity(w2) + ECS.addComponent(w2, e3, 'position', 'e3') - const e4 = ECS.createEntity(w2) - ECS.addComponentToEntity(w2, e4, 'position', 'e4') + const e4 = ECS.addEntity(w2) + ECS.addComponent(w2, e4, 'position', 'e4') - const e5 = ECS.createEntity(w2) - ECS.addComponentToEntity(w2, e5, 'position', 'e5') + const e5 = ECS.addEntity(w2) + ECS.addComponent(w2, e5, 'position', 'e5') let i = 0 const processed = { } @@ -76,10 +76,10 @@ import tap from 'tap' tap.same(processed, { e3: true, e4: true, e5: true }, 'all entities processed because of deferred removal') - const w3 = ECS.createWorld() + const w3 = ECS.addWorld() - const e6 = ECS.createEntity(w3) - ECS.addComponentToEntity(w3, e6, 'position') + const e6 = ECS.addEntity(w3) + ECS.addComponent(w3, e6, 'position') ECS.getEntities(w3, [ 'position' ], 'removed') @@ -105,10 +105,10 @@ import tap from 'tap' { // deferred removal should immediately remove the entity - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) - ECS.addComponentToEntity(w, e, 'testc') + const e = ECS.addEntity(w) + ECS.addComponent(w, e, 'testc') tap.equal(ECS.getEntities(w, [ 'testc' ]).length, 1, '1 entity should be present') @@ -121,16 +121,16 @@ import tap from 'tap' { // removing an entity immediately doesn't screw up removing other entities via deferred removal - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) - ECS.addComponentToEntity(w, e, 'e1') + const e = ECS.addEntity(w) + ECS.addComponent(w, e, 'e1') - const e2 = ECS.createEntity(w) - ECS.addComponentToEntity(w, e2, 'e2') + const e2 = ECS.addEntity(w) + ECS.addComponent(w, e2, 'e2') - const e3 = ECS.createEntity(w) - ECS.addComponentToEntity(w, e3, 'e3') + const e3 = ECS.addEntity(w) + ECS.addComponent(w, e3, 'e3') ECS.cleanup(w) @@ -148,10 +148,10 @@ import tap from 'tap' { // defer remove an entity, then remove it immediately. Prior to cleanup should empty the deferredRemovals set. - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) - ECS.addComponentToEntity(w, e, 'derp') + const e = ECS.addEntity(w) + ECS.addComponent(w, e, 'derp') ECS.cleanup(w) @@ -178,9 +178,9 @@ import tap from 'tap' { // remove an entity immediately, then deferred remove it. Should still provide listners - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) ECS.cleanup(w) @@ -201,17 +201,17 @@ import tap from 'tap' { // remove all values from the deferredRemovals.components list for an entity we just insta-removed - const w = ECS.createWorld() + const w = ECS.addWorld() - const e = ECS.createEntity(w) + const e = ECS.addEntity(w) - ECS.addComponentToEntity(w, e, 'a') - ECS.addComponentToEntity(w, e, 'b') + ECS.addComponent(w, e, 'a') + ECS.addComponent(w, e, 'b') ECS.cleanup(w) - ECS.removeComponentFromEntity(w, e, 'a') - ECS.removeComponentFromEntity(w, e, 'b') + ECS.removeComponent(w, e, 'a') + ECS.removeComponent(w, e, 'b') const deferredRemoval = false ECS.removeEntity(w, e, deferredRemoval) diff --git a/test/stats.js b/test/stats.js index 33e102a..749873c 100644 --- a/test/stats.js +++ b/test/stats.js @@ -10,7 +10,7 @@ async function sleep (ms) { async function main () { - const w = ECS.createWorld() + const w = ECS.addWorld() tap.same(w.stats, { entityCount: 0, @@ -22,9 +22,9 @@ async function main () { }, 'initial stats data') - const e = ECS.createEntity(w) - ECS.addComponentToEntity(w, e, 'componentId1') - ECS.addComponentToEntity(w, e, 'componentId2') + const e = ECS.addEntity(w) + ECS.addComponent(w, e, 'componentId1') + ECS.addComponent(w, e, 'componentId2') tap.same(w.stats, { entityCount: 1, @@ -40,10 +40,10 @@ async function main () { - const w2 = ECS.createWorld() - const e2 = ECS.createEntity(w2) - ECS.addComponentToEntity(w2, e2, 'componentId1') - ECS.addComponentToEntity(w2, e2, 'componentId1') + const w2 = ECS.addWorld() + const e2 = ECS.addEntity(w2) + ECS.addComponent(w2, e2, 'componentId1') + ECS.addComponent(w2, e2, 'componentId1') tap.same(w2.stats, { entityCount: 1, diff --git a/types/ecs.d.ts b/types/ecs.d.ts index 25c6077..0611f2b 100644 --- a/types/ecs.d.ts +++ b/types/ecs.d.ts @@ -210,6 +210,10 @@ declare namespace _default { export { preUpdate }; export { postUpdate }; export { cleanup }; + export { createWorld as addWorld }; + export { createEntity as addEntity }; + export { addComponentToEntity as addComponent }; + export { removeComponentFromEntity as removeComponent }; } export default _default; export type ListenerType = "added" | "removed"; diff --git a/types/ecs.d.ts.map b/types/ecs.d.ts.map index eb2562c..11f39bb 100644 --- a/types/ecs.d.ts.map +++ b/types/ecs.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"ecs.d.ts","sourceRoot":"","sources":["../ecs.js"],"names":[],"mappings":"AAOA;;GAEG;AAEH;;GAEG;AAEH;;GAEG;AAEH;;;;GAIG;AAEH;;GAEG;AAEH;;GAEG;AAEH;;;;;;;;GAQG;AAEH;;;;;GAKG;AAEH;;GAEG;AAEH;;GAEG;AAEH;;;;GAIG;AAEH;;GAEG;AAEH;;;;GAIG;AAE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