robots.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. func (game *Game) initRobots() {
  3. var fabricate int = game.level * 16
  4. for i := 0; i < fabricate; i++ {
  5. var found bool
  6. var rndPos Position
  7. for !found {
  8. rndPos = game.randPos()
  9. if !game.onPlayer(rndPos) {
  10. found = true
  11. }
  12. }
  13. game.robots = append(game.robots, rndPos)
  14. }
  15. }
  16. func (game *Game) drawRobots() {
  17. for _, r := range game.robots {
  18. game.screen.SetContent(r.x, r.y, '+', nil, game.style)
  19. }
  20. }
  21. func (game *Game) moveRobots() {
  22. // Iterate through the robots
  23. for i, r := range game.robots {
  24. // Determine in which direction to go
  25. if game.player.position.x < r.x {
  26. game.robots[i].x--
  27. } else if game.player.position.x > r.x {
  28. game.robots[i].x++
  29. }
  30. if game.player.position.y < r.y {
  31. game.robots[i].y--
  32. } else if game.player.position.y > r.y {
  33. game.robots[i].y++
  34. }
  35. }
  36. // After all the moves, lets check if any of the rowboz collided with anything
  37. for i, r := range game.robots {
  38. // Hit a player? Game over
  39. if game.onPlayer(r) {
  40. game.gameover = 1
  41. return
  42. }
  43. // Robots mingling? Trash
  44. if game.onRobot(i, r) {
  45. // Delete robot
  46. game.deleteRobot(i)
  47. // Create trash
  48. game.addTrash(r)
  49. }
  50. // Colided with Trash
  51. if game.onTrash(r) {
  52. // Delete robot
  53. game.deleteRobot(i)
  54. }
  55. }
  56. }
  57. func (game *Game) onRobot(index int, pos Position) bool {
  58. var found bool
  59. for i, r := range game.robots {
  60. if index != i && pos.x == r.x && pos.y == r.y {
  61. found = true
  62. }
  63. }
  64. return found
  65. }
  66. // TODO: improve this
  67. func (game *Game) deleteRobot(robot int) {
  68. var rowboz []Position
  69. for i, r := range game.robots {
  70. if robot != i {
  71. rowboz = append(rowboz, r)
  72. }
  73. }
  74. game.robots = nil
  75. game.robots = rowboz
  76. game.player.score++
  77. }