]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/serpentity.js
d1c1cb0cbfc3f70f0a89cbef249c3884020fe36a
4 Serpentity is a simple entity framework inspired by Ash.
10 ## Instantiating an engine
12 var engine = Serpentity();
14 Add entities or systems:
16 engine.addEntity(entityFactory());
17 engine.addSystem(new GameSystem());
23 Remove entities or systems:
25 engine.removeEntity(entityReference);
26 engine.removeSystem(systemReference);
30 Entities are the basic object of Serpentity, and they do nothing.
32 var entity = new Serpentity.Entity();
34 All the behavior is added through components
36 ## Creating Components
38 Components define data that we can add to an entity. This data will
39 eventually be consumed by "Systems"
41 Class("PositionComponent").inherits(Serpentity.Component)({
48 You can add components to entities by using the add method:
50 entity.add(new PositionComponent());
53 Systems can refer to entities by requesting nodes.
57 Nodes are sets of components that you define, so your system can require
58 entities that always follow the API defined in the node.
60 Class("MovementNode").inherits(Serpentity.Node)({
62 position : PositionComponent,
63 motion : MotionComponent
67 You can then request an array of all the nodes representing entities
68 that comply with that API
70 engine.getNodes(MovementNode);
74 Systems are called on every update, and they use components through nodes.
76 Class("TestSystem").inherits(Serpentity.System)({
78 added : function added(engine){
79 this.nodeList = engine.getNodes(MovementNode);
81 removed : function removed(engine){
82 this.nodeList = undefined;
84 update : function update(dt){
85 this.nodeList.forEach(function (node) {
86 console.log("Current position is: " + node.position.x + "," + node.position.y);
94 Just run `engine.update(dt)` in your game loop :D
100 nodeCollections : null,
103 init : function init(config
) {
106 config
= config
|| {};
110 this.nodeCollections
= {};
112 for (property
in config
) {
113 if (config
.hasOwnProperty(property
)) {
114 this[property
] = config
[property
];
120 * Adds a system to the engine, so its update method will be called
121 * with the others. Triggers added hook.
123 * returns true if added succesfully, false if already added
125 addSystem : function addSystem(system
) {
126 if (this.systems
.indexOf(system
) >= 0) {
129 this.systems
.push(system
);
135 * Removes a system from the engine, so its update method will no
136 * longer will be called. Triggers the removed hook.
138 * returns true if removed succesfully, false if already added
140 removeSystem : function removeSystem(system
) {
143 position
= this.systems
.indexOf(system
);
145 this.systems
[position
].removed(this);
146 this.systems
.splice(position
, 1);
154 * Adds an entity to the engine, adds to existing node collections
156 * returns true if added, false if already there
158 addEntity : function addEntity(entity
) {
161 if (this.entities
.indexOf(entity
) >= 0) {
164 this.entities
.push(entity
);
166 for (property
in this.nodeCollections
) {
167 if (this.nodeCollections
.hasOwnProperty(property
)) {
168 this.nodeCollections
[property
].add(entity
);
175 * Removes entity from system, removing from all node collections
177 * returns true if removed, false if not present
179 removeEntity : function removeEntity(entity
) {
182 position
= this.entities
.indexOf(entity
);
184 for (property
in this.nodeCollections
) {
185 if (this.nodeCollections
.hasOwnProperty(property
)) {
186 this.nodeCollections
[property
].remove(entity
);
190 this.entities
.splice(position
, 1);
198 * Given a Node Class, retrieves a list of all the nodes for each
201 getNodes : function getNodes(nodeType
) {
204 if (this.nodeCollections
.hasOwnProperty(nodeType
)) {
205 return this.nodeCollections
[nodeType
].nodes
;
208 nodeCollection
= new Serpentity
.NodeCollection({
211 this.nodeCollections
[nodeType
] = nodeCollection
;
213 this.entities
.forEach(function (entity
) {
214 nodeCollection
.add(entity
);
217 return nodeCollection
.nodes
;
221 * Calls update for every loaded system.
223 update : function update(dt
) {
224 this.systems
.forEach(function (system
) {
231 require("./component.js");
232 require("./entity.js");
233 require("./node.js");
234 require("./node_collection.js");
235 require("./system.js");