-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathair-fight-reimagined-javascript.html
More file actions
424 lines (368 loc) · 7.35 KB
/
air-fight-reimagined-javascript.html
File metadata and controls
424 lines (368 loc) · 7.35 KB
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<canvas id="canv" width="400" height="360" style="border:1px solid #ccc;" />
<script>
// set a dot at screen
function setd(ctx, x, y) {
ctx.fillStyle = "#ffffff";
dot(ctx, x, y);
}
// clear a dot at screen
function cleard(ctx, x, y) {
ctx.fillStyle = "#000000";
dot(ctx, x, y);
}
// set or celar at dot at screen
function dot(ctx, x, y) {
ctx.fillRect(x * 5, y * 5, 5, 5);
}
// aeroplanes
first = new Array(8);
first[0] = [
0, 1, 0,
1, 1, 1,
0, 0, 0
];
first[1] = [
1, 0, 1,
0, 1, 0,
1, 0, 0
];
first[2] = [
0, 1, 0,
1, 1, 0,
0, 1, 0];
first[3] = [
1, 0, 0,
0, 1, 0,
1, 0, 1
];
first[4] = [
0, 0, 0,
1, 1, 1,
0, 1, 0
];
first[5] = [
0, 0, 1,
0, 1, 0,
1, 0, 1
];
first[6] = [
0, 1, 0,
0, 1, 1,
0, 1, 0
];
first[7] = [
1, 0, 1,
0, 1, 0,
0, 0, 1
];
second = new Array(8);
second[0] = [
0, 1, 0,
1, 1, 1,
1, 0, 1
];
second[1] = [
1, 1, 1,
1, 1, 0,
1, 0, 0
];
second[2] = [
0, 1, 1,
1, 1, 0,
0, 1, 1
];
second[3] = [
1, 0, 0,
1, 1, 0,
1, 1, 1
];
second[4] = [
1, 0, 1,
1, 1, 1,
0, 1, 0
];
second[5] = [
0, 0, 1,
0, 1, 1,
1, 1, 1
];
second[6] = [
1, 1, 0,
0, 1, 1,
1, 1, 0
];
second[7] = [
1, 1, 1,
0, 1, 1,
0, 0, 1
];
// array of arrays
aeroplanes = [first, second];
// projectile
proj = [
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
];
// clear away the plane
function clear_aeroplane(ctx, x, y, angle) {
plane(ctx, x, y, angle, false);
}
// draw the plane
function draw_aeroplane(ctx, x, y, angle) {
plane(ctx, x, y, angle, true);
}
// routine for draing or clearing an aeroplane
function plane(ctx, x, y, angle, drawflag) {
angle.forEach(function(item, index, array) {
row = (index % 3);
column = Math.floor(index / 3);
if (item == 1)
if (drawflag)
setd(ctx, row + x, column + y);
else
cleard(ctx, row + x, column + y);
});
}
// fire
function shot(n) {
// calculate direction if shot in previous direction
switch(f[n]) {
case 0:
q[n] -= w;
break;
case 1:
q[n] -= w;
p[n] -= w;
break;
case 2:
p[n] -= w;
break;
case 3:
p[n] -= w;
q[n] += w;
break;
case 4:
q[n] += w;
break;
case 5:
q[n] += w;
p[n] += w;
break;
case 6:
p[n] += w;
break;
case 7:
p[n] += w;
q[n] -= w;
break;
}
// wrap around screen (or limits for the shot)
if (q[n] < MINY) q[n] = MAXY;
if (q[n] > MAXY) q[n] = MINY;
if (p[n] > MAXX) p[n] = MINX;
if (p[n] < MINX) p[n] = MAXX;
// clear previous dot and draw a new a step ahead
cleard(ctx, p1[n], q1[n]);
setd(ctx, p[n], q[n]);
// store the old position
p1[n] = p[n];
q1[n] = q[n];
}
// hacking away .. really no considerations
// for global variables and/or local .. well
// this is what "prototype programming" looks like
// figuring out what happends, what works,
// not considering security, calls or whatever
// general init
var c = document.getElementById("canv");
var ctx = c.getContext("2d");
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, 400, 360);
// check for limits
MAXY = 72;
MAXX = 80;
MINY = 0;
MINX = 0;
// init angle with start values
u = []; u1 = [];
u[0] = 2; u[1] = 6;
u1[0] = u[0]; u1[1] = u[1];
v = []; v[0] = 3; v[1] = 3;
// this is an easy path instead of
// checking through the variables
old_aeroplane = [];
// init x/y with start values
x = []; x1 = [];
y = []; y1 = [];
x[0] = 40;
x[1] = 8;
x1[0] = x[0];
x1[1] = x[1];
y[0] = 27;
y[1] = 47;
y1[0] = y[0];
y1[1] = y[1];
// step for planes -
// they will be drawn for every frame
k = 1;
// -----
p = []; q = [];
p[0] = q[0] = 0;
p[1] = q[1] = 0;
p1 = []; q1 = [];
p1[0] = q1[0] = 0;
p1[1] = q1[1] = 0;
// step for shots
// only drawn every third frame
// and N.B. the shot will not be checked
// for every frame!
// true WWI dogfight?
w = 3;
// shot is on its way
z = [];
z[0] = z[1] = 0;
// direction of shot
f = [];
f[0] = f[1] = 0;
// -----
// start with one aeroplane (one is 0 and the other is 1)
// will have to start somewhere
n = 1;
// ... game loop
function main() {
// change aeroplane - check the other aeroplane
if (n == 0) { n = 1; } else { n = 0; }
// rotate aeroplane by keys
// right
if (n == 0) {
if (keys[39]) v[0] -= 1;
if (keys[37]) v[0] += 1;
// left
} else {
if (keys[83]) v[1] -= 1;
if (keys[65]) v[1] += 1;
}
// "gearing"?
if (v[n] < 1) {
v[n] = 3;
u[n] -= 1;
}
if (v[n] > 5) {
v[n] = 3;
u[n] += 1;
}
// rotate aeroplane around "axis"
if (u[n] > 7) u[n] = 0;
if (u[n] < 0) u[n] = 7;
// calculate direction in x/y-plane
switch(u[n]) {
case 0:
y[n] -= k;
break;
case 1:
y[n] -= k;
x[n] -= k;
break;
case 2:
x[n] -= k;
break;
case 3:
x[n] -= k;
y[n] += k;
break;
case 4:
y[n] += k;
break;
case 5:
y[n] += k;
x[n] += k;
break;
case 6:
x[n] += k;
break;
case 7:
x[n] += k;
y[n] -= k;
break;
}
// check for screen limits and adjust
if (y[n] < MINY) y[n] = MAXY;
if (y[n] > MAXY) y[n] = MINY;
if (x[n] > MAXX) x[n] = MINX;
if (x[n] < MINX) x[n] = MAXX;
// old_aeroplane = aeroplanes[n][u1[n]];
aeroplane = aeroplanes[n][u[n]];
// simple cheat (looks rather nasty)
if (typeof old_aeroplane[n] != 'undefined')
clear_aeroplane(ctx, x1[n], y1[n], old_aeroplane[n]);
draw_aeroplane(ctx, x[n], y[n], aeroplane);
// save old position and angle
u1[n] = u[n];
x1[n] = x[n];
y1[n] = y[n];
// save the old aeroplane
old_aeroplane[n] = aeroplane;
// trigger a shot
if (keys[32] && !z[0]) {
z[0] = 15;
f[0] = u[0];
q[0] = y[0]+1;
p[0] = x[0]+1;
}
// trigger another shot
if (keys[87] && !z[1]) {
z[1] = 15;
f[1] = u[1];
q[1] = y[1]+1;
p[1] = x[1]+1;
}
// if a shot is on its way
if (z[n]) {
// check for "collision", a hit?
if (n == 0) m = 1; else m = 0;
// temp
i1 = q[n]; i2 = p[n];
j1 = y[m]; j2 = x[m];
// flag for a hit
b = (
(i1 === j1-1) && (i2 === j2-1) ||
(i1 === j1-1) && (i2 === j2) ||
(i1 === j1-1) && (i2 === j2+1) ||
(i1 === j1) && (i2 === j2-1) ||
(i1 === j1) && (i2 === j2) ||
(i1 === j1) && (i2 === j2+1) ||
(i1 === j1+1) && (i2 === j2-1) ||
(i1 === j1+1) && (i2 === j2) ||
(i1 === j1+1) && (i2 === j2+1)
);
// test the flag
if (b) {
// we have a hit
z[n] = 0;
draw_aeroplane(ctx, j2-2, j1-2, proj);
exit();
} else {
// draw shot
shot(n);
// count down for the range of the shot
z[n] -= 1;
}
// get rid of last dot
if (z[n] < 1)
cleard(ctx, p[n], q[n]);
}
// looping ...
requestAnimationFrame(main);
}
// init keys
keys = [];
keys[37] = keys[39] = keys[32] = 0;
keys[65] = keys[83] = keys[87] = 0;
// event controlled
document.onkeydown = function(e) { keys[e.keyCode] = true }
document.onkeyup = function(e) { keys[e.keyCode] = false }
main();
</script>