Commit 55e218c6 authored by Evan Hahn's avatar Evan Hahn

Shave off a few more bytes, start with something onscreen

parent 6114e474
......@@ -17,7 +17,6 @@
* r = scalar relative to screen size
* s = ∆t
* u = item constructor
* v = function to push new item
* y = stats
* z = scoreboard
*/
......@@ -27,6 +26,9 @@
// Initial setup
r = Math.min(a.width, a.height) / 30
c.textAlign = 'center'
c.textBaseline = 'middle'
c.font = b.style.font = 'bold 3vh sans-serif'
// Stats
......@@ -42,8 +44,8 @@ y = [
b.appendChild(z = document.createElement('b'))
z.style.position = 'absolute'
z.style.margin = '1vh'
z.style.fontSize = '4vh'
// `z.style.top = 0` is done later to save bytes
// The player
......@@ -77,7 +79,10 @@ u = (type, itemX, itemY, me) => (
c.fillStyle = '#000'
c.fillText(i, itemX, itemY)
if (((itemX - m) ** 2 + (itemY - n) ** 2) < ((r * y[2][2]) ** 2)) {
// This checks for distance to the player (taking its size into account)
if ((((itemX - m) ** 2) +
((itemY - n) ** 2)) <
((r * y[2][2]) ** 2)) {
y[type][2] += y[type][3]
y[type][4] += type === 4 ? y[type][3] : 1
e.splice(e.indexOf(me), 1)
......@@ -87,7 +92,8 @@ u = (type, itemX, itemY, me) => (
// Define the list of entities, starting with a player
e = [_ => {
e = [
_ => {
// y[3][2] === turn speed
k += ((g * y[3][2]) - (f * y[3][2])) * s
......@@ -98,13 +104,10 @@ e = [_ => {
j = Math.min(y[1][2], j + 3 * s)
}
m += Math.cos(k) * j * r * s
n += Math.sin(k) * j * r * s
m = (m + (Math.cos(k) * j * r * s)) % a.width
n = (n + (Math.sin(k) * j * r * s)) % a.height
if (m < 0) { m += a.width }
if (n < 0) { n += a.height }
m = m % a.width
n = n % a.height
// this could probably be smaller ^
c.fillStyle = '#f19'
c.translate(m, n)
......@@ -116,18 +119,9 @@ e = [_ => {
c.lineTo(-r * y[2][2], r * y[2][2])
c.fill()
c.setTransform(1, 0, 0, 1, 0, 0)
}]
// Add a new item periodically
;(v = _ => {
e.push(u(
(Math.random() * 5) | 0,
Math.random() * a.width,
Math.random() * a.height
))
setTimeout(v, Math.random() * 4000)
})()
},
u(0, Math.random() * a.width, Math.random() * a.height)
]
// Tick function
......@@ -136,17 +130,20 @@ requestAnimationFrame(q = (currentTime) => {
s = (l ? currentTime - l : 0) / 1000
l = currentTime
a.width = a.width // clears the screen
if (Math.random() < 0.007) {
e.push(u(
new Date() % 5,
Math.random() * a.width,
Math.random() * a.height
))
}
// canvas font stuff is reset when screen is cleared
c.textAlign = 'center'
c.textBaseline = 'middle'
c.font = b.style.font = 'bold 3vh sans-serif'
c.clearRect(0, 0, a.width, a.height)
e.map(entity => entity())
if (y[4][2] <= 0) {
z.innerHTML = `<font color=#fff>Game over! Score: ${y[0][2]}¢`
if (y[4][2] < 0) {
z.innerHTML = `Game over! Score: ${y[0][2]}¢`
} else {
z.innerHTML = y.map(stat => (
`<font color=${stat[0]}>${stat[1]} ${stat[4]}`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment