robots.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package robots
  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. rndRobot := Robot{
  14. id: i,
  15. position: rndPos,
  16. }
  17. game.robots = append(game.robots, rndRobot)
  18. }
  19. }
  20. func (game *Game) drawRobots() {
  21. for _, r := range game.robots {
  22. game.screen.SetContent(r.position.x, r.position.y, '+', nil, game.style)
  23. }
  24. }
  25. func (game *Game) moveRobots() {
  26. // Iterate through the robots
  27. for i, r := range game.robots {
  28. // Determine in which direction to go
  29. if game.player.position.x < r.position.x {
  30. game.robots[i].position.x--
  31. } else if game.player.position.x > r.position.x {
  32. game.robots[i].position.x++
  33. }
  34. if game.player.position.y < r.position.y {
  35. game.robots[i].position.y--
  36. } else if game.player.position.y > r.position.y {
  37. game.robots[i].position.y++
  38. }
  39. }
  40. // After all the moves, lets check if any of the rowboz collided with anything
  41. for _, r := range game.robots {
  42. // Hit a player? Game over!
  43. if game.onPlayer(r.position) {
  44. game.gameover = 1
  45. return
  46. }
  47. // Robots mingling? Trash!
  48. if game.onRobot(r) {
  49. // Delete robot
  50. game.deleteRobot(r)
  51. // Create trash
  52. game.addTrash(r)
  53. }
  54. // Hugging Trash? More trash!
  55. if game.onTrash(r.position) {
  56. // Delete robot
  57. game.deleteRobot(r)
  58. }
  59. }
  60. }
  61. func (game *Game) onRobot(robot Robot) bool {
  62. var found bool = false
  63. for _, r := range game.robots {
  64. if robot.id != r.id && robot.position == r.position {
  65. found = true
  66. }
  67. }
  68. return found
  69. }
  70. func (game *Game) deleteRobot(robot Robot) {
  71. var rowboz []Robot
  72. for _, r := range game.robots {
  73. if robot != r {
  74. rowboz = append(rowboz, r)
  75. }
  76. }
  77. game.robots = nil
  78. game.robots = rowboz
  79. game.player.score++
  80. }