I'm working on a 2D game in Node where the character needs to move diagonally. This is a top-down, text-based game purely in a Node environment (no browser, so I don't have nice keydown/keyup events at my disposal).
I'm using the keypress library to read user input but I don't know how to capture two keys at once to cause diagonal movement (e.g. down arrow and right arrow). Here's the code I currently have for horizontal and vertical movement:
game = new Game()
game.print()
keypress(process.stdin)
process.stdin.setRawMode(true)
process.stdin.resume()
process.stdin.on('keypress', (ch, key) ->
return unless key
return process.stdin.pause() if key.ctrl and key.name is 'c'
player = game.player
switch key.name
when 'up'
if key.shift then player.turnUp() else player.moveUp()
when 'right'
if key.shift then player.turnRight() else player.moveRight()
when 'down'
if key.shift then player.turnDown() else player.moveDown()
when 'left'
if key.shift then player.turnLeft() else player.moveLeft()
when 'enter'
player.repeatMove()
when 'b'
player.placeBlock()
game.update()
game.print()
)
This is a turn-based game and currently the run loop of the game is advanced on user input. Instead, I think what I need to do is have an interval-based game update and keep track of the two most recent keypress events and the time between them.
Is there a more robust way?