Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AroriaNetwork
GitHub Repository: AroriaNetwork/3kho-backup
Path: blob/main/projects/missiles/src/barrierQueue.js
1835 views
1
MG.barrierQueue = (function () {
2
var mBarrierQueue = [];
3
var mRootNode;
4
5
var mFirstBarrierIndex = 0;
6
7
return {
8
init: function (rootNode) {
9
mRootNode = rootNode;
10
},
11
12
update: function (dt) {
13
var i;
14
15
/* iterate through and update each of the barriers in the queue */
16
for (i = mFirstBarrierIndex; i < mBarrierQueue.length; i++) {
17
mBarrierQueue[i].update(dt);
18
}
19
},
20
21
updateDOM: function (missileX, missileY, missileOffset) {
22
var i;
23
24
/* Remove any barriers that have been queued for deletion */
25
while (mFirstBarrierIndex > 0) {
26
mBarrierQueue[0].destroy();
27
mBarrierQueue.shift();
28
mFirstBarrierIndex --;
29
}
30
31
/* Initialise any uninitialised barriers and add them behind the ones already there. */
32
for (i = 0; i < mBarrierQueue.length; i++) {
33
var barrier = mBarrierQueue[i];
34
35
if (!barrier.isInitialised()) {
36
barrier.init();
37
38
if (i > 0) {
39
mRootNode.insertBefore(barrier.getRootNode(), mBarrierQueue[i-1].getRootNode());
40
} else {
41
mRootNode.appendChild(barrier.getRootNode());
42
}
43
}
44
}
45
46
var z = 0.0;
47
for (i = 0; i < mBarrierQueue.length; i++) {
48
mBarrierQueue[i].updateDOM(missileX, missileY, z + missileOffset);
49
z += MG.BARRIER_SPACING;
50
}
51
},
52
53
/**
54
* Adds a new barrier to the end of the queue.
55
* The starting angle and angular rate are randomized.
56
*/
57
pushBarrier: function (type) {
58
/* Create a new barrier but do not initialise it */
59
var barrier = new MG.Barrier(type);
60
61
/* Add the barrier to the internal list */
62
mBarrierQueue[mBarrierQueue.length] = barrier;
63
64
},
65
66
/**
67
* Pops the barrier closest to the missile.
68
*/
69
popBarrier: function () {
70
mFirstBarrierIndex++;
71
mFirstBarrierIndex = Math.min(mFirstBarrierIndex, mBarrierQueue.length);
72
},
73
74
/**
75
* Returns a reference to the barrier at the front of the queue.
76
*/
77
nextBarrier: function () {
78
return mBarrierQueue[mFirstBarrierIndex];
79
},
80
81
/**
82
* Deletes all barriers and returns the queue to it's original, empty state.
83
*/
84
reset: function () {
85
while (mFirstBarrierIndex < mBarrierQueue.length) {
86
this.popBarrier();
87
}
88
},
89
90
/**
91
* Returns true if there are no barriers queued.
92
*/
93
isEmpty: function () {
94
return this.nextBarrier() === undefined;
95
},
96
97
98
/**
99
* Returns the number of barriers in the queue that have not been marked for deletion.
100
*/
101
numBarriers: function () {
102
return mBarrierQueue.length - mFirstBarrierIndex;
103
}
104
};
105
}());
106
107