Refactored how robots are handled. Closes #3

This commit is contained in:
Sacha Ligthert 2023-12-06 20:32:08 +01:00
parent fa37a4740b
commit 84a9ee5c52
3 changed files with 45 additions and 24 deletions

View File

@ -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 { if game.player.position.x < r.position.x {
game.robots[i].x-- game.robots[i].position.x--
} else if game.player.position.x > r.x { } else if game.player.position.x > r.position.x {
game.robots[i].x++ game.robots[i].position.x++
} }
if game.player.position.y < r.y { if game.player.position.y < r.position.y {
game.robots[i].y-- game.robots[i].position.y--
} else if game.player.position.y > r.y { } else if game.player.position.y > r.position.y {
game.robots[i].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 { for _, r := range game.robots {
if index != i && pos == r { 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++
} }

View File

@ -16,8 +16,8 @@ func (game *Game) onTrash(pos Position) bool {
return found return found
} }
func (game *Game) addTrash(pos Position) { func (game *Game) addTrash(robot Robot) {
game.trash = append(game.trash, pos) game.trash = append(game.trash, robot.position)
} }
func (game *Game) cleanTrash() { func (game *Game) cleanTrash() {

View File

@ -1,6 +1,8 @@
package main package main
import "github.com/gdamore/tcell/v2" import (
"github.com/gdamore/tcell/v2"
)
const ( const (
Up = iota Up = iota
@ -32,7 +34,12 @@ type Game struct {
keypresses chan int keypresses chan int
player Player player Player
level int level int
robots []Position robots []Robot
trash []Position trash []Position
gameover int gameover int
} }
type Robot struct {
id int
position Position
}