There is nothing weird with Greeny GRUB controls
WTF with the weird controls in Greeny GRUB? Here is some explaining. Keep reading if you know a little bit about CoffeeScript, Python or C-like syntax:
# Cursor-keys handling
document.onkeydown = (event) ->
__deltaVelocity = 80 # in pixels per second
__switch event.keyCode
____when KEY_UP then player.adjustVy(-deltaVelocity)
____when KEY_LEFT then player.adjustVx(-deltaVelocity)
____when KEY_DOWN then player.adjustVy(deltaVelocity)
____when KEY_RIGHT then player.adjustVx(deltaVelocity)
where deltaVelocity will be used by:
- player.adjustVx() and player.adjustVy() to update vX and Vy velocities.
- player.update(fps) to update x and y coordinates
class Player
__constructor: (@x,@y,@vX,@vY) ->
__adjustVx: (deltaVx) ->
____if Math.abs(@vX)<100 or (deltaVx>0 and @vX<0) or (deltaVx<0 and @vX>0)
______@vX+=deltaVx
__adjustVy: (deltaVy) ->
____if Math.abs(@vY)<100 or (deltaVy>0 and @vY<0) or (deltaVy<0 and @vY>0)
______@vY+=deltaVy
__update: (fps)->
____@x+=@vX/fps
____@y+=@vY/fps
The weird behaviour comes from the fact that there is no damping on the velocity once it has been passed to the player’s methods and that I do not handle onkeyup events.