3.0.0 • Published 5 years ago

gg-entities v3.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

GG-Entities

easy-to-use Entity-Component System for browsers and Node.js

Usage

import { EntityManager } from 'gg-entities'

const entityManager = new EntityManager()

// 1. Register Components

const pos = entityManager.registerComponent('position', {
    x : 10.0,
    y : 10.0
})

const vel = entityManager.registerComponent('velocity', 2.0)

// 2. Register Systems

function movementSystem(entities, { delta }) {
    for (const {entity} of entities) {
        entity[pos].x += entity[vel] * delta
    }
}

entityManager.registerLogicSystem([ pos, vel ], movementSystem)

function logSystem(entities) {
    for (const {entity} of entities) {
        console.log(entity[pos].x, entity[pos].y)
    }
}

entityManager.registerRenderSystem([ pos ], logSystem)

// 3. Add an entity

entityManager
    .build()
    .withComponent(pos)
    .withComponent(vel)
    .create(1)

// 4. Run the systems

entityManager.onLogic({ delta: 16 })  // invokes all logic systems (movementSystem)
entityManager.onRender({ delta: 16 }) // invokes all render systems (logSystem)

Features

Examples

Docs

http://ggalansmithee.github.io/Entities/

Typings

There are no official typings (yet), but @ForsakenHarmony has provided a gist for TypeScript users.

FAQ / Gotchas

  • Since a system is bound with the EntityManager as its context, a system must be a regular function (not an es6 arrow function)

Tips and tricks

Accessing an entity's components in a system usually looks like this

function movementSystem(entities) {
    for (const { entity } of entities) {
        entity[POS_COMPONENT].x += entity[VEL_COMPONENT].x * delta
        entity[POS_COMPONENT].y += entity[VEL_COMPONENT].y * delta
    }
}

which can be a bit ugly, especially if your entity has a lot of components which are accessed multiple times. Using some ES6 (computed property keys) and ES7 (object spread) magic, we can make it a bit more concise:

function movementSystem(entities) {
    for (const { entity: { [POS_COMPONENT]: pos, [VEL_COMPONENT]: vel } } of entities) {
        pos.x += vel.x * delta
        pos.y += vel.y * delta
    }
}

Get involved

  • Got questions or want to leave feedback? create an issue

  • Got improvements? Feel free to send a PR to the dev branch (don't forget to add tests)

    1. npm run build builds the project
    2. npm run test runs the test suite
4.0.0-beta.2

5 years ago

4.0.0-beta.1

5 years ago

3.0.0

6 years ago

3.0.0-beta.2

6 years ago

3.0.0-beta.1

6 years ago

3.0.0-alpha.2

6 years ago

3.0.0-alpha.1

6 years ago

2.4.0-beta.1

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.4

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.0

7 years ago

2.0.0-beta.9

8 years ago

2.0.0-beta.8

8 years ago

2.0.0-beta.7

8 years ago

2.0.0-beta.6

8 years ago

2.0.0-beta.5

8 years ago

2.0.0-beta.4

8 years ago

2.0.0-beta.3

8 years ago

2.0.0-beta.2

8 years ago

2.0.0-beta.1

8 years ago

2.0.0-alpha.3

8 years ago

2.0.0-alpha.2.1

8 years ago

2.0.0-alpha.2

8 years ago

2.0.0-alpha.1

8 years ago

1.1.0

8 years ago

1.0.4

9 years ago

2.0.1

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago