• Stars
    star
    250
  • Rank 161,451 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Entity-Component-System library for JavaScript.

Ape-ECS

Ape-ECS Hero

npm Build Status Coveralls github PRs Welcome

chat on discord @fritzy twitter

A performant, featureful, and flexible Entity-Component-System library for JavaScript, written in ECMAScript ES2018, intended for use in games and simulations.

Documentation

Install

npm install ape-ecs 

Differentiating Features

  • Advanced Queries for entities.
  • Persisted Queries (indexes) are updated as Entity composition changes.
  • Component reference properties to Entities (EntityRef, EntitySet, EntityObject)
    • When a referenced entity is destroyed, the property is updated to null.
    • Subscribe-able events for adding and removing references.
    • Reverse query from entity to entity-components that reference it.
  • Not all systems need to run every frame.
  • Export/import support for saving/restoring state with component-level serialization configuration.
  • 100% Test Coverage.

Example

const ApeECS = require('ape-ecs');

class Gravity extends ApeECS.System {
  init() {
    this.mainQuery = this.createQuery().fromAll('Position', 'Physics');
  }

  update(tick) {
    const entities = this.mainQuery.execute();
    const frameInfo = this.world.getEntity('frame');
    for (const entity of entities) {
      const point = entity.getOne('Position');
      if (!entity.has('Vector')) {
        entity.addComponent({
          type: 'Vector',
          mx: 0,
          my: 0
        })
      }
      const vector = entity.getOne('Vector');
      vector.my += 9.807 * frame.time.deltaTime * .01;
      vector.update();
    }
  }
}

class Position extends ApeECS.Component {}
Position.properties = {
  x: 0,
  y: 0
};

class Vector extends ApeECS.Component {

  get speed() {
    return Math.sqrt(this.mx**2 + this.my**2);
  }
}
Vector.properties = {
  mx: 0,
  my: 0,
  speed: 0
};

class FrameInfo extends ApeECS.Component {}
FrameInfo.properties = {
  deltaTime: 0,
  deltaFrame: 0,
  time: 0
};

const world = new ApeECS.World();
world.registerComponent(Position);
world.registerComponent(Vectory);
world.registerComponent(FrameInfo);
world.registerTags('Physics');
world.registerSystem('frame', Gravity);

const frame = world.createEntity({
  id: 'frame',
  c: {
    time: {
      type: 'FrameInfo',
    }
  }
})

// see world.creatEntity and world.createEntities
// in docs/World.md for more details
world.registerSystem('frame', require('./move.js'));
world.createEntities(require('./saveGame.json'));

let lastTime = 0;

function update(time) {
  const delta = time - lastTime;
  time = lastTime;
  frame.time.update({
    time: time,
    deltaTime: delta,
    deltaFrame: delta / 16.667
  });
  world.runSystems('frame');
  // run update again the next browser render call
  // every 16ms or so
  window.requestAnimationFrame(update);
}
update(0);

More About ECS

The Entity-Component-System paradigm is great for managing dynamic objects in games and simulations. Instead of binding functionality to data through methods, entities are dynamically composed of any combination of types. Separate systems are then able to query for entities with a given set of types.

ECS's dynamic data composition and freely interacting systems leads to:

  • More complex and dynamic composition than OOP
  • Improved performance due to lack of API methods
  • Emergent Gameplay with logical behavior extended beyond the programmer's vision.

This library has been inspired in part by:

Example Game

An in-progress arcade game Missile Orders.

This game is not in a complete state yet, nor does it show off all of the potential of ECS yet.

Running the Tests

The goal is to keep test coverage at 100%.

git clone [email protected]/fritzy/ape-ecs.git
cd ape-ecs
npm install
npm test

Contributors

  • Ben Morse -- Ben is an early adopter that provided a lot of insight and you have him to thank for the TypeScript definitions! Ben has a game, Super Game of Life that uses Ape ECS.

Special Thanks

More Repositories

1

SleekXMPP

Python 2.6+/3.1+ XMPP Library
Python
1,102
star
2

Dulcimer

Dulcimer is a Node.js ORM for keystores.
JavaScript
79
star
3

