-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight.js
400 lines (370 loc) · 12.1 KB
/
flight.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
kontra.init('canvas')
var loop
var sprites = []
var uiSprite
// Constants
const COLOR_GREEN = '#33ff33'
const COLOR_AMBER = '#FFBF00'
// Helper functions
var degreesToRadians = function (deg) {
return deg * Math.PI / 180;
}
var normalize = function (n) {
var magnitude = Math.sqrt(n.x*n.x + n.y*n.y)
n = {
x: n.x / magnitude,
y: n.y / magnitude
}
return n
}
var dotProduct = function (v1, v2) {
// The dot product is the sum of products of the vector elements, so for two 2D vectors v1=(dx1,dy1) and v2=(dx2,dy2) the Dot Product is:
// Dot(v1,v2)=(dx1*dx2)+(dy1*dy2)
return v1.x * v2.x + v1.y * v2.y
}
function clamp (val, a, b) {
if (a < b) {
return Math.max(a, Math.min(b, val))
} else {
return Math.max(b, Math.min(a, val))
}
}
var warningFlash = {
anchor: {x: 0.5, y: 0.5},
ttl: 15,
speed: 1,
color: '#ff0000',
width: 25,
height: 25,
radius: 25,
update: function (dt) {
this.radius += this.speed
this.width += this.speed
this.height += this.speed
this.advance()
},
render: function (dt) {
kontra.context.save()
kontra.context.strokeStyle = this.color
kontra.context.beginPath()
// kontra.context.moveTo(this.x, this.y)
kontra.context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI)
kontra.context.stroke()
kontra.context.restore()
}
}
var airplane = {
type: 'plane',
anchor: {
x: 0.5,
y: 0.5
},
x: 0,
y: 240,
width: 25,
height: 25,
radius: 12.5,
dx: 1,
dy: 1,
speed: 0.5,
angle: 0,
color: COLOR_GREEN,
warningCooldown: 0,
onDown: function () {
this.selected = true
this.path = []
},
onUp: function () {
this.selected = false
},
update: function (dt) {
if (!kontra.pointer.pressed('left')) this.selected = false
this.warningCooldown --
if (this.selected) {
// Add to the line
let newPoint = {x: Math.floor(kontra.pointer.x), y: Math.floor(kontra.pointer.y)}
let lastPoint = (this.path.length > 0) ? this.path[this.path.length-1] : null
if (!lastPoint || newPoint.x !== lastPoint.x || newPoint.y !== lastPoint.y) {
this.path.push(newPoint)
}
}
// Move along line speed distance
// If there's more line to follow
if (this.path && this.path.length > 0) {
// Update the angle
let point = this.path[0]
this.angle = Math.atan2(point.y - this.y, point.x - this.x)
}
// If we are out of bounds, turn towards the center
if (this.x < 50 || this.x > kontra.canvas.width- 50 ||
this.y < 50 || this.y > kontra.canvas.height - 50) {
let targetVector = {x: kontra.canvas.width * 0.5 - this.x, y: kontra.canvas.height * 0.5 - this.y}
let targetAngle = Math.atan2(targetVector.y, targetVector.x)
if (Math.abs(targetAngle - this.angle) > Math.PI) targetAngle += 2 * Math.PI
this.angle += (this.angle > targetAngle) ? degreesToRadians(-2) : degreesToRadians(2)
}
if (this.angle < 0) this.angle += degreesToRadians(360)
// Set dx and dy to the angle of the line it's following
this.dx = Math.cos(this.angle) * this.speed
this.dy = Math.sin(this.angle) * this.speed
this.advance()
// If we made it to the point, delete it
while(this.path && this.path.length > 0) {
let point = this.path[0]
let vec = {
x: point.x - this.x,
y: point.y - this.y
}
var distance = Math.sqrt(vec.x * vec.x + vec.y * vec.y)
if (distance > 1) {
break;
}
this.path.shift()
}
// Am I near another plane?
var planes = sprites.filter(s => s.type == 'plane')
planes.forEach(p => {
if (this == p || this.landed || p.landed) return
let x = this.x - p.x
let y = this.y - p.y
let distance = Math.sqrt(x*x + y*y)
if (distance < this.radius + p.radius) {
// Crashed!
loop.stop()
} else if (distance < 3 * (this.radius + p.radius) ) {
// Warning!
if (this.warningCooldown < 0) {
let l = kontra.sprite(warningFlash)
l.x = this.x
l.y = this.y
sprites.push(l)
this.warningCooldown = 25
}
}
})
// Am I landed?
// TODO: Move this to the pad
var pads = sprites.filter(s => s.type == 'pad')
pads.forEach(p => {
if (p.collidesWith(this)) {
// Score!
uiSprite.score++
this.color = "#999999"
this.landed = true
this.angle = p.rotation
this.path = []
kontra.pointer.untrack(this)
this.ttl = 90
}
})
},
render: function (dt) {
// First the path
let context = kontra.context
let line = this.path
if (!this.landed && line && line.length > 0) {
context.strokeStyle = this.color
context.lineWidth = 2,
context.beginPath();
context.moveTo(this.x, this.y)
// context.moveTo(line[0].x, line[0].y)
line.forEach((point) => {
context.lineTo(point.x, point.y)
})
// context.closePath()
context.stroke()
}
// Then the plane
context.save()
context.translate(this.x, this.y)
context.rotate(this.angle)
context.fillStyle = this.color
context.fillRect(-this.radius, -this.radius, 2*this.radius, 2*this.radius)
context.restore()
// kontra.context.font = '36px Courier New'
// context.fillText(""+this.angle, 0,50)
}
}
var landing = {
type: 'pad',
anchor: {
x: 0.5, y: 0.5
},
x: 300,
y: 400,
rotation: degreesToRadians(180),
width: 50,
height: 10,
color: COLOR_GREEN,
update: function (dt) {
if (this.rotation < 0) this.rotation += 2 * Math.PI
},
collidesWith: function (p) {
if (this.color !== p.color) return false
if (p.landed) return false
if (Math.abs(p.angle - this.rotation) > Math.PI/2) {
return false
}
let x = this.x - p.x
let y = this.y - p.y
let distance = Math.sqrt(x*x + y*y)
return (distance < p.radius)
},
render: function (dt) {
kontra.context.save()
let x = this.x
let y = this.y
kontra.context.translate(this.x, this.y)
kontra.context.rotate(this.rotation)
kontra.context.rotate(-Math.PI * 0.5)
// Runway
kontra.context.fillStyle = "#14141f"
kontra.context.fillRect( -this.width * this.anchor.x, -this.height * this.anchor.y, this.width, this.height + 100 )
// Entrance
kontra.context.fillStyle = this.color
kontra.context.fillRect( -this.width * this.anchor.x, -this.height * this.anchor.y, this.width, this.height )
kontra.context.restore()
}
}
var ui = {
score: 0,
planes: 0,
nextPlane: 1,
update: function (dt) {
if (this.highScore === undefined) {
this.highScore = kontra.store.get('flight-high') || 0
}
if (this.score > this.highScore) {
this.highScore = this.score
kontra.store.set('flight-high', this.score)
}
// time out some planes to fly in
if (this.nextPlane === undefined) this.nextPlane = 60 * 6
this.nextPlane--
if (this.nextPlane === 0) {
this.planes++
let a = kontra.sprite(airplane)
a.color = (Math.random() > 0.66) ? COLOR_AMBER : COLOR_GREEN;
a.speed = (a.color == COLOR_GREEN) ? 0.3 : 0.45
let angle = Math.random() * 2 * Math.PI
a.x = Math.cos(angle) * kontra.canvas.width + 0.5 * kontra.canvas.width
a.x = clamp(a.x, -a.radius, kontra.canvas.width + a.radius)
a.angle = angle - Math.PI
a.y = Math.sin(angle) * kontra.canvas.height + 0.5 * kontra.canvas.height
a.y = clamp(a.y, -a.radius, kontra.canvas.height + a.radius)
kontra.pointer.track(a)
sprites.push(a)
this.nextPlane = 60 * 6 - this.planes*5
this.nextPlane = clamp(60 * 6 - this.planes*5, 60 * 3, 6000)
}
},
render: function (dt) {
// blue water
kontra.context.save()
kontra.context.fillStyle = "#0033cc"
kontra.context.fillRect(0, 0, kontra.canvas.width, kontra.canvas.height)
// green land
kontra.context.fillStyle = "#009900"
kontra.context.fillRect(120, 80, 280, 320)
kontra.context.fillRect(70, 190, 280, 320)
// Score
kontra.context.fillStyle = "#ffffff"
kontra.context.font = '36px Courier New'
kontra.context.textBaseline = 'top'
let message = "Score:" + this.score
kontra.context.fillText(message, 0, 0)
let highText = "High:" + this.highScore
let w = kontra.context.measureText(highText).width
kontra.context.fillText(highText, kontra.canvas.width - w, 0)
}
}
var sketch = {
x: 0,
y: 0,
width: kontra.canvas.width,
height:kontra.canvas.height,
lines: [],
color: COLOR_GREEN,
thickness: 5,
onDown: function () {
this.drawing = true;
// Add a new line
this.lines.push([{x: kontra.pointer.x, y: kontra.pointer.y}])
},
onOver: function () {
if (this.drawing) {
let line = this.lines[this.lines.length-1]
line.push({x: kontra.pointer.x, y: kontra.pointer.y})
}
},
onUp: function () {
this.drawing = false;
},
render: function (dt) {
let context = kontra.context
context.strokeStyle = this.color
context.lineWidth = this.thickness,
this.lines.forEach((line) => {
context.beginPath();
context.moveTo(line[0].x, line[0].y)
line.forEach((point) => {
context.lineTo(point.x, point.y)
})
// context.closePath()
context.stroke()
})
}
}
// Set up the GUI
let reset = function() {
sprites.forEach(s=>s.ttl=-1)
// // Drawing
// let s = kontra.sprite(sketch)
// kontra.pointer.track(s)
// sprites.push(s)
uiSprite = kontra.sprite(ui)
sprites.push(uiSprite)
// Landing pads
let l = kontra.sprite(landing)
l.x = 300
l.y = 400
l.rotation = degreesToRadians(180)
sprites.push(l)
let l2 = kontra.sprite(landing)
l2.x = 150
l2.y = 200
l2.rotation = 0
sprites.push(l2)
let l3 = kontra.sprite(landing)
l3.color = COLOR_AMBER
l3.x = 250
l3.y = 250
l3.rotation = degreesToRadians(120)
sprites.push(l3)
loop.start();
}
kontra.keys.bind('r', function() {
console.log("resettting")
reset()
})
let gameOver = function() {
loop.stop()
}
// Boilerplate gameloop
loop = kontra.gameLoop({
update(dt) {
sprites.forEach(sprite => sprite.update(dt))
sprites = sprites.filter(sprite => sprite.isAlive());
},
render() {
sprites.forEach(sprite => sprite.render())
}
});
this.loop = loop
kontra.canvas.addEventListener('mousedown', function (e) {
if (loop.isStopped) reset()
})
kontra.canvas.addEventListener('touchstart', function (e) {
if (loop.isStopped) reset()
})
reset()