112 lines
2.3 KiB
Go
112 lines
2.3 KiB
Go
package main
|
|
|
|
func (game *Game) initRobots() {
|
|
var fabricate int = game.level * 16
|
|
for i := 0; i < fabricate; i++ {
|
|
var found bool
|
|
var rndPos Position
|
|
for !found {
|
|
rndPos = game.randPos()
|
|
if !game.onPlayer(rndPos) {
|
|
found = true
|
|
}
|
|
}
|
|
game.robots = append(game.robots, rndPos)
|
|
}
|
|
}
|
|
|
|
func (game *Game) drawRobots() {
|
|
for _, r := range game.robots {
|
|
game.screen.SetContent(r.x, r.y, '+', nil, game.style)
|
|
}
|
|
}
|
|
|
|
func (game *Game) moveRobots() {
|
|
// Iterate through the robots
|
|
for i, r := range game.robots {
|
|
|
|
// 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.y < r.y {
|
|
game.robots[i].y--
|
|
} else if game.player.position.y > r.y {
|
|
game.robots[i].y++
|
|
}
|
|
|
|
}
|
|
|
|
// After all the moves, lets check if any of the rowboz collided with anything
|
|
for i, r := range game.robots {
|
|
|
|
// Hit a player? Game over!
|
|
if game.onPlayer(r) {
|
|
game.gameover = 1
|
|
return
|
|
}
|
|
|
|
// Robots mingling? Trash!
|
|
if game.onRobot(i, r) {
|
|
|
|
// Delete robot
|
|
game.deleteRobot(i)
|
|
|
|
// Create trash
|
|
game.addTrash(r)
|
|
|
|
}
|
|
|
|
// Hugging Trash? More trash!
|
|
if game.onTrash(r) {
|
|
|
|
// Delete robot
|
|
game.deleteRobot(i)
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func (game *Game) onRobot(index int, pos Position) bool {
|
|
var found bool = false
|
|
for i, r := range game.robots {
|
|
if index != i && pos == r {
|
|
found = true
|
|
}
|
|
}
|
|
return found
|
|
}
|
|
|
|
// TODO: improve this
|
|
func (game *Game) deleteRobot(robot int) {
|
|
|
|
// 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
|
|
// when the array has been reduced to 13 elements.
|
|
// if robot < len(game.robots) {
|
|
// game.robots = append(game.robots[:robot], game.robots[robot+1:]...)
|
|
// }
|
|
|
|
// This isn't going to work for the same reason,
|
|
// and it adds the issue that random robots will get deleted.
|
|
// var rowboz []Position
|
|
|
|
// for i, r := range game.robots {
|
|
// if robot != i {
|
|
// rowboz = append(rowboz, r)
|
|
// }
|
|
// }
|
|
|
|
// game.robots = nil
|
|
// game.robots = rowboz
|
|
|
|
// Conclusion: I need to find a different way to do this properly while iterating to that same array.
|
|
|
|
game.player.score++
|
|
|
|
}
|