-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoor.js
67 lines (57 loc) · 1.62 KB
/
door.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
'use strict'
class Door {
constructor(x, y) {
this.x = x
this.y = y
this.width = .7
this.height = 1
this._width = this.width
this.do_open = 0
this.slam = 0
this.state = 'closed'
}
open() {
this.do_open ++
let progress = Math.cos((this.do_open / 10) + 1)
if (progress > .5) progress = .5
if (progress < -.5) progress = -.5
this._width = progress * this.width * 2
if (progress <= -.5) this.state = 'open'
if (progress >= .5 && this.state == 'open') {
if (!this.slam) cam.boom(15, .07, .07)
this.slam ++
if (this.slam > 20) game.nextLevel()
}
}
update() {
if (this.do_open) this.open()
this.draw()
}
draw() {
const HANDLE = .14
const PLANKS = 5
ctx.fillStyle = '#000'
fillRect(this.x, this.y, this.width, this.height)
for (let i = 0; i < PLANKS; i ++) {
const grade = Math.sin(Math.tan(i * 5) * 2) * 5
ctx.fillStyle = rgb(
150 - grade,
90 - grade,
40 - grade
)
fillRect(
this.x + i * (this._width / PLANKS),
this.y,
this._width / PLANKS,
this.height
)
}
ctx.fillStyle = '#333'
fillRect(
this.x + this._width - HANDLE / .75,
this.y + this.height / 2 - HANDLE / 2,
HANDLE,
HANDLE
)
}
}