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
|
|
|
|
}
|
|
|
|
}
|
2023-12-06 20:32:08 +01:00
|
|
|
rndRobot := Robot{
|
|
|
|
id: i,
|
|
|
|
position: rndPos,
|
|
|
|
}
|
|
|
|
game.robots = append(game.robots, rndRobot)
|
2023-12-02 00:19:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) drawRobots() {
|
|
|
|
for _, r := range game.robots {
|
2023-12-06 20:32:08 +01:00
|
|
|
game.screen.SetContent(r.position.x, r.position.y, '+', nil, game.style)
|
2023-12-02 00:19:33 +01:00
|
|
|
}
|
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
|
2023-12-06 20:32:08 +01:00
|
|
|
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++
|
2023-12-03 22:20:44 +01:00
|
|
|
}
|
|
|
|
|
2023-12-06 20:32:08 +01:00
|
|
|
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++
|
2023-12-03 22:20:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// After all the moves, lets check if any of the rowboz collided with anything
|
2023-12-06 20:32:08 +01:00
|
|
|
for _, r := range game.robots {
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-06 19:26:06 +01:00
|
|
|
// Hit a player? Game over!
|
2023-12-06 20:32:08 +01:00
|
|
|
if game.onPlayer(r.position) {
|
2023-12-03 22:20:44 +01:00
|
|
|
game.gameover = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-06 19:26:06 +01:00
|
|
|
// Robots mingling? Trash!
|
2023-12-06 20:32:08 +01:00
|
|
|
if game.onRobot(r) {
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-06 19:23:47 +01:00
|
|
|
// Delete robot
|
2023-12-06 20:32:08 +01:00
|
|
|
game.deleteRobot(r)
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-06 19:23:47 +01:00
|
|
|
// Create trash
|
|
|
|
game.addTrash(r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-06 19:26:06 +01:00
|
|
|
// Hugging Trash? More trash!
|
2023-12-06 20:32:08 +01:00
|
|
|
if game.onTrash(r.position) {
|
2023-12-06 19:23:47 +01:00
|
|
|
|
|
|
|
// Delete robot
|
2023-12-06 20:32:08 +01:00
|
|
|
game.deleteRobot(r)
|
2023-12-03 22:20:44 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 20:32:08 +01:00
|
|
|
func (game *Game) onRobot(robot Robot) bool {
|
2023-12-06 19:26:06 +01:00
|
|
|
var found bool = false
|
2023-12-06 20:32:08 +01:00
|
|
|
for _, r := range game.robots {
|
|
|
|
if robot.id != r.id && robot.position == r.position {
|
2023-12-03 22:20:44 +01:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: improve this
|
2023-12-06 20:32:08 +01:00
|
|
|
func (game *Game) deleteRobot(robot Robot) {
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-06 19:26:06 +01:00
|
|
|
// 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:]...)
|
|
|
|
// }
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-06 19:26:06 +01:00
|
|
|
// 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
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-06 19:26:06 +01:00
|
|
|
// Conclusion: I need to find a different way to do this properly while iterating to that same array.
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-06 20:32:08 +01:00
|
|
|
var rowboz []Robot
|
|
|
|
|
|
|
|
for _, r := range game.robots {
|
|
|
|
if robot != r {
|
|
|
|
rowboz = append(rowboz, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|