-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgame-loop.js
225 lines (178 loc) · 6.15 KB
/
game-loop.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*global gs, game, Phaser, console*/
/*global levelController*/
/*global HUD_WIDTH*/
/*jshint esversion: 6, laxbreak: true*/
'use strict';
// UPDATE:
// ************************************************************************************************
gs.update = function () {
if (this.state === 'OPTIONS_MENU_STATE') {
this.optionsMenu.update();
}
if (this.globalState === 'MAIN_MENU_STATE') {
this.mainMenuBase.update();
return;
}
this.updateProjectiles();
this.updateParticles();
this.updateParticleGenerators();
if (this.pauseTime > 0) {
this.pauseTime -= 1;
}
// NPCs will pause before using abilities until the player finishes moving:
if (this.activeCharacter.state === 'PAUSE' && gs.pc.body.state === 'WAITING') {
this.activeCharacter.state = 'WAITING';
}
if (this.state === 'END_TURN') {
if (this.allCharactersReady()) {
this.startTurn();
this.setState('GAME_STATE');
}
}
else if (gs.pc.currentHp > 0 && gs.pauseTime === 0) {
// Player chooseAction:
if (this.activeCharacter === this.pc && this.canCharacterAct(this.pc)) {
this.pc.chooseAction();
}
// NPC chooseAction:
// As long as a character does not perform a blocking action, many characters can chooseAction in the same frame
// This makes turns much faster
while (this.canCharacterAct(this.activeCharacter) && (this.activeCharacter !== this.pc || this.pc.actionQueue.length > 0 && this.pc.actionQueue[0].type === 'WAIT')) {
this.activeCharacter.chooseAction();
}
}
// Update sprites:
this.updateCharacterFrames();
this.HUD.refresh();
this.updateDamageText();
this.updateHUDTileSprites();
// Update Shop Menu:
if (this.state === 'SHOP_MENU_STATE') {
this.shopMenu.update();
}
if (this.state === 'CHARACTER_MENU_STATE') {
this.characterMenu.update();
}
if (this.state === 'TRANSFERANCE_MENU_STATE') {
this.transferanceMenu.update();
}
// Update camera:s
game.camera.focusOnXY(this.pc.body.position.x + HUD_WIDTH / 2, this.pc.body.position.y);
this.shadowMaskSprite.x = this.pc.body.position.x;//game.camera.x;
this.shadowMaskSprite.y = this.pc.body.position.y;//game.camera.y;
this.updateTileMapSprites();
gs.objectSpritesGroup.sort('y', Phaser.Group.SORT_ASCENDING);
};
// CAN_CHARACTER_ACT:
// ************************************************************************************************
gs.canCharacterAct = function (character) {
return this.projectileList.length === 0
&& this.state === 'GAME_STATE'
&& character.state === 'WAITING'
&& character.body.state === 'WAITING';
};
// ALL_CHARACTERS_READY:
// Use this to determine if all characters have completed movement
// ************************************************************************************************
gs.allCharactersReady = function () {
for (let i = 0; i < this.characterList.length; i += 1) {
if (this.characterList[i].isAlive && (this.characterList[i].body.state !== 'WAITING' || this.characterList[i].eventQueue.isProcessing())) {
return false;
}
}
if (this.projectileList.length > 0) {
return false;
}
return true;
};
// END_TURN:
// ************************************************************************************************
gs.endTurn = function () {
if (this.state !== 'DIALOG_MENU_STATE' && (this.projectileList.length > 0 || this.activeCharacter.eventQueue.isProcessing())) {
this.setState('END_TURN');
}
else {
this.startTurn();
}
};
gs.removeDeadCharacters = function () {
// Remove dead Characters:
for (let i = this.characterList.length - 1; i >= 0; i -= 1) {
if (!this.characterList[i].isAlive && this.characterList[i] !== gs.pc) {
this.characterList.splice(i, 1);
}
}
};
// START_TURN:
// ************************************************************************************************
gs.startTurn = function () {
var lastActiveCharacter = this.activeCharacter;
this.removeDeadCharacters();
if (this.activeCharacterIndex >= this.characterList.length) {
this.activeCharacterIndex = 0;
}
// Ticking the global clock:
if (lastActiveCharacter === this.pc) {
this.globalTurnTimer += gs.pc.waitTime;
}
// Dead player:
if (!gs.pc.isAlive) {
this.activeCharacterIndex = 0;
this.activeCharacter = gs.pc;
return;
}
// Find the next active character:
while (this.characterList[this.activeCharacterIndex].waitTime > 0) {
this.characterList[this.activeCharacterIndex].waitTime -= 50;
this.activeCharacterIndex += 1;
if (this.activeCharacterIndex >= this.characterList.length) {
this.activeCharacterIndex = 0;
}
}
this.activeCharacter = this.characterList[this.activeCharacterIndex];
// Global Turns:
if (this.activeCharacter === this.pc) {
while (this.globalTurnTimer >= 100) {
this.globalTurnTimer -= 100;
this.updateGlobalTurn();
}
}
// The player has just started his turn so we set hasNPCActed to false
// If an npc acts in between then we stop the player from moving
if (this.activeCharacter === this.pc) {
gs.HUD.miniMap.refresh();
gs.pc.previousTileIndex = {x: gs.pc.tileIndex.x, y: gs.pc.tileIndex.y};
gs.pc.inventory.quickWeaponEquipped = false;
if (this.hasNPCActed || this.numVisibleNPCs() > 0) {
gs.pc.stopExploring();
}
this.hasNPCActed = false;
}
};
// UPDATE_GLOBAL_TURN:
// ************************************************************************************************
gs.updateGlobalTurn = function () {
gs.turn += 1;
// Update effect:
// Reverse order so that adding new effects won't immediately update
for (let i = this.cloudList.length - 1; i >= 0 ; i -= 1) {
if (this.cloudList[i].isAlive) {
this.cloudList[i].updateTurn();
}
}
// Update objects:
this.objectList.forEach(function (object) {
if (object.updateTurn) {
object.updateTurn();
}
}, this);
// Update Characters:
for (let i = 0; i < this.characterList.length; i += 1) {
if (this.characterList[i].isAlive) {
this.characterList[i].updateTurn();
}
}
// Level Controller:
levelController.updateTurn();
this.calculateLoS();
};