aboutsummaryrefslogtreecommitdiff
path: root/src/paperflight/static/draw.js
blob: 4ba138b443e325bac7dc1f8d4615dc9a6b94405a (plain)
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
/**
 * 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)

    if(gameIsOver){
        ctx.font = "20px Courier"
        ctx.fillStyle = "black"
        ctx.fillText("Game Over!", 200, 200);
    }
}
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 ((this.state == -1 || this.state == switchState) && 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 drawDrip(){
    ctx.fillStyle = "#00F"
    if (t % this.frequency == 0) {
        this.drips.push({
            x: this.x,
            y: this.y,
            radius: 8
        })
    }
    this.drips.forEach(drip => {
        drip.y += 3
    })
    // Remove all wind higher than certain height
    this.drips = this.drips.filter(drip => {
        return drip.y < height
    })
    // Finally draw each wind
    this.drips.forEach(drip => {
        ctx.beginPath()
        ctx.arc(drip.x, drip.y, drip.radius, 0, Math.PI * 2)
        ctx.fill()
    })
}
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) {
    if(collected >= currLevel.coinsNeeded){
        ctx.fillStyle = "green"
    } else {
        ctx.fillStyle = "red"
    }
    
    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([]);
    }
}
function drawText(){
    ctx.fillStyle = this.style
    ctx.font = this.font
    ctx.fillText(this.text, this.x, this.y)
}