VeryModel

A JavaScript model system for validation, creation, and editing of models.
JavaScript
55
star
4

SleekPubsub

Publish-Subscribe Component w/ XMPP, REST, and Ad-hoc interfaces
Python
27
star
5

node-neovim

NeoVIM RPC lib for Node.js
JavaScript
26
star
6

staydown

Keeps a DOM element scrolled down, based on user intention.
JavaScript
24
star
7

node-redisscan

Recursively scan through Redis keys.
JavaScript
22
star
8

node-azure-media

JavaScript
15
star
9

thoonk.js

Thoonk.js: Persistent (and fast!) push feeds, queues, and jobs leveraging Redis for node.js.
JavaScript
13
star
10

pgdown

postgres backend for levelup
JavaScript
12
star
11

thoonk.py

Thoonk.py!
Python
12
star
12

thoonk-jobs.js

JavaScript
11
star
13

spacewar.pro

JavaScript
9
star
14

pie

JavaScript
9
star
15

REST-MUC

An HTTP interface for creating, configuring, destroying rooms as well as inviting and kicking users.
Python
9
star
16

jsrogue-entity-component

Roguelike Component System in JavaScript ES6
JavaScript
9
star
17

gatepost

Node.js module for binding postgres queries to models.
JavaScript
8
star
18

SleekComponent

XMPP Component Framework for Sleek that includes presence and rosters.
Python
5
star
19

ecs-js-example

A classic roguelike example for the usage of fritzy/ecs-js
JavaScript
5
star
20

jsdice

JavaScript
5
star
21

paddle

You are up a creek; here is your Paddle. Paddle provides a way of ensuring that JS asynchronous callbacks are actually ran, or calls a provided error function upon timeout.
JavaScript
5
star
22

gh-watchmyself

Summarize which github repos you're watching and not.
JavaScript
4
star
23

github-cli-auth

Node github cli interface for authenticating, getting 2factor auth, and storing and retrieving the token.
JavaScript
4
star
24

tcgdevclub-oct2019jam

C#
3
star
25

padlock

An execution locking mechanism for Node.js
JavaScript
3
star
26

verymodel-level

JavaScript
3
star
27

Jorb

XMPP Pubsub Job Blather Extension
Ruby
2
star
28

level-dulcimer

Dulcimer's requirements for a level backend, all wrapped into one.
JavaScript
2
star
29

MissileOrders

JavaScript
2
star
30

level-atomichooks

Extensions hooks for levelup.
JavaScript
2
star
31

pgboom

A postgres error caster for Hapi
JavaScript
2
star
32

fritzy.github.com

Nathan Fritz's projects
JavaScript
2
star
33

level-foreignkeys

Add foreign key indexing to levelup.
JavaScript
1
star
34

posthole

Postgres Object Store ORM for Node.js
JavaScript
1
star
35

riakdown-indextotal

Get the index totals when using a riakdown backend with levelup.
JavaScript
1
star
36

level-bucket

Buckets for leveldb.
JavaScript
1
star
37

level-2i

Secondary indexes for leveldb.
JavaScript
1
star
38

level-bucketarray

JavaScript
1
star
39

dotfiles

Vim Script
1
star
40

level-streams

Useful read, transform, and write streams for working with levelup.
JavaScript
1
star
41

fritzy

1
star
42

goalpost

JavaScript
1
star
43

deputy

Local Key-Bucket and Directory Database
JavaScript
1
star
44

level-increment

Incrementer for level.
JavaScript
1
star
45

drboom-pg

DrBoom plugin for Postgresql errors.
JavaScript
1
star
46

level-continuation

JavaScript
1
star
47

xmastruck

xmas truck
JavaScript
1
star
48

riak-dulcimer

JavaScript
1
star
49

base60fill

Node.js left-fill base60 for lexicographically sorting numbers
JavaScript
1
star
50

riakdown-increment

increment access to riakdown
JavaScript
1
star
51

sqlmoses-oracle

JavaScript
1
star
52

gists.fritzy.io

gists blog posts
Makefile
1
star
53

verymodel-hapi

JavaScript
1
star