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
|
/**
* Draw functions
*/
function draw() {
ctx.fillStyle = "#4444ff"
ctx.fillRect(0, 0, width, height)
currLevel.items.forEach(item => {
item.draw()
})
drawExit(currLevel.exit)
drawPlane()
ctx.fillStyle = "#222"
ctx.font = "20px Courier"
ctx.fillText(`Level ${currLevelIndex} (${collected}/${currLevel.coinsNeeded}) - ${currLevel.title} - Time ${Math.round(t / fps / 60)}:${Math.round(t / fps) % 60}`, 20, 20)
}
function drawPlane() {
ctx.fillStyle = "#fff"
ctx.strokeStyle = "#111"
ctx.lineWidth = 3
ctx.beginPath();
if (plane.dir < 0) {
ctx.moveTo(plane.x + plane.width, plane.y);
ctx.lineTo(plane.x, plane.y);
ctx.lineTo(plane.x + plane.width + 4, plane.y + plane.height);
ctx.lineTo(plane.x + plane.width, plane.y);
} else {
ctx.moveTo(plane.x, plane.y);
ctx.lineTo(plane.x + plane.width, plane.y);
ctx.lineTo(plane.x + 4, plane.y + plane.height);
ctx.lineTo(plane.x, plane.y);
}
ctx.stroke();
ctx.fill();
}
function drawVent() {
ctx.fillStyle = "#ccc"
ctx.fillRect(this.x, this.y, this.width, 10)
// every 1/3 seconds spawn new air vents
// (since this is purely cosmetic, updates to it are in here)
if (t % Math.floor(fps / 3) == 0) {
for (var j = 0; j <= this.width; j += 20) {
this.wind.push({
x: this.x + j,
y: this.y - 10,
height: Math.floor(Math.random() * 15) + 4
})
}
}
// Move up each wind a bit randomly
this.wind.forEach(w => {
w.y -= Math.random() * 4 + 2
})
// Remove all wind higher than certain height
this.wind = this.wind.filter(w => {
return this.y - w.y < this.height
}, this)
// Finally draw each wind
ctx.fillStyle = "white"
this.wind.forEach(w => {
ctx.fillRect(w.x, w.y, 1, w.height)
})
}
function drawBlock() {
ctx.fillStyle = "#0d0"
ctx.fillRect(this.x, this.y, this.width, this.height)
}
function drawRamp() {
ctx.fillStyle = "#0d0"
var delta = this.width * this.slope
ctx.beginPath()
ctx.moveTo(this.x, this.y)
// Note we subtract beause y=0 is top
ctx.lineTo(this.x + this.width, this.y - delta)
ctx.lineTo(this.x + this.width, this.y + this.height - delta)
ctx.lineTo(this.x, this.y + this.height)
ctx.lineTo(this.x, this.y)
ctx.stroke()
ctx.fill()
}
function drawCoin() {
if (!this.collected) {
ctx.fillStyle = "yellow"
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2)
ctx.fill()
}
}
function drawExit(exit) {
ctx.fillStyle = "green"
ctx.beginPath()
ctx.arc(exit.x, exit.y, 20, 0, Math.PI * 2)
ctx.fill()
}
function drawSwitch() {
ctx.fillStyle = "black"
ctx.fillRect(this.x, this.y, this.width, this.height)
}
function drawSwitchRect() {
if (switchState == this.state) {
ctx.fillStyle = "blue"
ctx.fillRect(this.x, this.y, this.width, this.height)
} else {
ctx.stokeStyle = "blue"
ctx.setLineDash([5, 5]);
ctx.beginPath()
ctx.moveTo(this.x, this.y)
ctx.lineTo(this.x + this.width, this.y)
ctx.lineTo(this.x + this.width, this.y + this.height)
ctx.lineTo(this.x, this.y + this.height)
ctx.lineTo(this.x, this.y)
ctx.stroke()
ctx.setLineDash([]);
}
}
|