• Stars
    star
    185
  • Rank 204,032 (Top 5 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated over 6 years ago

Reviews

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

Repository Details

Component-Entity-System framework for JavaScript games.

CES.js

CES.js is a lightweight Component-Entity-System framework for JavaScript games.

Basic Usage

To define a new component, simply extend CES.Component. Note that each component should have a unique name property.

var Position = CES.Component.extend({
    name: 'position',
    init: function (x, y) {
        this.x = x;
        this.y = y;
    }
});

var Velocity = CES.Component.extend({
    name: 'velocity',
    init: function (x, y) {
        this.x = x;
        this.y = y;
    }
});

var Health = CES.Component.extend({
    name: 'health',
    init: function (maxHealth) {
        this.health = this.maxHealth = maxHealth;
    },
    isDead: function () {
        return this.health <= 0;
    },
    receiveDamage: function (damage) {
        this.health -= damage;
    }
});

An entity is essentially a container of one or more components.

var hero = new CES.Entity();
hero.addComponent(new Position(0, 0));
hero.addComponent(new Velocity(0, 0));
hero.addComponent(new Health(100));

The system is responsible for updating the entities. In a real game there may be a lot of systems, like CollisionSystem, RenderSystem, ControlSystem etc.

var PhysicSystem = CES.System.extend({
    update: function (dt) {
        var entities, position, velocity;

        entities = this.world.getEntities('position', 'velocity');

        entities.forEach(function (entity) {
            position = entity.getComponent('position');
            velocity = entity.getComponent('velocity');
            position.x += velocity.x * dt;
            position.y += velocity.y * dt;
        });
    }
});

The world is the container of all the entities and systems. Calling the update method will sequentially update all the systems, in the order they were added.

var world = new CES.World();

world.addEntity(hero);
// ... add other entities

world.addSystem(new PhysicSystem());
// ... add other systems

requestAnimationFrame(function () {
    world.update(/* interval */);
})

A system is notified when it is added or removed from the world:

var MySystem = CES.System.extend({
  addedToWorld: function(world)  {
    // Code to handle being added to world. Remeber to call this._super.
    this._super(world);
  },
  removedFromWorld: function(world) {
    // Code to handle being removed from world.
    this._super(world);
  }
});

The world emits signals when entities are added or removed. You can listen for specific entities and handle the signal accordingly:

var MySystem = CES.System.extend({
  addedToWorld: function(world) {
    world.entityAdded('position', 'velocity').add(function(entity) {
      // This function is called whenever an entity with both 'position' and
      // 'velocity' components is added to the world. It can also be called when
      // a component is added to an entity; for example, when an entity with
      // only 'position' has 'velocity' added to it.
    });
    world.entityRemoved('position', 'velocity').add(function(entity) {
      // This function is called whenever an entity with both 'position' and
      // 'velocity' components is removed from the world. It can also be called 
      // when a component is removed from an entity; for example, when an entity
      // with both 'position' and 'velocity' has 'velocity' removed from it.
    });
  }
});

Installation (Browser)

Download the minified js file and include it in your web page.

<script type="text/javascript" src="./ces-browser.min.js"></script>

Installation (Node.js)

If you want to use it in Node.js, you may install it via npm.

npm install ces

Then, in your program:

var CES = require('ces');

Development

Layout:

.
|-- dist         # browser distribution
|-- src          # source code
`-- test         # test scripts

You will need to install node.js and use npm to install the dependencies:

npm install -d 

To build the browser distribution (It will use node-browserify to generate a browser distribution, and use UglifyJS to compress):

make

To run the tests with mocha and should.js

make test

License

MIT License

Copyright 2013 Xueqiao Xu <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

PathFinding.js

A comprehensive path-finding library for grid based games
JavaScript
8,327
star
2

euphony

MIDI visualizer in WebGL
JavaScript
846
star
3

difflib.js

Text diff library in JavaScript, ported from Python's difflib module.
CoffeeScript
245
star
4

sicp-solutions

My solutions to Structure and Interpretation of Computer Programs
Scheme
240
star
5

fractal-terrain-generator

A random fractal terrain generator in JavaScript
JavaScript
214
star
6

heap.js

A binary heap implementation in CoffeeScript/JavaScript.
CoffeeScript
123
star
7

ProgressCircle.js

A progress indicator in HTML5 canvas
JavaScript
114
star
8

coffee-box

A blog engine built upon Node.js, Express, MongoDB and CoffeeScript.
JavaScript
71
star
9

fingertree.js

Implementation of Finger Tree in JavaScript.
JavaScript
45
star
10

visual.js

JavaScript 3D library for dummies
JavaScript
33
star
11

mdwatch

Preview markdown document with live updates
JavaScript
25
star
12

vimfiles

My vim plugins and configuration
Vim Script
23
star
13

javascript-playground

A collection of my toy codes when I just started to learn javascript.
JavaScript
21
star
14

r7rs.js

R7RS compliant Scheme implementation in JavaScript (Work in Progress)
JavaScript
18
star
15

immutable-sequence.js

High performance implementation of Immutable Sequence in JavaScript, based on Finger Tree.
JavaScript
16
star
16

typedef.me

My blog at http://typedef.me in Ruby on Rails. (Deprecated)
Ruby
9
star
17

GR-JSON-Reader

Reader for exported Google Reader JSON files
Python
6
star
18

clando

A command-line todo list manager for minimalists
Common Lisp
2
star
19

scroll-progress

A small chrome extension showing the scroll progress of current page.
JavaScript
2
star