forked from FreezingMoon/AncientBeast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreature_queue.js
74 lines (62 loc) · 1.76 KB
/
creature_queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var CreatureQueue = class CreatureQueue {
constructor(game) {
this.game = game;
this.queue = [];
this.nextQueue = [];
}
/**
* Add a creature to the next turn's queue by initiative
* creature - The creature to add
*/
addByInitiative(creature) {
for (let i = 0; i < this.nextQueue.length; i++) {
let queue = this.nextQueue[i];
if (queue.delayed || queue.getInitiative() < creature.getInitiative()) {
this.nextQueue.splice(i, 0, creature);
return;
}
}
this.nextQueue.push(creature);
}
dequeue() {
return this.queue.splice(0, 1)[0];
}
remove(creature) {
arrayUtils.removePos(this.queue, creature);
arrayUtils.removePos(this.nextQueue, creature);
}
nextRound() {
// Copy next queue into current queue
this.queue = this.nextQueue.slice(0);
// Sort next queue by initiative (current queue may be reordered) descending
this.nextQueue = this.nextQueue.sort((a, b) => b.getInitiative() - a.getInitiative());
}
isCurrentEmpty() {
return this.queue.length === 0;
}
isNextEmpty() {
return this.nextQueue.length === 0;
}
delay(creature) {
// Find out if the creature is in the current queue or next queue; remove
// it from the queue and replace it at the end
let game = this.game,
inQueue = arrayUtils.removePos(this.queue, creature) || creature === game.activeCreature,
queue = this.queue;
if (!inQueue) {
queue = this.nextQueue;
arrayUtils.removePos(this.nextQueue, creature);
}
// Move creature to end of queue but in order w.r.t. other delayed creatures
for (let i = 0, len = queue.length; i < len; i++) {
if (!queue[i].delayed) {
continue;
}
if (queue[i].getInitiative() < creature.getInitiative()) {
queue.splice(i, 0, creature);
return;
}
}
queue.push(creature);
}
};