var engine = Serpentity();
-Add entities or systems:
+Add entities or systems, systems are added with a priority (the smaller
+the number, the earlier it will be called):
engine.addEntity(entityFactory());
- engine.addSystem(new GameSystem());
+ engine.addSystem(new GameSystem(), priority);
Update all systems:
*
* returns true if added succesfully, false if already added
*/
- addSystem : function addSystem(system) {
+ addSystem : function addSystem(system, priority) {
+ var lastIndex, found;
+
if (this.systems.indexOf(system) >= 0) {
return false;
}
- this.systems.push(system);
+
+ system.priority = priority;
+
+ found = false;
+ lastIndex = 0;
+
+ this.systems.some(function findPriority(existingSystem, i) {
+ lastIndex = i;
+ if (existingSystem.priority >= system.priority) {
+ found = true;
+ return true;
+ }
+ });
+
+ if (!found) {
+ lastIndex += 1
+ }
+
+ this.systems.splice(lastIndex, 0, system);
system.added(this);
return true;
},