-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmissile-command.html
279 lines (255 loc) · 7.53 KB
/
missile-command.html
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
<!--
* Copyright 2013, Gregg Tavares.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Gregg Tavares. nor the names of his
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE html>
<html>
<title>Missile Command</title>
<style>
body, html {
background: purple;
margin: 20px;
}
canvas {
border: 1px solid red;
width: 640px;
height: 480px;
image-rendering:optimizeSpeed; /* Legal fallback */
image-rendering:-moz-crisp-edges; /* Firefox */
image-rendering:-o-crisp-edges; /* Opera */
image-rendering:-webkit-optimize-contrast; /* Chrome (and eventually Safari) */
image-rendering:optimize-contrast; /* CSS3 Proposed */
}
</style>
<script src="js/webgl-utils.js"></script>
<script>
var canvas;
var ctx;
var frameCount = 0;
var missiles = [];
var explosions = [];
var missileId = 0;
var clearToBlack = function () {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
};
var cycleColors = [
"red",
"green",
"blue",
"white",
];
var getFlashingColor = function() {
return cycleColors[frameCount % cycleColors.length];
};
var setupLine = function(x, y, targetX, targetY) {
var deltaX = targetX - x;
var deltaY = targetY - y;
var deltaRow = Math.abs(deltaX);
var deltaCol = Math.abs(deltaY);
var counter = Math.max(deltaCol, deltaRow);
var axis = counter == deltaCol ? 1 : 0;
// setup a line draw.
var line = {
position: [x, y],
delta: [deltaX, deltaY],
deltaPerp: [deltaRow, deltaCol],
inc: [sign(deltaX), sign(deltaY)],
accum: Math.floor(counter / 2),
counter: counter,
endPnt: counter,
axis: axis,
};
return line;
};
var advanceLine = function(line) {
--line.counter;
if (line.counter <= 0) {
return false;
}
var axis = line.axis;
var perp = 1 - axis;
line.accum += line.deltaPerp[perp];
if (line.accum >= line.endPnt) {
line.accum -= line.endPnt;
line.position[perp] += line.inc[perp];
}
line.position[axis] += line.inc[axis];
return true;
};
var sign = function(v) {
return (v > 0) ? 1 : ((v < 0) ? -1 : 0);
};
var randInt = function(range) {
return Math.floor(Math.random() * range);
};
var drawPixel = function(x, y, color) {
if (!color) {
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.globalCompositeOperation = "destination-out";
} else {
ctx.fillStyle = color;
ctx.globalCompositeOperation = "source-over";
}
ctx.fillRect(x, y, 1, 1);
};
var addMissile = function() {
// top or bottom?
var top = Math.random() > 0.5;
var x = Math.floor(Math.random() * canvas.width);
var y = top ? 0 : canvas.height - 1;
var targetX = Math.floor((Math.random() * 0.6 + 0.2) * canvas.width);
var targetY = Math.floor((Math.random() * 0.3 + top ? 0.5 : 0.2) * canvas.height);
// setup a line draw.
var line = setupLine (x, y, targetX, targetY);
var missile = {
id: ++missileId,
start: [x, y],
target: [targetX, targetY],
line: line,
speed: 1 + randInt(3),
};
missiles.push(missile);
};
var addExplosion = function(x, y) {
var duration = 90;
var explosion = {
x: x,
y: y,
counter: duration,
halfLife: Math.floor(duration / 2),
};
explosions.push(explosion);
};
var killMissile = function(missile) {
// erase missile.
var line = missile.line;
var eraseLine = setupLine(
missile.start[0], missile.start[1],
missile.target[0], missile.target[1]);
eraseLine.counter -= missile.line.counter;
missile.line.counter = 0;
while (advanceLine(eraseLine)) {
drawPixel(eraseLine.position[0], eraseLine.position[1], "black");
}
addExplosion(line.position[0], line.position[1]);
};
var processMissiles = function() {
var color = "black";
for (var ii = 0; ii < missiles.length; ++ii) {
var missile = missiles[ii];
var line = missile.line;
for (var step = 0; step < missile.speed; ++step) {
drawPixel(line.position[0], line.position[1], "red");
if (!advanceLine(line)) {
killMissile(missile);
break;
}
var x = line.position[0];
var y = line.position[1];
var pixel = ctx.getImageData(x, y, 1, 1);
// Check for the flashing color (this means 2 missiles
// can hit each other which is not the real missile command.
if (pixel.data[3] == 0) {
killMissile(missile);
break;
} else {
drawPixel(x, y);
}
}
}
// remove dead missiles.
var m = [];
for (var ii = 0; ii < missiles.length; ++ii) {
var missile = missiles[ii];
if (missile.line.counter > 0) {
m.push(missile);
}
}
missiles = m;
};
var processExplosions = function() {
ctx.lineWidth = 2;
for (var ii = 0; ii < explosions.length; ++ii) {
var explosion = explosions[ii];
--explosion.counter;
var radius = explosion.counter > explosion.halfLife ?
(explosion.halfLife - (explosion.counter % explosion.halfLife)) :
explosion.counter;
if (explosion.counter <= explosion.halfLife) {
ctx.globalCompositeOperation = "source-over";
ctx.strokeColor = "black";
} else {
ctx.globalCompositeOperation = "destination-out";
ctx.strokeColor = "rgba(0,0,0,1)";
}
ctx.beginPath();
ctx.arc(explosion.x, explosion.y, Math.max(0, radius), 0, 360, false);
ctx.stroke();
}
// remove dead explosions.
var e = [];
for (var ii = 0; ii < explosions.length; ++ii) {
var explosion = explosions[ii];
if (explosion.counter > 0) {
e.push(explosion);
}
}
explosions = e;
};
var cycleBackgroundColor = function() {
canvas.style.backgroundColor = getFlashingColor();
};
var process = function() {
cycleBackgroundColor();
if (frameCount % 60 == 0) {
addMissile();
}
processMissiles();
processExplosions();
++frameCount;
requestAnimFrame(process);
}
var startLevel = function() {
clearToBlack();
}
var init = function() {
canvas = document.getElementById("c");
ctx = canvas.getContext("2d");
startLevel();
// addMissile();
// addExplosion(100, 100);
process();
}
window.addEventListener('load', init);
</script>
<body>
<canvas id="c" width="320" height="240"></canvas>
</body>
</html>