A=600;B=300;C="splice";g=window;k=c.style;l=Math;S=l.sqrt;m=l.pow;o=l.random;p=b=iterator=0;q=[];t=[];u=15;v=8;w={};function x(e,h,r,s){a.beginPath();a.arc(e,h,s||25,0,9, false);a.fillStyle=r;a.fill()}function y(e,h,r,s,z){t.push({x:e+p,y:h+b,c:r+p,d:s+b,b:0,f:2/S(m(r-e,2)+m(s-h,2)),a:z})}setInterval(function(){c.width=c.height=A;k.width=k.height=A+"px";w[87]&&b--;w[83]&&b++;w[65]&&p--;w[68]&&p++;iterator%100||q.push({x:o()*A+p,y:o()*A+b,e:o()-0.5,a:o()-0.5});for(d in q)d=q[d],o()<v/1E4&&y(B,B,d.x-p,d.y-b,1),d.y+=d.e,d.x+=d.a,x(d.x-p,d.y-b,"red");x(B,B,"lime",u*2);for(i in t){f=t[i];var e=f.c+(f.x-f.c)*f.b,h=f.d+(f.y-f.d)*f.b;x(e-p,h-b,f.a?"gold":"gray",3);f.a&&S(m(B+p-e,2)+m(B+b-h,2))<u*2&&(--u||(alert(v),history.go(0)),t[C](i,1));for(j in q)d=q[j],!f.a&& S(m(d.x-e,2)+m(d.y-h,2))<28&&(t[C](i,1),q[C](j,1),v+=~~(o()*4)+1);(f.b+=f.f)>9&&t[C](i,1)}a.fillText(v,9,9);iterator++},9);g.onkeydown=g.onkeyup=g.onclick=function(e){w[e.keyCode]=e.type!="keyup";e.type=="click"&&v&&(y(e.clientX,e.clientY,B,B),--v||v++)}
// compression directions:
// google closure, then
// remove 'var'
// remove unneeded vars
// remove end semicolon
// 800 and 400 to vars
// .splice to var [var]
// math.sqrt
// remove space
var boardWidth, y, enemy, bull
var windoww = window,
canvasStyle = c.style,
middle = 300,
size = 600,
math = Math,
splicee = "splice",
power = math.pow,
rand = math.random,
x = y = iterator = 0,
enemies = [],
bullets = [],
health = 15,
score = 8,
keyStates = {}
function draw (x, y, color, size) {
a.beginPath();
a.arc(x, y, size || 25, 0, 9, false)
a.fillStyle = color
a.fill()
}
function distance (a, b, c, d) {
return math.sqrt(power(a - b, 2) + power(c - d, 2))
}
function bullet (destX, destY, sourceX, sourceY, isEnemy) {
bullets.push({
x: destX + x,
y: destY + y,
c: sourceX + x,
u: sourceY + y,
p: 0,
s: 2 / distance(sourceX, destX, sourceY, destY),
e: isEnemy
})
}
setInterval(function () {
c.width = c.height = boardWidth = size
canvasStyle.width = canvasStyle.height = boardWidth + "px"
// move!
// w
keyStates[87] && y--
// s
keyStates[83] && y++
// a
keyStates[65] && x--
// d
keyStates[68] && x++
if (! (iterator % 100)) {
// add enemy
enemies.push({
x: rand() * boardWidth + x,
y: rand() * boardWidth + y,
d: rand() - .5, // travel direction vert
e: rand() - .5 // travel direction horz
})
}
// enemies
for (enemy in enemies) {
enemy = enemies[enemy]
// shoot back!
rand() < score / 10000 && bullet(middle, middle, enemy.x - x, enemy.y - y, 1)
// more awesome enemy movement
// if enemy has made it to its destination, make a new destination
// if (! enemy.u || enemy.u == enemy.y)
// enemy.u = enemy.y + ~~(rand() * 200 - 100)
// if (! enemy.c || enemy.c == enemy.x)
// enemy.c = enemy.x + ~~(rand() * 200 - 100)
//
// // move enemies closer to their destinations
// if (iterator % 2) {
// enemy.u < enemy.y ? enemy.y-- : enemy.y++
// enemy.c < enemy.x ? enemy.x-- : enemy.x++
// }
// lame enemy movement
enemy.y += enemy.d
enemy.x += enemy.e
draw(enemy.x - x, enemy.y - y, "red")
}
// draw character
draw(middle, middle, "lime", health * 2)
// update and draw bullets
for (i in bullets) {
bull = bullets[i]
var bulletX = bull.c + (bull.x - bull.c) * bull.p,
bulletY = bull.u + (bull.y - bull.u) * bull.p
draw(bulletX - x, bulletY - y, bull.e ? "gold" : "gray", 3)
// hit detect player
if (bull.e && distance(middle + x, bulletX, middle + y, bulletY) < health * 2) {
if (!--health) {
alert(score)
history.go(0)
}
bullets[splicee](i, 1)
}
// hit detect enemies
for (j in enemies) {
enemy = enemies[j]
if (!bull.e && distance(enemy.x, bulletX, enemy.y, bulletY) < 28) {
bullets[splicee](i, 1)
enemies[splicee](j, 1)
score += ~~(rand() * 4) + 1
}
}
// increment bullet position and remove old bullets - 9 instead of 1 where 1 would be the bullet dies where user clicked
(bull.p += bull.s) > 9 && bullets[splicee](i, 1)
}
a.fillText(score, 9, 9)
iterator++
}, 9)
windoww.onkeydown = windoww.onkeyup = windoww.onclick = function (e) {
keyStates[e.keyCode] = e.type != "keyup"
if (e.type == "click" && score) {
bullet(e.clientX, e.clientY, middle, middle)
// subtract one from score... unless that makes it 0 - then make it 1 again
--score || score++
}
}