robots.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // Check if it collides with anything.
  39. if game.onPlayer(r) {
  40. game.gameover = 1
  41. return
  42. }
  43. if game.onRobot(i, r) || game.onTrash(r) {
  44. trash := r
  45. // delete robot
  46. game.deleteRobot(i)
  47. // create trash
  48. game.addTrash(trash)
  49. }
  50. }
  51. }
  52. func (game *Game) onRobot(index int, pos Position) bool {
  53. var found bool
  54. for i, r := range game.robots {
  55. if index != i && pos.x == r.x && pos.y == r.y {
  56. found = true
  57. }
  58. }
  59. return found
  60. }
  61. // TODO: improve this
  62. func (game *Game) deleteRobot(robot int) {
  63. var rowboz []Position
  64. for i, r := range game.robots {
  65. if robot != i {
  66. rowboz = append(rowboz, r)
  67. }
  68. }
  69. game.robots = nil
  70. game.robots = rowboz
  71. }