|
@@ -11,13 +11,17 @@ func (game *Game) initRobots() {
|
|
|
found = true
|
|
found = true
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- game.robots = append(game.robots, rndPos)
|
|
|
|
|
|
|
+ rndRobot := Robot{
|
|
|
|
|
+ id: i,
|
|
|
|
|
+ position: rndPos,
|
|
|
|
|
+ }
|
|
|
|
|
+ game.robots = append(game.robots, rndRobot)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) drawRobots() {
|
|
func (game *Game) drawRobots() {
|
|
|
for _, r := range game.robots {
|
|
for _, r := range game.robots {
|
|
|
- game.screen.SetContent(r.x, r.y, '+', nil, game.style)
|
|
|
|
|
|
|
+ game.screen.SetContent(r.position.x, r.position.y, '+', nil, game.style)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -26,34 +30,34 @@ func (game *Game) moveRobots() {
|
|
|
for i, r := range game.robots {
|
|
for i, r := range game.robots {
|
|
|
|
|
|
|
|
// Determine in which direction to go
|
|
// Determine in which direction to go
|
|
|
- if game.player.position.x < r.x {
|
|
|
|
|
- game.robots[i].x--
|
|
|
|
|
- } else if game.player.position.x > r.x {
|
|
|
|
|
- game.robots[i].x++
|
|
|
|
|
|
|
+ if game.player.position.x < r.position.x {
|
|
|
|
|
+ game.robots[i].position.x--
|
|
|
|
|
+ } else if game.player.position.x > r.position.x {
|
|
|
|
|
+ game.robots[i].position.x++
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if game.player.position.y < r.y {
|
|
|
|
|
- game.robots[i].y--
|
|
|
|
|
- } else if game.player.position.y > r.y {
|
|
|
|
|
- game.robots[i].y++
|
|
|
|
|
|
|
+ if game.player.position.y < r.position.y {
|
|
|
|
|
+ game.robots[i].position.y--
|
|
|
|
|
+ } else if game.player.position.y > r.position.y {
|
|
|
|
|
+ game.robots[i].position.y++
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// After all the moves, lets check if any of the rowboz collided with anything
|
|
// After all the moves, lets check if any of the rowboz collided with anything
|
|
|
- for i, r := range game.robots {
|
|
|
|
|
|
|
+ for _, r := range game.robots {
|
|
|
|
|
|
|
|
// Hit a player? Game over!
|
|
// Hit a player? Game over!
|
|
|
- if game.onPlayer(r) {
|
|
|
|
|
|
|
+ if game.onPlayer(r.position) {
|
|
|
game.gameover = 1
|
|
game.gameover = 1
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Robots mingling? Trash!
|
|
// Robots mingling? Trash!
|
|
|
- if game.onRobot(i, r) {
|
|
|
|
|
|
|
+ if game.onRobot(r) {
|
|
|
|
|
|
|
|
// Delete robot
|
|
// Delete robot
|
|
|
- game.deleteRobot(i)
|
|
|
|
|
|
|
+ game.deleteRobot(r)
|
|
|
|
|
|
|
|
// Create trash
|
|
// Create trash
|
|
|
game.addTrash(r)
|
|
game.addTrash(r)
|
|
@@ -61,20 +65,20 @@ func (game *Game) moveRobots() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Hugging Trash? More trash!
|
|
// Hugging Trash? More trash!
|
|
|
- if game.onTrash(r) {
|
|
|
|
|
|
|
+ if game.onTrash(r.position) {
|
|
|
|
|
|
|
|
// Delete robot
|
|
// Delete robot
|
|
|
- game.deleteRobot(i)
|
|
|
|
|
|
|
+ game.deleteRobot(r)
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (game *Game) onRobot(index int, pos Position) bool {
|
|
|
|
|
|
|
+func (game *Game) onRobot(robot Robot) bool {
|
|
|
var found bool = false
|
|
var found bool = false
|
|
|
- for i, r := range game.robots {
|
|
|
|
|
- if index != i && pos == r {
|
|
|
|
|
|
|
+ for _, r := range game.robots {
|
|
|
|
|
+ if robot.id != r.id && robot.position == r.position {
|
|
|
found = true
|
|
found = true
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -82,7 +86,7 @@ func (game *Game) onRobot(index int, pos Position) bool {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// TODO: improve this
|
|
// TODO: improve this
|
|
|
-func (game *Game) deleteRobot(robot int) {
|
|
|
|
|
|
|
+func (game *Game) deleteRobot(robot Robot) {
|
|
|
|
|
|
|
|
// The following would work, if it wasn't for the fact you are iterating through an array,
|
|
// The following would work, if it wasn't for the fact you are iterating through an array,
|
|
|
// and deleting elements form that same array. So deleting item #15 isn't going to work
|
|
// and deleting elements form that same array. So deleting item #15 isn't going to work
|
|
@@ -106,6 +110,16 @@ func (game *Game) deleteRobot(robot int) {
|
|
|
|
|
|
|
|
// Conclusion: I need to find a different way to do this properly while iterating to that same array.
|
|
// Conclusion: I need to find a different way to do this properly while iterating to that same array.
|
|
|
|
|
|
|
|
|
|
+ var rowboz []Robot
|
|
|
|
|
+
|
|
|
|
|
+ for _, r := range game.robots {
|
|
|
|
|
+ if robot != r {
|
|
|
|
|
+ rowboz = append(rowboz, r)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ game.robots = nil
|
|
|
|
|
+ game.robots = rowboz
|
|
|
|
|
+
|
|
|
game.player.score++
|
|
game.player.score++
|
|
|
|
|
|
|
|
}
|
|
}
|