Path: blob/main/projects/missiles/src/barrierQueue.js
1835 views
MG.barrierQueue = (function () {1var mBarrierQueue = [];2var mRootNode;34var mFirstBarrierIndex = 0;56return {7init: function (rootNode) {8mRootNode = rootNode;9},1011update: function (dt) {12var i;1314/* iterate through and update each of the barriers in the queue */15for (i = mFirstBarrierIndex; i < mBarrierQueue.length; i++) {16mBarrierQueue[i].update(dt);17}18},1920updateDOM: function (missileX, missileY, missileOffset) {21var i;2223/* Remove any barriers that have been queued for deletion */24while (mFirstBarrierIndex > 0) {25mBarrierQueue[0].destroy();26mBarrierQueue.shift();27mFirstBarrierIndex --;28}2930/* Initialise any uninitialised barriers and add them behind the ones already there. */31for (i = 0; i < mBarrierQueue.length; i++) {32var barrier = mBarrierQueue[i];3334if (!barrier.isInitialised()) {35barrier.init();3637if (i > 0) {38mRootNode.insertBefore(barrier.getRootNode(), mBarrierQueue[i-1].getRootNode());39} else {40mRootNode.appendChild(barrier.getRootNode());41}42}43}4445var z = 0.0;46for (i = 0; i < mBarrierQueue.length; i++) {47mBarrierQueue[i].updateDOM(missileX, missileY, z + missileOffset);48z += MG.BARRIER_SPACING;49}50},5152/**53* Adds a new barrier to the end of the queue.54* The starting angle and angular rate are randomized.55*/56pushBarrier: function (type) {57/* Create a new barrier but do not initialise it */58var barrier = new MG.Barrier(type);5960/* Add the barrier to the internal list */61mBarrierQueue[mBarrierQueue.length] = barrier;6263},6465/**66* Pops the barrier closest to the missile.67*/68popBarrier: function () {69mFirstBarrierIndex++;70mFirstBarrierIndex = Math.min(mFirstBarrierIndex, mBarrierQueue.length);71},7273/**74* Returns a reference to the barrier at the front of the queue.75*/76nextBarrier: function () {77return mBarrierQueue[mFirstBarrierIndex];78},7980/**81* Deletes all barriers and returns the queue to it's original, empty state.82*/83reset: function () {84while (mFirstBarrierIndex < mBarrierQueue.length) {85this.popBarrier();86}87},8889/**90* Returns true if there are no barriers queued.91*/92isEmpty: function () {93return this.nextBarrier() === undefined;94},959697/**98* Returns the number of barriers in the queue that have not been marked for deletion.99*/100numBarriers: function () {101return mBarrierQueue.length - mFirstBarrierIndex;102}103};104}());105106107