Skip to content

Commit

Permalink
use performance.now() everywhere. Create shorter aliased names for se…
Browse files Browse the repository at this point in the history
…veral functions
  • Loading branch information
mreinstein committed Oct 30, 2024
1 parent b58f6c6 commit ebeed90
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 187 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
```
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 19 additions & 16 deletions ecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -697,5 +694,11 @@ export default {
update,
preUpdate,
postUpdate,
cleanup
cleanup,

// aliases. shorter == nicer :)
addWorld: createWorld,
addEntity: createEntity,
addComponent: addComponentToEntity,
removeComponent: removeComponentFromEntity
}
6 changes: 3 additions & 3 deletions examples/events/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
})
}
Expand Down
12 changes: 6 additions & 6 deletions test/addComponentToEntity.js → test/addComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions test/createEntity.js → test/addEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ])
2 changes: 1 addition & 1 deletion test/addSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ECS from '../ecs.js'
import tap from 'tap'


const w = ECS.createWorld()
const w = ECS.addWorld()


function boringSystem (world) {
Expand Down
2 changes: 1 addition & 1 deletion test/createWorld.js → test/addWorld.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
6 changes: 6 additions & 0 deletions test/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
16 changes: 8 additions & 8 deletions test/getEntities.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import ECS from '../ecs.js'
import tap from 'tap'


const w = ECS.createWorld()
const w = ECS.addWorld()

const entities = ECS.getEntities(w, [ 'position' ])

tap.equal(w.filters.position.length, 0, 'querying a filter creates it the first time')



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')

Expand All @@ -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')
22 changes: 11 additions & 11 deletions test/getEntitiesNotModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ])
Expand All @@ -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)

Expand Down
Loading

0 comments on commit ebeed90

Please sign in to comment.