rowboz/robots/robots.go

103 lines
1.8 KiB
Go
Raw Permalink Normal View History

package robots
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
}
}
rndRobot := Robot{
id: i,
position: rndPos,
}
game.robots = append(game.robots, rndRobot)
}
}
func (game *Game) drawRobots() {
for _, r := range game.robots {
game.screen.SetContent(r.position.x, r.position.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.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.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
for _, r := range game.robots {
// Hit a player? Game over!
if game.onPlayer(r.position) {
game.gameover = 1
return
}
// Robots mingling? Trash!
if game.onRobot(r) {
// Delete robot
game.deleteRobot(r)
// Create trash
game.addTrash(r)
}
// Hugging Trash? More trash!
if game.onTrash(r.position) {
// Delete robot
game.deleteRobot(r)
}
}
}
func (game *Game) onRobot(robot Robot) bool {
var found bool = false
for _, r := range game.robots {
if robot.id != r.id && robot.position == r.position {
found = true
}
}
return found
}
func (game *Game) deleteRobot(robot Robot) {
var rowboz []Robot
for _, r := range game.robots {
if robot != r {
rowboz = append(rowboz, r)
}
}
game.robots = nil
game.robots = rowboz
game.player.score++
}