To define a new component, simply extend
Note that each component should have a unique
var Position = CESComponentextendname: 'position'thisx = x;thisy = y;;var Velocity = CESComponentextendname: 'velocity'thisx = x;thisy = y;;var Health = CESComponentextendname: 'health'thishealth = thismaxHealth = maxHealth;return thishealth <= 0;thishealth -= damage;;
An entity is essentially a container of one or more components.
var hero = ;heroaddComponent0 0;heroaddComponent0 0;heroaddComponent100;
The system is responsible for updating the entities.
In a real game there may be a lot of systems, like
var PhysicSystem = CESSystemextendvar entities position velocity;entities = thisworldgetEntities'position' 'velocity';entitiesforEachposition = entitygetComponent'position';velocity = entitygetComponent'velocity';positionx += velocityx * dt;positiony += velocityy * dt;;;
The world is the container of all the entities and systems.
update method will sequentially update all the systems,
in the order they were added.
var world = ;worldaddEntityhero;// ... add other entitiesworldaddSystem;// ... add other systemsrequestAnimationFrameworldupdate/* interval */;
A system is notified when it is added or removed from the world:
var MySystem = CESSystemextend// Code to handle being added to world. Remeber to call this._super.this_superworld;// Code to handle being removed from world.this_superworld;;
The world emits signals when entities are added or removed. You can listen for specific entities and handle the signal accordingly:
var MySystem = CESSystemextendworldentityAdded'position' 'velocity'add// 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.;worldentityRemoved'position' 'velocity'add// 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.;;
Download the minified js file and include it in your web page.
If you want to use it in Node.js, you may install it via
npm install ces
Then, in your program:
var CES = require'ces';
. |-- 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
Copyright 2013 Xueqiao Xu <firstname.lastname@example.org>
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.