2023-12-01 21:57:52 +01:00
|
|
|
package main
|
|
|
|
|
2023-12-02 00:19:33 +01:00
|
|
|
func (game *Game) initRobots() {
|
|
|
|
var fabricate int = game.level * 16
|
|
|
|
for i := 0; i < fabricate; i++ {
|
|
|
|
var found bool
|
|
|
|
var rndPos Position
|
|
|
|
for !found {
|
2023-12-03 22:20:44 +01:00
|
|
|
rndPos = game.randPos()
|
2023-12-02 00:19:33 +01:00
|
|
|
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)
|
|
|
|
}
|
2023-12-01 21:57:52 +01:00
|
|
|
}
|
|
|
|
|
2023-12-03 22:20:44 +01:00
|
|
|
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 {
|
|
|
|
|
|
|
|
// Check if it collides with anything.
|
|
|
|
if game.onPlayer(r) {
|
|
|
|
game.gameover = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if game.onRobot(i, r) || game.onTrash(r) {
|
|
|
|
|
|
|
|
trash := r
|
|
|
|
|
|
|
|
// delete robot
|
|
|
|
game.deleteRobot(i)
|
|
|
|
|
|
|
|
// create trash
|
|
|
|
game.addTrash(trash)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) onRobot(index int, pos Position) bool {
|
|
|
|
var found bool
|
|
|
|
for i, r := range game.robots {
|
|
|
|
if index != i && pos.x == r.x && pos.y == r.y {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: improve this
|
|
|
|
func (game *Game) deleteRobot(robot int) {
|
|
|
|
|
|
|
|
var rowboz []Position
|
|
|
|
|
|
|
|
for i, r := range game.robots {
|
|
|
|
if robot != i {
|
|
|
|
rowboz = append(rowboz, r)
|
|
|
|
}
|
2023-12-02 00:19:33 +01:00
|
|
|
}
|
2023-12-03 22:20:44 +01:00
|
|
|
|
|
|
|
game.robots = nil
|
|
|
|
game.robots = rowboz
|
|
|
|
|
2023-12-04 01:11:33 +01:00
|
|
|
game.player.score++
|
|
|
|
|
2023-12-01 21:57:52 +01:00
|
|
|
}
|