]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/serpentity.js
18985c56891c27941f2ac6d64d699d9b6d9af5b7
1 if (typeof require
!== "undefined") {
6 Serpentity is a simple entity framework inspired by Ash.
10 require('serpentity');
12 ## Instantiating an engine
14 var engine = Serpentity();
16 Add entities or systems, systems are added with a priority (the smaller
17 the number, the earlier it will be called):
19 engine.addEntity(entityFactory());
20 engine.addSystem(new GameSystem(), priority);
26 Remove entities or systems:
28 engine.removeEntity(entityReference);
29 engine.removeSystem(systemReference);
33 Entities are the basic object of Serpentity, and they do nothing.
35 var entity = new Serpentity.Entity();
37 All the behavior is added through components
39 ## Creating Components
41 Components define data that we can add to an entity. This data will
42 eventually be consumed by "Systems"
44 Class("PositionComponent").inherits(Serpentity.Component)({
51 You can add components to entities by using the add method:
53 entity.add(new PositionComponent());
56 Systems can refer to entities by requesting nodes.
60 Nodes are sets of components that you define, so your system can require
61 entities that always follow the API defined in the node.
63 Class("MovementNode").inherits(Serpentity.Node)({
65 position : PositionComponent,
66 motion : MotionComponent
70 You can then request an array of all the nodes representing entities
71 that comply with that API
73 engine.getNodes(MovementNode);
77 Systems are called on every update, and they use components through nodes.
79 Class("TestSystem").inherits(Serpentity.System)({
81 added : function added(engine){
82 this.nodeList = engine.getNodes(MovementNode);
84 removed : function removed(engine){
85 this.nodeList = undefined;
87 update : function update(dt){
88 this.nodeList.forEach(function (node) {
89 console.log("Current position is: " + node.position.x + "," + node.position.y);
97 Just run `engine.update(dt)` in your game loop :D
100 Class("Serpentity")({
104 _nodeCollections : null,
105 _nodeCollectionKeys : null,
107 init : function init(config
) {
110 config
= config
|| {};
114 this._nodeCollections
= [];
115 this._nodeCollectionKeys
= [];
117 for (property
in config
) {
118 if (config
.hasOwnProperty(property
)) {
119 this[property
] = config
[property
];
125 * Adds a system to the engine, so its update method will be called
126 * with the others. Triggers added hook.
128 * returns true if added succesfully, false if already added
130 addSystem : function addSystem(system
, priority
) {
131 var lastIndex
, found
;
133 if (this.systems
.indexOf(system
) >= 0) {
137 system
.priority
= priority
;
142 this.systems
.some(function findPriority(existingSystem
, i
) {
144 if (existingSystem
.priority
>= system
.priority
) {
154 this.systems
.splice(lastIndex
, 0, system
);
160 * Removes a system from the engine, so its update method will no
161 * longer will be called. Triggers the removed hook.
163 * returns true if removed succesfully, false if already added
165 removeSystem : function removeSystem(system
) {
168 position
= this.systems
.indexOf(system
);
170 this.systems
[position
].removed(this);
171 this.systems
.splice(position
, 1);
179 * Adds an entity to the engine, adds to existing node collections
181 * returns true if added, false if already there
183 addEntity : function addEntity(entity
) {
184 if (this.entities
.indexOf(entity
) >= 0) {
187 this.entities
.push(entity
);
189 this._nodeCollections
.forEach(function (collection
) {
190 collection
.add(entity
);
197 * Removes entity from system, removing from all node collections
199 * returns true if removed, false if not present
201 removeEntity : function removeEntity(entity
) {
204 position
= this.entities
.indexOf(entity
);
206 this._nodeCollections
.forEach(function (collection
) {
207 collection
.remove(entity
);
210 this.entities
.splice(position
, 1);
218 * Given a Node Class, retrieves a list of all the nodes for each
221 getNodes : function getNodes(nodeType
) {
222 var position
, nodeCollection
;
224 position
= this._nodeCollectionKeys
.indexOf(nodeType
);
227 return this._nodeCollections
[position
].nodes
;
230 nodeCollection
= new Serpentity
.NodeCollection({
234 this._nodeCollectionKeys
.push(nodeType
);
235 this._nodeCollections
.push(nodeCollection
);
237 this.entities
.forEach(function (entity
) {
238 nodeCollection
.add(entity
);
241 return nodeCollection
.nodes
;
245 * Calls update for every loaded system.
247 update : function update(dt
) {
248 this.systems
.forEach(function (system
) {
255 if (typeof require
!== "undefined") {
256 require("./component.js");
257 require("./entity.js");
258 require("./node.js");
259 require("./node_collection.js");
260 require("./system.js");