-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserState.js
More file actions
293 lines (260 loc) · 6.47 KB
/
UserState.js
File metadata and controls
293 lines (260 loc) · 6.47 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import SpriteColors from "./SpriteColors.js";
import WHUtil from "./WHUtil.js";
import UserSprite from "./UserSprite.js";
import ClientRoomManager from "./ClientRoomManager.js";
// block on the right side of the screen that show the information
// about users and their associated health
export default class UserState {
static BLOCKSIZE = 5;
static powerupPoints = [
[20, 60],
[7, 100],
[41, 147],
[91, 147],
[97, 60],
[105, 100],
];
static POWERUP_FIELDS = 6;
static POWERUP_LEN = 10;
static POWERUP_DISPLAY_TIMEOUT = 10000;
isEmpty;
refresh;
// store the ClientUser object here
clientUser;
// userId;
// username;
portalSprite;
gameOver;
wins;
refresh;
width;
height;
// shipType;
icons;
teamId;
myHeight;
cx;
cy;
imgPowerups;
offsetCycle;
y;
h;
healthPercent;
numPowerups;
oldHealth;
color;
slot;
game;
constructor(game) {
this.isEmpty = true;
this.refresh = false;
// this.userId = null;
// this.username = null;
this.clientUser = null;
this.portalSprite = null;
this.game = game;
this.powerups = new Array(5);
// this.titleBarH = 40;
this.powerupTimeouts = new Array(6);
this.colors = new SpriteColors();
this.reset();
// this.imgPowerups = (Image[])WormholeModel.g_mediaTable.get("img_smallpowerups");
this.myHeight = 158;
this.y = 0;
this.h = 0;
}
setDrawLocation(y, h) {
this.y = y;
this.h = h;
}
setState(clientUser, slot) {
this.clientUser = clientUser;
this.refresh = true;
this.slot = slot;
this.color = this.colors.colors[slot][0];
this.isEmpty = false;
}
reset() {
this.isEmpty = true;
this.clientUser = null;
// this.userId = null;
// this.username = null;
this.numPowerups = 0;
this.healthPercent = 100;
this.refresh = true;
this.color = "gray";
this.gameOver = false;
if (this.portalSprite != null) {
this.portalSprite.killSelf();
this.portalSprite = null;
}
this.teamId = 0;
}
draw(context, n, n2) {
this.refresh = false;
if (this.isEmpty) {
return;
}
WHUtil.drawBoundRect(
context,
0,
0,
n,
n2,
this.color,
this.gameOver ? "gray" : this.offsetCycle == 30 ? "orange" : "black",
);
context.font = "12px Helvetica";
if (this.clientUser.username != null) {
context.fillText(
this.clientUser.username.length > 12
? this.clientUser.username.substring(0, 11)
: this.clientUser.username,
30,
11,
);
}
// TODO
// where to put the draw icons function?
// CFSkin.getSkin().drawIcons(graphics, this.icons, 97, 2, 15, 3);
context.fillText(`wins: ${this.wins}`, 85, 24);
context.fillText(
`rank: ${this.clientUser.rank != null ? this.clientUser.rank : "n/a"}`,
30,
24,
);
for (let b = 0; b < this.numPowerups; b++) {
let shiftedNumber = this.powerups[b] - 6;
let powerupNumber;
if (shiftedNumber <= 0) {
powerupNumber = 0;
} else {
powerupNumber = shiftedNumber;
}
let img = document.getElementById("smallPowerupImages");
let imgWidth = 21;
let imgHeight = 17;
context.drawImage(
img,
powerupNumber + powerupNumber * imgWidth + 1,
1,
imgWidth,
imgHeight - 2,
34 + b * 21,
29,
imgWidth,
imgHeight - 2,
);
// graphics.drawImage(
// this.imgPowerups[PowerupSprite.convertToSmallImage(this.powerups[b])],
// 34 + b * 21,
// 29,
// null
// );
}
let n3 = n - 10;
let min = Math.min((n3 * this.healthPercent) / 100, n3);
context.strokeStyle = this.color;
context.fillStyle = this.color;
context.strokeRect(5, n2 - 13, n3, 10);
context.fillRect(5, n2 - 13, min, 10);
let n4 = 15;
let n5 = n2 / 2;
if (this.offsetCycle > 0) {
this.offsetCycle--;
n4 += WHUtil.randInt(2);
n5 += WHUtil.randInt(2);
this.refresh = true;
this.game.refreshOtherBar = true;
}
let n6 = UserSprite.fighterData[this.clientUser.shipType].shipScale;
context.translate(n4, n5);
const room = this.game.gameNetLogic.clientRoomManager.getRoomById(
this.game.gameNetLogic.roomId,
);
// make a new polygon for the fighterData
const polygon = WHUtil.createPolygon(
UserSprite.shipShapes[this.clientUser.shipType],
);
WHUtil.drawScaledPoly(context, polygon, n6);
context.translate(-n4, -n5);
if (this.teamId != 0 && room.isTeamRoom) {
this.drawTeamShape(context, 1, 1, this.teamId);
}
}
fullReset() {
this.wins = 0;
this.reset();
}
// TODO - drawTeamShape method
static drawTeamShape(context, n, n2, n3) {
if (n3 == 1) {
WHUtil.drawBoundRect(
context,
n,
n2,
8,
8,
ClientRoomManager.TEAM_COLORS[n3],
ClientRoomManager.TEAM_BG_COLORS[n3],
);
return;
}
WHUtil.drawBoundCircle(
context,
n,
n2,
10,
ClientRoomManager.TEAM_COLORS[n3],
ClientRoomManager.TEAM_BG_COLORS[n3],
);
}
// void addEnemyPowerupAttack( paramShort, paramByte) {}
readState(packet) {
this.refresh = true;
this.oldHealth = this.healthPercent;
this.healthPercent = packet.healthPercent;
if (this.oldHealth > this.healthPercent) {
this.offsetCycle = 30;
}
this.numPowerups = packet.numPowerups;
for (let b = 0; b < this.numPowerups; b++) {
this.powerups[b] = packet.powerups[b];
}
this.clientUser.shipType = packet.shipType;
if (this.clientUser.shipType > 10 || this.clientUser.shipType < 0) {
this.clientUser.shipType = 0;
}
}
isPlaying() {
return !(
this.isEmpty ||
this.gameOver ||
this.portalSprite == null ||
this.portalSprite.shouldRemoveSelf
);
}
timeoutAttacks() {
let n = Date.now();
let b = false;
for (let i = 0; i < 6; i++) {
this.powerupTimeouts[i] = Array(10);
for (let j = 0; j < 10; j++) {
if (this.powerupTimeouts[i][j] > 0 && n > this.powerupTimeouts[i][j]) {
this.powerupTimeouts[i][j] = 0;
b = true;
this.refresh = true;
}
}
}
return b;
}
resetPowerups() {
for (let i = 0; i < 6; i++) {
this.powerupTimeouts[i] = Array(10);
for (let j = 0; j < 10; j++) {
this.powerupTimeouts[i][j] = 0;
}
}
}
}