diff options
| author | Ben Beltran <ben@nsovocal.com> | 2014-08-11 06:56:50 -0500 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2014-08-11 06:56:50 -0500 |
| commit | 42c62ebf0114f17ffc88f7a025b0bd826cd068fb (patch) | |
| tree | 3a954d7ffb6be73524ba7cd51f6223483575021b /lib | |
| parent | ae99b55e21cc721a28a651120a04387f4a27a9d0 (diff) | |
Adds priorities
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; }, |