Commit 089f03a4 authored by Evan Hahn's avatar Evan Hahn

early work on powerups

parent c296718a
......@@ -3,8 +3,7 @@
* a = canvas
* b = body
* c = context
* d = document
* e = array of entities (q is a method called on every tick)
* e = array of entities (functions that are called)
* f = pressing Left?
* g = pressing Right?
* h = pressing Stop?
......@@ -13,6 +12,8 @@
* q = tick function
* r = scalar relative to screen size
* s = ∆t
* u = item constructor
* z = stats display element
*/
/* eslint-disable no-undef, no-return-assign */
......@@ -21,10 +22,24 @@
a.style.background = '#0f073b'
r = Math.min(a.width, a.height) / 30
b.style.font = c.font = '3vh sans-serif'
c.textAlign = 'center'
c.textBaseline = 'middle'
// The player
p = ((playerX, playerY, playerVelocity, playerAcceleration, playerDirection) => {
p = (() => {
let playerX = a.width / 2
let playerY = a.height / 2
let playerSpeed = 0
let playerAcceleration = 3
let playerDirection = 0
let playerTopSpeed = r / 2
let playerBoost = r / 2
let playerWeight = 100
let playerTurn = 5
f = g = h = 0
onkeydown = event => {
if (event.keyCode === 32) { h = 1 }
......@@ -32,41 +47,76 @@ p = ((playerX, playerY, playerVelocity, playerAcceleration, playerDirection) =>
if (event.keyCode === 68) { g = 1 }
}
onkeyup = event => {
if (event.keyCode === 32) { h = 0 }
if (event.keyCode === 32) {
h = 0
playerSpeed += playerBoost
}
if (event.keyCode === 65) { f = 0 }
if (event.keyCode === 68) { g = 0 }
}
return {
q: () => {
playerDirection += ((g * 5) - (f * 5)) * s
playerVelocity = Math.min(r, playerVelocity + playerAcceleration * s)
playerX += Math.cos(playerDirection) * playerVelocity * r * s
playerY += Math.sin(playerDirection) * playerVelocity * r * s
if (playerX < 0) { playerX += a.width }
if (playerY < 0) { playerY += a.height }
playerX = playerX % a.width
playerY = playerY % a.height
// this could probably be smaller ^
c.fillStyle = '#ffff00'
c.translate(playerX, playerY)
c.rotate(playerDirection)
c.beginPath()
c.moveTo(-r, -r)
c.lineTo(r, 0)
c.lineTo(-r, r)
c.fill()
c.setTransform(1, 0, 0, 1, 0, 0)
b.appendChild(z = document.createElement('center'))
z.style.width = '100%'
z.style.position = 'absolute'
z.style.bottom = 0
z.style.paddingBottom = '1em'
z.style.fontSize = '3vh'
return () => {
playerDirection += ((g * playerTurn) - (f * playerTurn)) * s
if (h) {
playerSpeed = Math.max(0, playerSpeed - playerWeight * s)
} else {
playerSpeed = Math.min(playerTopSpeed, playerSpeed + playerAcceleration * s)
}
playerX += Math.cos(playerDirection) * playerSpeed * r * s
playerY += Math.sin(playerDirection) * playerSpeed * r * s
if (playerX < 0) { playerX += a.width }
if (playerY < 0) { playerY += a.height }
playerX = playerX % a.width
playerY = playerY % a.height
// this could probably be smaller ^
c.fillStyle = '#ffff00'
c.translate(playerX, playerY)
c.rotate(playerDirection)
c.beginPath()
c.moveTo(-r, -r)
c.lineTo(r, 0)
c.lineTo(-r, r)
c.fill()
c.setTransform(1, 0, 0, 1, 0, 0)
z.innerHTML = `<font color="aqua">⇒ ${playerTopSpeed}</font>`
}
})()
// Item
u = (type) => {
let x = Math.random() * a.width
let y = Math.random() * a.height
return () => {
let icon
[c.fillStyle, icon] = [
['aqua', '⇒']
][type]
c.beginPath()
c.arc(x, y, r / 2, 0, 7)
c.fill()
c.fillStyle = 'black'
c.fillText(icon, x, y)
}
})(a.width / 2, a.height / 2, 0, 3, 0)
}
// Define the list of entities
e = [p]
e = [p, u(0)]
// Tick function
......@@ -77,7 +127,7 @@ requestAnimationFrame(q = (currentTime) => {
a.width = a.width // clears the screen
e.map(entity => entity.q())
e.map(entity => entity())
requestAnimationFrame(q)
})
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