player.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package robots
  2. func (game *Game) drawPlayer() {
  3. game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style)
  4. }
  5. func (game *Game) movePlayer(press int) {
  6. if game.blockedByTrash(press) {
  7. game.player.moves++
  8. return
  9. }
  10. if press == Left {
  11. if game.player.position.x != 2 {
  12. game.player.position.x--
  13. game.player.moves++
  14. }
  15. } else if press == Right {
  16. if game.player.position.x != 78 {
  17. game.player.position.x++
  18. game.player.moves++
  19. }
  20. } else if press == Up {
  21. if game.player.position.y != 1 {
  22. game.player.position.y--
  23. game.player.moves++
  24. }
  25. } else if press == Down {
  26. if game.player.position.y != 22 {
  27. game.player.position.y++
  28. game.player.moves++
  29. }
  30. } else if press == upleft {
  31. if game.player.position.x != 2 && game.player.position.y != 1 {
  32. game.player.position.x--
  33. game.player.position.y--
  34. game.player.moves++
  35. }
  36. } else if press == upright {
  37. if game.player.position.x != 78 && game.player.position.y != 1 {
  38. game.player.position.x++
  39. game.player.position.y--
  40. game.player.moves++
  41. }
  42. } else if press == downright {
  43. if game.player.position.x != 78 && game.player.position.y != 22 {
  44. game.player.position.x++
  45. game.player.position.y++
  46. game.player.moves++
  47. }
  48. } else if press == downleft {
  49. if game.player.position.x != 2 && game.player.position.y != 22 {
  50. game.player.position.x--
  51. game.player.position.y++
  52. game.player.moves++
  53. }
  54. } else if press == teleport {
  55. game.teleport()
  56. game.player.teleports++
  57. }
  58. }
  59. // blockedByTrash() Checks if player can move into a direction and if trash isn't blocking
  60. func (game *Game) blockedByTrash(press int) bool {
  61. var checkPos Position
  62. if press == Left {
  63. checkPos.x = game.player.position.x - 1
  64. checkPos.y = game.player.position.y
  65. } else if press == Right {
  66. checkPos.x = game.player.position.x + 1
  67. checkPos.y = game.player.position.y
  68. } else if press == Up {
  69. checkPos.x = game.player.position.x
  70. checkPos.y = game.player.position.y - 1
  71. } else if press == Down {
  72. checkPos.x = game.player.position.x
  73. checkPos.y = game.player.position.y + 1
  74. } else if press == upleft {
  75. checkPos.x = game.player.position.x - 1
  76. checkPos.y = game.player.position.y - 1
  77. } else if press == upright {
  78. checkPos.x = game.player.position.x + 1
  79. checkPos.y = game.player.position.y - 1
  80. } else if press == downright {
  81. checkPos.x = game.player.position.x + 1
  82. checkPos.y = game.player.position.y + 1
  83. } else if press == downleft {
  84. checkPos.x = game.player.position.x - 1
  85. checkPos.y = game.player.position.y + 1
  86. }
  87. return game.onTrash(checkPos)
  88. }
  89. func (game *Game) teleport() {
  90. var pos Position
  91. var safe bool = false
  92. for !safe {
  93. pos = game.randPos()
  94. if !game.onTrash(pos) {
  95. safe = true
  96. }
  97. }
  98. game.player.position.x = pos.x
  99. game.player.position.y = pos.y
  100. }
  101. func (game *Game) onPlayer(pos Position) bool {
  102. var onPlayer bool
  103. if pos.x == game.player.position.x && pos.y == game.player.position.y {
  104. onPlayer = true
  105. } else {
  106. onPlayer = false
  107. }
  108. return onPlayer
  109. }
  110. func (game *Game) resetPlayer() {
  111. game.player.position.x = 40
  112. game.player.position.y = 12
  113. }