]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/serpentity.js
d86c47e49fcbf112b1255682561f2d9a8278d19a
4 Serpentity is a simple entity framework inspired by Ash.
10 ## Instantiating an engine
12 var engine = Serpentity();
14 Add entities or systems, systems are added with a priority (the smaller
15 the number, the earlier it will be called):
17 engine.addEntity(entityFactory());
18 engine.addSystem(new GameSystem(), priority);
24 Remove entities or systems:
26 engine.removeEntity(entityReference);
27 engine.removeSystem(systemReference);
31 Entities are the basic object of Serpentity, and they do nothing.
33 var entity = new Serpentity.Entity();
35 All the behavior is added through components
37 ## Creating Components
39 Components define data that we can add to an entity. This data will
40 eventually be consumed by "Systems"
42 Class("PositionComponent").inherits(Serpentity.Component)({
49 You can add components to entities by using the add method:
51 entity.add(new PositionComponent());
54 Systems can refer to entities by requesting nodes.
58 Nodes are sets of components that you define, so your system can require
59 entities that always follow the API defined in the node.
61 Class("MovementNode").inherits(Serpentity.Node)({
63 position : PositionComponent,
64 motion : MotionComponent
68 You can then request an array of all the nodes representing entities
69 that comply with that API
71 engine.getNodes(MovementNode);
75 Systems are called on every update, and they use components through nodes.
77 Class("TestSystem").inherits(Serpentity.System)({
79 added : function added(engine){
80 this.nodeList = engine.getNodes(MovementNode);
82 removed : function removed(engine){
83 this.nodeList = undefined;
85 update : function update(dt){
86 this.nodeList.forEach(function (node) {
87 console.log("Current position is: " + node.position.x + "," + node.position.y);
95 Just run `engine.update(dt)` in your game loop :D
101 nodeCollections : null,
104 init : function init(config
) {
107 config
= config
|| {};
111 this.nodeCollections
= {};
113 for (property
in config
) {
114 if (config
.hasOwnProperty(property
)) {
115 this[property
] = config
[property
];
121 * Adds a system to the engine, so its update method will be called
122 * with the others. Triggers added hook.
124 * returns true if added succesfully, false if already added
126 addSystem : function addSystem(system
, priority
) {
127 var lastIndex
, found
;
129 if (this.systems
.indexOf(system
) >= 0) {
133 system
.priority
= priority
;
138 this.systems
.some(function findPriority(existingSystem
, i
) {
140 if (existingSystem
.priority
>= system
.priority
) {
150 this.systems
.splice(lastIndex
, 0, system
);
156 * Removes a system from the engine, so its update method will no
157 * longer will be called. Triggers the removed hook.
159 * returns true if removed succesfully, false if already added
161 removeSystem : function removeSystem(system
) {
164 position
= this.systems
.indexOf(system
);
166 this.systems
[position
].removed(this);
167 this.systems
.splice(position
, 1);
175 * Adds an entity to the engine, adds to existing node collections
177 * returns true if added, false if already there
179 addEntity : function addEntity(entity
) {
182 if (this.entities
.indexOf(entity
) >= 0) {
185 this.entities
.push(entity
);
187 for (property
in this.nodeCollections
) {
188 if (this.nodeCollections
.hasOwnProperty(property
)) {
189 this.nodeCollections
[property
].add(entity
);
196 * Removes entity from system, removing from all node collections
198 * returns true if removed, false if not present
200 removeEntity : function removeEntity(entity
) {
203 position
= this.entities
.indexOf(entity
);
205 for (property
in this.nodeCollections
) {
206 if (this.nodeCollections
.hasOwnProperty(property
)) {
207 this.nodeCollections
[property
].remove(entity
);
211 this.entities
.splice(position
, 1);
219 * Given a Node Class, retrieves a list of all the nodes for each
222 getNodes : function getNodes(nodeType
) {
225 if (this.nodeCollections
.hasOwnProperty(nodeType
)) {
226 return this.nodeCollections
[nodeType
].nodes
;
229 nodeCollection
= new Serpentity
.NodeCollection({
232 this.nodeCollections
[nodeType
] = nodeCollection
;
234 this.entities
.forEach(function (entity
) {
235 nodeCollection
.add(entity
);
238 return nodeCollection
.nodes
;
242 * Calls update for every loaded system.
244 update : function update(dt
) {
245 this.systems
.forEach(function (system
) {
252 require("./component.js");
253 require("./entity.js");
254 require("./node.js");
255 require("./node_collection.js");
256 require("./system.js");