keyboardProcessor.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package game
  2. import "github.com/gdamore/tcell/v3"
  3. func (game *Game) keyboardProcessor() {
  4. defer close(game.keypresses)
  5. for game.gameover == 0 {
  6. // Poll for an event
  7. ev := <-game.screen.EventQ()
  8. // Fetch the type, and check if any other actions are required.
  9. switch ev := ev.(type) {
  10. case *tcell.EventResize:
  11. game.screen.Sync()
  12. case *tcell.EventKey:
  13. // Keys to bug out
  14. if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
  15. game.gameover = 1
  16. return
  17. } else if ev.Key() == tcell.KeyCtrlL {
  18. game.screen.Sync()
  19. } else if ev.Key() == 'q' {
  20. game.keypresses <- upleft
  21. } else if ev.Key() == 'e' {
  22. game.keypresses <- upright
  23. } else if ev.Key() == 'z' {
  24. game.keypresses <- downleft
  25. } else if ev.Key() == 'c' {
  26. game.keypresses <- downright
  27. } else if ev.Key() == tcell.KeyUp || ev.Key() == 'w' {
  28. game.keypresses <- up
  29. } else if ev.Key() == tcell.KeyRight || ev.Key() == 'd' {
  30. game.keypresses <- right
  31. } else if ev.Key() == tcell.KeyDown || ev.Key() == 's' || ev.Key() == 'x' {
  32. game.keypresses <- down
  33. } else if ev.Key() == tcell.KeyLeft || ev.Key() == 'a' {
  34. game.keypresses <- left
  35. } else if ev.Key() == 'r' {
  36. game.keypresses <- rotateleft
  37. } else if ev.Key() == 't' {
  38. game.keypresses <- rotateright
  39. } else if ev.Key() == tcell.Key(0x2c) { // Spacebar?
  40. game.keypresses <- action
  41. }
  42. }
  43. }
  44. }