diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/serpentity/serpentity.js | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/lib/serpentity/serpentity.js b/lib/serpentity/serpentity.js index d1c1cb0..d86c47e 100644 --- a/lib/serpentity/serpentity.js +++ b/lib/serpentity/serpentity.js @@ -11,10 +11,11 @@ Usage: 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: @@ -122,11 +123,31 @@ Class("Serpentity")({ * * 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; }, |