-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
470 lines (394 loc) · 13.6 KB
/
renderer.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// 3rd
const PIXI = require('pixi.js')
// 1st
const util = require('../common/util')
const sprites = require('./sprites')
// Initialize the renderer by passing in the actual map dimensions,
// which is different from the viewport dimensions.
exports.init = function ({ x: mapX, y: mapY }, walls, polygons, redFlagPos, blueFlagPos, onStageClick) {
console.assert(Array.isArray(walls))
console.assert(Array.isArray(polygons))
console.assert(Array.isArray(redFlagPos))
console.assert(Array.isArray(blueFlagPos))
// we assign this soon, but i need to be able to access it
// in window.onresize.
let pixiRenderer
// STATE
const state = {
sprites: Object.create(null)
}
// VIEWPORT
const viewport = {
x: null,
y: null,
// Converts p2 y to pixi y
fixY (y) {
return mapY - y
},
reset () {
this.x = document.documentElement.clientWidth
this.y = document.documentElement.clientHeight
}
}
// Init viewport
viewport.reset()
window.onresize = function () {
viewport.reset()
pixiRenderer.resize(viewport.x, viewport.y)
}
// STAGE
const stage = new PIXI.Container()
pixiRenderer = PIXI.autoDetectRenderer(viewport.x, viewport.y)
pixiRenderer.backgroundColor = 0x333333
document.body.appendChild(pixiRenderer.view)
stage.interactive = true
stage.on('mousedown', (e) => {
const position = e.data.getLocalPosition(stage)
onStageClick(position)
})
// TILING BACKGROUND
const bg = (function () {
//const texture = PIXI.Texture.fromImage('./img/starfield.jpg')
const texture = PIXI.Texture.fromImage('./img/bg.jpg')
const sprite = new PIXI.extras.TilingSprite(texture)
sprite.height = mapY
sprite.width = mapX
return sprite
})()
stage.addChild(bg)
// DRAW WALLS, trying to figure out why the ship often tunnel into the
// p2.Plane walls
const [top, bot, left, right] = walls
let gfx
gfx = new PIXI.Graphics() // top, red
gfx.beginFill(0x000000)
gfx.lineStyle(5, 0xFF0000)
gfx.moveTo(-viewport.x * 2, viewport.fixY(top.position[1]))
gfx.lineTo(viewport.x * 2, viewport.fixY(top.position[1]))
stage.addChild(gfx)
gfx = new PIXI.Graphics() // bottom, orange
gfx.beginFill(0x000000)
gfx.lineStyle(5, 0xFFA500)
gfx.moveTo(-viewport.x * 2, viewport.fixY(bot.position[1]))
gfx.lineTo(viewport.x * 2, viewport.fixY(bot.position[1]))
stage.addChild(gfx)
gfx = new PIXI.Graphics() // left, blue
gfx.beginFill(0x000000)
gfx.lineStyle(5, 0x0000FF)
gfx.moveTo(left.position[0], -viewport.y * 2)
gfx.lineTo(left.position[0], viewport.y * 2)
stage.addChild(gfx)
gfx = new PIXI.Graphics() // right, green
gfx.beginFill(0x000000)
gfx.lineStyle(5, 0x00FF00)
gfx.moveTo(right.position[0], -viewport.y * 2)
gfx.lineTo(right.position[0], viewport.y * 2)
stage.addChild(gfx)
const wallWarning = (function () {
const message = `You've tunneled outside of the map.`
const wallWarning = new PIXI.Text(message, {
font: '18px Arial',
fill: 0xFF0000,
align: 'center'
})
wallWarning.anchor.set(0.5)
wallWarning.visible = false
stage.addChild(wallWarning)
return wallWarning
})()
// COLLISION MAP
polygons.forEach((vertices) => {
const fixed = vertices.map(([x, y]) => [x, viewport.fixY(y)])
const gfx = sprites.makeCollisionPolygon(fixed)
stage.addChild(gfx)
})
// TILES
/* for (const body of tiles) {
* const sprite = sprites.makeTile(body.tilesize)
* sprite.position.set(body.position[0], viewport.fixY(body.position[1]))
* stage.addChild(sprite)
* }
*/
// TEAM COLORS
const colors = {
red: 0xFFBBBB,
blue: 0xA8CFFF
}
// FLAGS
const redFlag = sprites.makeFlag('RED', colors.red)
redFlag.position.x = redFlagPos[0]
redFlag.position.y = viewport.fixY(redFlagPos[1])
stage.addChild(redFlag)
const blueFlag = sprites.makeFlag('BLUE', colors.blue)
blueFlag.position.x = blueFlagPos[0]
blueFlag.position.y = viewport.fixY(blueFlagPos[1])
stage.addChild(blueFlag)
// These markers become visible when a flag is taken
const redFlagTaken = (function () {
const text = new PIXI.Text('Taken', {
font: '16px Arial',
fill: colors.red,
align: 'center'
})
text.anchor.set(0.5)
text.position = redFlag.position
text.visible = false
return text
})()
stage.addChild(redFlagTaken)
const blueFlagTaken = (function () {
const text = new PIXI.Text('Taken', {
font: '16px Arial',
fill: colors.blue,
align: 'center'
})
text.anchor.set(0.5)
text.position = blueFlag.position
text.visible = true
return text
})()
stage.addChild(blueFlagTaken)
// EXPLOSIONS
//
// Active movieclips are maintained in two maps.
// On every render, we remove any movieclips that
// are on their last frame.
// Map of playerId -> PIXI.MovieClip
const shipExplosions = {}
// Map of bombId -> PIXI.MovieClip
const bombExplosions = {}
// FLAG CARRIER GLOW
const redCarrierGlow = sprites.makeFlagCarrierGlow(colors.red)
const blueCarrierGlow = sprites.makeFlagCarrierGlow(colors.blue)
stage.addChild(redCarrierGlow)
stage.addChild(blueCarrierGlow)
// ENERGY BAR
// energy bar is the rectangle that appears beneath the player's
// ship to indicate how much energy they have
function makeEnergyBar (maxWidth) {
const gfx = new PIXI.Graphics()
gfx.beginFill(0xFFFFFF)
gfx.drawRect(0, 0, maxWidth, 5)
gfx.alpha = 0
return gfx
}
// 0 is empty, 1 is full
function getEnergyTint (scale) {
if (scale < 0.5) {
// red
return 0xFF0000
} else if (scale < 0.75) {
// yellow
return 0xF3F315
} else {
// green
return 0x39FF14
}
}
// HELPERS
// https://gist.github.com/gre/1650294
const easing = {
// no easing, no acceleration
linear: function (t) { return t },
// accelerating from zero velocity
easeInQuad: function (t) { return t*t },
// decelerating to zero velocity
easeOutQuad: function (t) { return t*(2-t) },
// acceleration until halfway, then deceleration
easeInOutQuad: function (t) { return t<.5 ? 2*t*t : -1+(4-2*t)*t },
// accelerating from zero velocity
easeInCubic: function (t) { return t*t*t },
// decelerating to zero velocity
easeOutCubic: function (t) { return (--t)*t*t+1 },
// acceleration until halfway, then deceleration
easeInOutCubic: function (t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 },
// accelerating from zero velocity
easeInQuart: function (t) { return t*t*t*t },
// decelerating to zero velocity
easeOutQuart: function (t) { return 1-(--t)*t*t*t },
// acceleration until halfway, then deceleration
easeInOutQuart: function (t) { return t<.5 ? 8*t*t*t*t : 1-8*(--t)*t*t*t },
// accelerating from zero velocity
easeInQuint: function (t) { return t*t*t*t*t },
// decelerating to zero velocity
easeOutQuint: function (t) { return 1+(--t)*t*t*t*t },
// acceleration until halfway, then deceleration
easeInOutQuint: function (t) { return t<.5 ? 16*t*t*t*t*t : 1+16*(--t)*t*t*t*t }
}
// RENDER
return function render (simulation, currUserId, spritesToRemove, detonatedBombs, killedPlayers, showHitbox) {
// UPDATE FLAG SPAWNS
if (simulation.redCarrier) {
redFlag.visible = false
redCarrierGlow.visible = true
redFlagTaken.visible = true
} else {
redFlag.visible = true
redCarrierGlow.visible = false
redFlagTaken.visible = false
}
if (simulation.blueCarrier) {
blueFlag.visible = false
blueCarrierGlow.visible = true
blueFlagTaken.visible = true
} else {
blueFlag.visible = true
blueCarrierGlow.visible = false
blueFlagTaken.visible = false
}
// Update / decay / destroy existing bob explosions
for (const id in bombExplosions) {
const clip = bombExplosions[id]
// if clip is at final frame, destroy it
if (clip.currentFrame === clip.totalFrames - 1) {
stage.removeChild(clip)
clip.destroy()
delete bombExplosions[id]
}
}
// Create bomb explosions
for (const [id, x, y] of detonatedBombs) {
const clip = sprites.makeBombExplosion()
clip.position.set(x, viewport.fixY(y))
bombExplosions[id] = clip
stage.addChild(clip)
}
// DECAY / REMOVE SHIP EXPLOSIONS
for (const id in shipExplosions) {
const clip = shipExplosions[id]
// if clip is at final frame, destroy it
if (clip.currentFrame === clip.totalFrames - 1) {
stage.removeChild(clip)
clip.destroy()
delete shipExplosions[id]
}
}
// CREATE SHIP EXPLOSIONS
for (const player of killedPlayers) {
const clip = sprites.makeShipExplosion()
clip.position.set(player.body.position[0],
viewport.fixY(player.body.position[1]))
shipExplosions[player.id] = clip
stage.addChild(clip)
}
// Update player sprites
for (const id in simulation.players) {
const player = simulation.players[id]
if (state.sprites[id]) {
// player sprite exists, so update it
const [x, y] = Array.from(player.body.interpolatedPosition)
const container = state.sprites[id]
const sprite = container.getChildAt(0)
container.position.set(x, viewport.fixY(y))
sprite.rotation = util.clampRad(player.body.interpolatedAngle)
// if player is flag carrier, give them the flag glow
if (player.id === simulation.redCarrier) {
redCarrierGlow.position = container.position
} else if (player.id === simulation.blueCarrier) {
blueCarrierGlow.position = container.position
}
// if this player is us, offset stage so that we are centered
if (player.id === currUserId) {
stage.position.x = viewport.x/2 - x
stage.position.y = viewport.y/2 - viewport.fixY(y)
// also, check if we are out of bounds to display wallWarning
if (x < 0 || x > mapX || y < 0 || y > mapY) {
wallWarning.position.x = x
wallWarning.position.y = viewport.fixY(y + 50)
wallWarning.visible = true
} else {
wallWarning.visible = false
}
// check for show-hitbox change
// FIXME: don't rely on container.getChildAt(3) to be hitbox
if (showHitbox && !container.getChildAt(3).visible) {
container.getChildAt(3).visible = true
} else if (!showHitbox && container.getChildAt(3).visible) {
container.getChildAt(3).visible = false
}
// update energy bar
if (container.energyBar) {
const scalar = player.curEnergy / player.maxEnergy
container.energyBar.width = sprite.width * scalar
container.energyBar.alpha = 1 - easing.easeInCubic(scalar)
// update color
container.energyBar.tint = getEnergyTint(scalar)
}
}
} else {
// player sprite must be created
// TODO: Relocate to sprites.js
// don't interpolate on sprite spawn, causes weird stuff
const [x, y] = Array.from(player.body.position)
const container = new PIXI.Container()
// container children (the ship sprite and the username)
const sprite = new PIXI.Sprite.fromImage('./img/warbird.gif')
const text = new PIXI.Text(player.uname, {
font: '12px Arial',
fill: 0xFFFFFF,
align: 'left'
})
// Apply team tint
if (player.team === 'RED') {
sprite.tint = colors.red
text.tint = colors.red
} else if (player.team === 'BLUE') {
sprite.tint = colors.blue
text.tint = colors.blue
}
sprite.anchor.set(0.5)
sprite.height = 30
sprite.width = 30
container.position.set(x, viewport.fixY(y))
sprite.rotation = util.clampRad(player.body.interpolatedAngle)
text.position.set(sprite.x + 15, sprite.y + 10)
container.addChild(sprite)
container.addChild(text)
// Mount energy bar if it's current player
if (player.id === currUserId) {
const energyBar = makeEnergyBar(sprite.width)
energyBar.position.set(sprite.x - sprite.width / 2,
sprite.y + sprite.width / 2)
container.addChild(energyBar)
container.energyBar = energyBar
}
// Add hitbox visualization
const hitbox = new PIXI.Graphics()
hitbox.lineStyle(1, 0xFFFFFF)
hitbox.drawCircle(sprite.position.x, sprite.position.y, 15)
hitbox.visible = false
container.addChild(hitbox)
// Add to stage
state.sprites[id] = container
stage.addChild(container)
}
}
// Upsert bomb sprites
for (const id in simulation.bombs) {
const bomb = simulation.bombs[id]
if (state.sprites[id]) {
// sprite exists, so updated it
const sprite = state.sprites[id]
const [x, y] = Array.from(bomb.body.interpolatedPosition)
sprite.position.set(x, viewport.fixY(y))
} else {
// sprite does not exist, so create it
// don't interpolate on sprite spawn, causes weird stuff
const [x, y] = Array.from(bomb.body.position)
const sprite = sprites.makeBomb('A', 3)
sprite.position.set(x, viewport.fixY(y))
state.sprites[id] = sprite
stage.addChild(sprite)
}
}
// Clean up old sprites
for (const id of spritesToRemove) {
const sprite = state.sprites[id]
stage.removeChild(sprite)
delete state.sprites[id]
if (sprite) sprite.destroy()
}
// Render
pixiRenderer.render(stage)
}
}