-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.js
326 lines (258 loc) · 10.7 KB
/
screen.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
'use strict'
class Screen {
constructor() {
this.bg = [0, 0, 0, 0]
this.change = 2.2
this.closest = {
item: 0,
type: '',
angle: 0,
rot_speed: 0,
momentum: .9,
damping: 100,
fade: 0
}
this.set()
this.bg_cvs = document.createElement('canvas')
this.bg_ctx = this.bg_cvs.getContext('2d')
this.health_momentum = .8
this.health_damping = 10
this.health = []
this.center = {
x: 0,
speed: 0,
momentum: .7,
damping: 10
}
this.filter = [0, 0, 0, 0]
this.fade = {use: false}
this.end = {
white: 255,
}
}
generateBackground() {
if (game.level == game.end_level) {
this.bg_cvs.width = cvs.width
this.bg_cvs.height = cvs.height
this.bg_ctx.fillStyle = '#000'
this.bg_ctx.fillRect(0, 0, this.bg_cvs.width, this.bg_cvs.height)
}
else {
this.bg_cvs.width = cvs.width
this.bg_cvs.height = cvs.height
this.bg_ctx.clearRect(0, 0, cvs.width, cvs.height)
const big = cvs.width > cvs.height ? cvs.width : cvs.height
const small = cvs.width > cvs.height ? cvs.height : cvs.width
const h_blocks = 150
const v_blocks = h_blocks / (big / small)
const width = cvs.width / Math.floor(h_blocks)
const height = cvs.height / Math.floor(v_blocks)
for (let x = 0; x < h_blocks; x ++) {
for (let y = 0; y < v_blocks; y ++) {
this.bg_ctx.fillStyle = rgb(0, 0, 0, random(0, .022, 0))
this.bg_ctx.fillRect(x * width, y * height, width, height)
}
}
}
}
set() {
const rgb = colorChange(this.change)
const alpha = (game.level - 1) / 50 + .1
this.bg[0] = rgb[0]
this.bg[1] = rgb[1]
this.bg[2] = rgb[2]
this.bg[3] = alpha > .5 ? .5 : alpha
this.change += .2
}
calcClosest() {
const close = {
dist: 'none',
type: 'none',
i: 0,
}
const think = (index, item, type) => {
const dist_x = cam.x - item.x
const dist_y = cam.y - item.y
const dist = Math.sqrt((dist_x * dist_x) + (dist_y * dist_y))
if (close.dist == 'none' || close.dist > dist) {
close.dist = dist
close.i = index
close.type = type
}
}
for (let i = 0; i < game.points.length; i ++) think(i, game.points[i], 'point')
for (let i = 0; i < game.upgrades.length; i ++) think(i, game.upgrades[i], 'upgrade')
if (close.type == 'point') this.closest.item = game.points[close.i]
if (close.type == 'upgrade') this.closest.item = game.upgrades[close.i]
this.closest.type = close.type
}
fadeIn(max, speed, color, smoothness) {
this.fade.use = true
this.filter[0] += (color[0] - this.filter[0]) / smoothness
this.filter[1] += (color[1] - this.filter[1]) / smoothness
this.filter[2] += (color[2] - this.filter[2]) / smoothness
if (this.filter[3] < max) this.filter[3] += speed
if (this.filter[3] > max) this.filter[3] = max
}
fadeOut(speed, color, smoothness) {
this.fade.use = true
this.filter[0] += (color[0] - this.filter[0]) / smoothness
this.filter[1] += (color[1] - this.filter[1]) / smoothness
this.filter[2] += (color[2] - this.filter[2]) / smoothness
if (this.filter[3] > 0) this.filter[3] -= speed
if (this.filter[3] < 0) this.filter[3] = 0
}
update() {
if (!this.fade.use) this.fadeOut(.01, [0, 0, 0], 10)
if (hero.point.state) {
if (!game.points.length && !game.upgrades.length) this.closest.item = 0
else this.calcClosest()
}
this.fade.use = false
}
drawBack() {
const speed = .3
const width = (cvs.width / scale) / speed
const height = (cvs.height / scale) / speed
const cam_x = (cam.x % width + width) % width
const cam_y = (cam.y % height + height) % height
const x = -cam_x * scale * speed
const y = -cam_y * scale * speed
ctx.drawImage(this.bg_cvs, x, y)
ctx.drawImage(this.bg_cvs, x + cvs.width, y)
ctx.drawImage(this.bg_cvs, x, y + cvs.height)
ctx.drawImage(this.bg_cvs, x + cvs.width, y + cvs.height)
}
drawMid() {
ctx.fillStyle = rgb(this.filter[0], this.filter[1], this.filter[2], this.filter[3])
ctx.fillRect(0, 0, cvs.width, cvs.height)
}
draw() {
const COLOR = game.level == game.end_level ? '#fff' : '#000'
// Health
const health = {
distance: scale / 2.4,
width: scale / 3.7,
height: scale / 2.5
}
const font_pad = 5
const font_size = scale / 2.5
const center = cvs.width / 2 - hero.health / 2 * health.distance
if (!this.center.x) this.center.x = cvs.width / 2
this.center.speed *= this.center.momentum
this.center.x += this.center.speed / this.center.damping
this.center.speed -= this.center.x - center
for (let i = 0; i < this.health.length; i ++) {
if (this.health[i].size >= 1 && this.health[i].state == 'grow')
this.health[i].state = 'norm'
this.health[i].speed *= this.health_momentum
this.health[i].size += this.health[i].speed / this.health_damping
if (this.health[i].state == 'die') {
this.health[i].speed -= .1 / this.health[i].size
if (this.health[i].size <= 0) {
this.health.pop()
continue
}
}
else this.health[i].speed += 1 - this.health[i].size
const w = health.width * this.health[i].size
const h = health.height * this.health[i].size
ctx.fillStyle = COLOR
ctx.fillRect(
(this.center.x + i * health.distance) - w / 2,
health.distance - h / 2,
w, h
)
}
// make health get bigger and smaller
for (let i = 0 ; i < (this.health.length || 1); i ++) {
const state = this.health.length ? this.health[this.health.length - 1].state : 'norm'
if (state == 'norm') {
if (this.health.length < hero.health)
this.health.push({size: 0, speed: 0, state: 'grow'})
else if (this.health.length > hero.health)
this.health[this.health.length - 1].state = 'die'
}
}
ctx.fillStyle = COLOR
ctx.font = font_size + 'px monospace'
// Level
const text = 'LEVEL ' + game.level
const width = ctx.measureText(text).width
ctx.fillText(
text,
cvs.width - width - font_pad * 2,
font_size + font_pad
)
// Points
ctx.fillText(
(game.points_collected + game.upgrades_collected) + '/' +
(game.points_max + game.upgrades_max) + ' COINS',
font_pad * 2,
font_size + font_pad
)
// Upgrades & Power-ups
let upg_text = ''
const _type = hero.upgrade_past[hero.upgrade_past.length - 1].type
if (_type == 'pound') upg_text = 'Ground Pound'
if (_type == 'point') upg_text = 'Coin Location'
if (_type == 'jet') upg_text = 'Jetpack'
ctx.fillStyle = COLOR
ctx.fillText('UPG ' + upg_text, font_pad * 2, (font_size + font_pad) * 2)
let pow_text = ''
const add = (text, mid) => {
let str = ''
if (mid) str += ' / '
pow_text += text + str
}
for(let i = 0; i < hero.power_past.length; i ++) {
const type = hero.power_past[i].type
const mid = (i < hero.power_past.length - 1)
if (type == 'shield') add('Shield', mid)
else if (type == 'super pound') add('Super Pound', mid)
}
ctx.fillText('PWR ' + pow_text, font_pad * 2, (font_size + font_pad) * 3)
// Point Search Upgrade
if (hero.point.state && this.closest.item) {
const arrowTo = (item, color) => {
const dist_x = (cam.x - hero.width / 2) - (item.x - hero.width / 2)
const dist_y = (cam.y - hero.height / 2) - (item.y - hero.height / 2)
const dist = Math.sqrt((dist_x * dist_x) + (dist_y * dist_y))
const smallest = cvs.width < cvs.height ? cvs.width / 3 : cvs.height / 3
const fade_dist = (dist - 1) * scale
const _fade = (fade_dist - smallest) / 400
const fade = _fade > 1 ? 1 : _fade
const size = scale / 6
let angle = Math.atan2(dist_y, dist_x)
const bit = Math.PI / 2
if (this.closest.angle > bit && angle < -bit) angle += Math.PI * 2
else if (this.closest.angle < -bit && angle > bit) angle -= Math.PI * 2
this.closest.fade += (fade - this.closest.fade) / 10
this.closest.angle += this.closest.rot_speed
this.closest.rot_speed *= this.closest.momentum
this.closest.rot_speed += (angle - this.closest.angle) / this.closest.damping
ctx.strokeStyle = rgb(
color[0],
color[1],
color[2],
this.closest.fade * color[3]
)
ctx.lineWidth = scale / 15
ctx.save()
ctx.beginPath()
ctx.translate(
cvs.width / 2 - Math.cos(this.closest.angle) * smallest,
cvs.height / 2 - Math.sin(this.closest.angle) * smallest
)
ctx.rotate(this.closest.angle)
ctx.moveTo(size, size)
ctx.lineTo(0, 0)
ctx.lineTo(size, -size)
ctx.stroke()
ctx.restore()
}
if (this.closest.type == 'point') arrowTo(this.closest.item, [200, 100, 10, 1])
else if (this.closest.type == 'upgrade') arrowTo(this.closest.item, [10, 200, 0, 1])
}
}
}