-
Notifications
You must be signed in to change notification settings - Fork 0
/
tanks.js
326 lines (298 loc) · 9.2 KB
/
tanks.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
const SCREENSHOT_UPDATE_DELAY = 60; // send a screenshot every 60 frames
const COLOR_AMBER = '#FFBF00'
const COLOR_GREEN = '#33ff33'
const COLOR_BLUE = '#0350a0'
const COLOR_DURATION = 150
var Foods = [
{x:0.25, y: 0.25, color: '#3c6e6f'},
{x:0.5, y: 0.25, color: '#007727'},
{x:0.75, y: 0.25, color: '#b8aa01'},
{x:0.25, y: 0.5, color: '#0350a0'},
{x:0.75, y: 0.5, color: '#966401'},
{x:0.25, y: 0.75, color: '#48019d'},
{x:0.5, y: 0.75, color: '#730075'},
{x:0.75, y: 0.75, color: '#9c0e3e'}
]
kontra.init();
var sprites = []
// Utility functions
var degreesToRadians = function (deg) {
return deg * Math.PI / 180;
}
function lerp (min, max, t) {
return min * (1-t) + max * t
}
function damp (a, b, lambda, dt) {
return lerp(a, b, 1 - Math.exp(-lambda * dt))
}
function shuffle(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
// Define the sprites
const FLOOR_COUNT = 24
const FLOOR_WIDTH = kontra.canvas.width / FLOOR_COUNT
const TANK_HEIGHT = 24
var floor = {
type:'floor',
ttl: Infinity,
x:0,
y:0,
color: COLOR_GREEN,
width: FLOOR_WIDTH,
height: kontra.canvas.height,
floorIndex: -1
}
var tank = {
type: 'tank',
ttl: Infinity,
x:3 * FLOOR_WIDTH,
y:0,
color: COLOR_AMBER,
width: kontra.canvas.width / FLOOR_COUNT,
height: TANK_HEIGHT,
}
var score = kontra.sprite({
x:0,
y:480,
hit:0,
color: COLOR_GREEN,
render: function () {
// Make a progress bar
this.context.fillStyle = '#333';
this.context.fillRect(this.x, this.y, kontra.canvas.width, kontra.canvas.height - this.y)
this.context.fillStyle = '#666';
let percent = this.hit / 5.0;
let percentWidth = percent * kontra.canvas.width
this.context.fillRect(this.x, this.y, percentWidth, kontra.canvas.height - this.y)
// Write the hunger
this.context.fillStyle = this.color;
this.context.font = "48px Courier New"
this.context.textBaseline = 'top'
this.context.fillText(this.hit + '/5', this.x, 0)
}
})
sprites.push(score)
var missile = {
type: 'missile',
x: 0,
y: 0,
width:10,
height:10,
ddy: 0.5,
ttl: 4 * 60,
color: '#ff0000',
update: function (dt) {
this.advance()
let b = kontra.sprite({
x:this.x + this.width/2,
y:this.y + this.height/2,
color: '#ffffff',
dx: (Math.random() - 0.5) * 0.25,
dy: (Math.random() - 0.5) * 0.25,
width:4,
height:4,
ttl: 1 * 60,
update: function(dt){ // override to get desired behavior
this.advance()
}
})
sprites.push(b)
let hit = sprites.find(sprite => {
return (sprite.type == 'tank' || sprite.type == 'floor') &&
sprite.collidesWith(this)
})
if (hit) {
let b = kontra.sprite({
anchor: { x: 0.5, y: 0.5 },
x:this.x + this.width/2,
y:this.y + this.height/2,
color: '#ff0000',
dx: (Math.random() - 0.5) * 0.25,
dy: (Math.random() - 0.5) * 0.25,
width:24,
height:24,
ttl: 10,
update: function(dt){ // override to get desired behavior
this.width--
this.height--
this.advance()
}
})
sprites.push(b)
this.ttl = 0;
if (hit.type == 'floor') {
hit.y += 10
}
if (hit.type == 'tank') {
hit.ttl = 0
score.hit++
// Add another hard one
let s = kontra.sprite(tank)
s.update = function (dt) {
this.advance()
this.x = (this.x + kontra.canvas.width) % kontra.canvas.width
}
s.x = i * FLOOR_WIDTH
s.dx = (Math.random() * 1 + 3) * (Math.floor(Math.random()*2) == 0 ? 1 : -1 )
s.y = kontra.canvas.height * 0.05 + (Math.random() * kontra.canvas.height * 0.2)
sprites.push(s)
}
}
let tanksArray = sprites.filter(sprite => sprite.type == 'tank')
let floorArray = sprites.filter(sprite => sprite.type == 'floor')
}
}
var debugSprite = kontra.sprite({
render: function() {
kontra.context.save()
this.context.fillStyle = "#ff00ff";
this.context.font = "24px Courier New"
this.context.textBaseline = 'top'
// this.context.fillText("pointer: " + kontra.pointer.pressed('left'), 0, 0)
kontra.context.restore()
}
})
sprites.push(debugSprite)
// UI Sprites
var angleWidget = kontra.sprite({
anchor: {
x: 0.5,
y: 0.5
},
x: 50,
y: 540,
width: 80,
height: 80,
selected: false,
color: COLOR_AMBER,
angle: 0,
onDown: function() { this.selected = true },
onUp: function() { this.selected = false },
update: function(dt) {
if (!kontra.pointer.pressed('left')) this.selected = false
if (this.selected) {
// make an angle
let dx = kontra.pointer.x - this.x
let dy = kontra.pointer.y - this.y
this.angle = Math.atan2(-dx, dy) + degreesToRadians(90)
}
},
render: function (dt) {
kontra.context.save()
kontra.context.translate(this.x, this.y)
kontra.context.rotate(this.angle)
kontra.context.strokeStyle = this.color
kontra.context.beginPath()
kontra.context.moveTo(-40,-20)
kontra.context.lineTo(0, -20)
kontra.context.lineTo(0, -40)
kontra.context.lineTo(40, 0)
kontra.context.lineTo(0, 40)
kontra.context.lineTo(0, 20)
kontra.context.lineTo(-40, 20)
kontra.context.closePath()
kontra.context.stroke()
kontra.context.restore()
}
})
sprites.push(angleWidget)
kontra.pointer.track(angleWidget)
let powerWidget = kontra.sprite({
x: 120,
y: 500,
width:240,
height:80,
color: COLOR_AMBER,
power: 1,
maxWidth: 240,
onDown: function() { this.selected = true },
onUp: function() { this.selected = false },
update: function (dt) {
if (!kontra.pointer.pressed('left')) this.selected = false
if (this.selected) {
let dx = kontra.pointer.x - this.x
this.width = Math.max(10, Math.min(dx, this.maxWidth))
this.power = this.width / this.maxWidth
}
}
})
sprites.push(powerWidget)
kontra.pointer.track(powerWidget)
let shootButton = kontra.sprite({
anchor: {
x: 0.5,
y: 0.5
},
x: kontra.canvas.width - 50,
y: 540,
width:80,
height: 80,
cooldownFrames: 0,
color: '#ff0000',
onDown: function() {
if (this.cooldownFrames > 0) return;
let player = sprites.find(sprite => sprite.type == 'player')
// Shoot a missile
let s = kontra.sprite(missile)
s.x = player.x
s.y = player.y
s.dx = 20 * (powerWidget.power) * Math.cos(angleWidget.angle)
s.dy = 20 * (powerWidget.power) * Math.sin(angleWidget.angle)
sprites.push(s)
this.cooldownFrames = 30
},
update: function (dt) {
this.cooldownFrames--;
}
})
sprites.push(shootButton)
kontra.pointer.track(shootButton)
let reset = function() {
// Ground
let floorHeight = kontra.canvas.height * 0.65
for (let i = 0; i < FLOOR_COUNT; i++) {
let s = kontra.sprite(floor)
s.x = i * FLOOR_WIDTH
s.floorIndex = i
floorHeight += Math.floor(Math.random() * 15 - 10)
floorHeight = Math.max(0, Math.min(480,floorHeight)) // clamp
s.y = floorHeight
sprites.unshift(s)
}
// Player
var player = kontra.sprite(tank)
player.type = 'player'
player.color = COLOR_BLUE
player.x = 3 * FLOOR_WIDTH
player.y = sprites.find(sprite => sprite.floorIndex == 3).y - TANK_HEIGHT
sprites.push(player)
// Enemies
// Three stationary targets
for (let i = 0; i < 3; i++) {
let s = kontra.sprite(tank)
let floorI = (i+1) * 6
s.x = floorI * FLOOR_WIDTH
s.y = sprites.find(sprite => sprite.floorIndex == floorI).y - TANK_HEIGHT
sprites.push(s)
}
}
var loop = kontra.gameLoop({ // create the main game loop
fps: 60,
update(dt) { // update the game state
sprites.forEach(sprite => sprite.update(dt))
sprites = sprites.filter(sprite => sprite.isAlive());
},
render() { // render the game state
sprites.forEach(sprite => sprite.render())
}
});
reset()
loop.start();
this.loop = loop