Jecs Entity Component System
⚠ Version 2.0.0 breaking changes ⚠
Some changes in version 2.0.0 are not backward compatible.
Please read the migration guide here: 2.0.0 migration guide
Description
ECS or Entity Component System is an architectural pattern particularly suitable for game development.
Jecs is a javascript ECS engine for Node.js and the browser.
Installation
NodeJs with npm:
npm install jecs
NodeJs with yarn:
yarn add jecs
Documentation
You can find the documentation here or under the "docs" directory.
Usage in the browser
You can use jecs in a browser by loading the script in the directory 'browser'. The library exports the Ecs class as a global Ecs variable (window.Ecs).
<script type="text/javascript" src="path/to/jecs_min.js"></script>
Live demo
Included in this package you can find a simple text-based browser game demo using Jecs (actually it's a simulation because there is no user input...).
See the demo here. You can find the source code under the "docs/example" directory.
Example
In this example we create a "player" entity and assign it the "position" and "speed" components.
We define also a "move" system requiring "position" and "speed" components.
Components can be any type of data, they will be passed to the systems without any modification.
The "move" system simply update position values by adding speed values and will be called once for every entity associated to both components (in this case there is only one, "player").
The Simulator utility will update the engine for us taking care of limiting the maximum fps to 60.
If you prefer, you can avoid using the simulator and directly call ecs.tick() to start next engine iteration.
; // Instantiate a new engineconst engine = ; // Declare a 'player' entityconst player = engine; // Associate the player entity to components.// In this case we set 'position' and 'speed'.player;player; // Define a 'move' system for updating position of// entities associated to components 'position' and 'speed'engine; // Instantiate a simulator// If you prefer, you can avoid using simulator and start// engine iterations manually by calling engine.tick() in a loop.const sim = engine; // Limit the fps to 60sim; // Start simulatorsimstart;
Controlling speed
The simulator fps limiter can limit the maximum frames per second. The limiter, wich is disabled by default, can be activated by calling Simulator.setFps(<value>)
with value being a integer greater then 0. A value of 0 will disable the limiter.
The limiter can be handy in some situation but generally what you really want is to run at the maximum possible speed...
In order to get a consistent animation speed, your systems need to know how much time is passed since the previous call.
The Timer helper class
You can use the Timer helper class to get timings informations in your systems.
You just have to instantiate the class passing your engine instance as parameter, then you can call the Timer getTime() method from your systems to get useful timings informations.
const engine = ;const timer = engine; engine;
timer.getTime() returns a object with these properties:
ticks // Number of ticks (frames) start // Initial time (milliseconds since the EPOCH) now // Current time (milliseconds since the EPOCH) total // Total execution time (milliseconds) delta // Delta time from prev tick (milliseconds)
You can call timer.reset() method to reset all timing values to zero.
Version 2.0.0 migration guide
Version 2.0.0 introduces some non backward compatible changes.
This guide will help you to update your code.
Classes location
Versions prior 2.0.0 exported one single class, the Ecs
class.
The Entity
, System
, Timer
and Simulator
classes where exposed by the Ecs
class.
In version 2.0.0, the Ecs
class is renamed to Engine
and the main module, jecs
, exports all the classes you are supposed to directly istantiate: Engine
, Simulator
and Timer
.
Entity
and System
are not directly available, but they were never meant to be directly istantiated anyway. In order to get a new Entity
or System
istance you have to call the Engine entity()
and system()
methods.
Prior 2.0.0:
; const ecs = ;const simulator = ecs;const timer = ecs;
After 2.0.0:
; const engine = ;const simulator = engine;const timer = engine;
Events emitted before and after running the systems
Versions prior 2.0.0 exposed the Ecs
class static string properties TICK_BEFORE
and TICK_AFTER
.
Theese properties vaules represented the names of the events emitted just before and after running the systems, respectively.
In 2.0.0, the properties have been removed from the new Engine
class (which replaces the old Ecs
class). You will have to use the event names directly.
The event names have been changed to a more standard form too:
tick-before
becometick:before
tick-after
becometick:after
.
Prior 2.0.0:
ecs);ecs);
After 2.0.0:
engine);engine);
License
MIT License
Copyright (c) 2018 Valerio Bianchi
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.