From 84a9ee5c52b420aedcaf7640426c8d6cc9d33664 Mon Sep 17 00:00:00 2001 From: Sacha Ligthert Date: Wed, 6 Dec 2023 20:32:08 +0100 Subject: [PATCH] Refactored how robots are handled. Closes #3 --- robots.go | 54 ++++++++++++++++++++++++++++++++++-------------------- trash.go | 4 ++-- types.go | 11 +++++++++-- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/robots.go b/robots.go index 4ab98a2..c978fe8 100644 --- a/robots.go +++ b/robots.go @@ -11,13 +11,17 @@ func (game *Game) initRobots() { found = true } } - game.robots = append(game.robots, rndPos) + 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.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 { // 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.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.y { - game.robots[i].y-- - } else if game.player.position.y > r.y { - game.robots[i].y++ + 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 i, r := range game.robots { + for _, r := range game.robots { // Hit a player? Game over! - if game.onPlayer(r) { + if game.onPlayer(r.position) { game.gameover = 1 return } // Robots mingling? Trash! - if game.onRobot(i, r) { + if game.onRobot(r) { // Delete robot - game.deleteRobot(i) + game.deleteRobot(r) // Create trash game.addTrash(r) @@ -61,20 +65,20 @@ func (game *Game) moveRobots() { } // Hugging Trash? More trash! - if game.onTrash(r) { + if game.onTrash(r.position) { // 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 - for i, r := range game.robots { - if index != i && pos == r { + for _, r := range game.robots { + if robot.id != r.id && robot.position == r.position { found = true } } @@ -82,7 +86,7 @@ func (game *Game) onRobot(index int, pos Position) bool { } // 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, // 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. + var rowboz []Robot + + for _, r := range game.robots { + if robot != r { + rowboz = append(rowboz, r) + } + } + game.robots = nil + game.robots = rowboz + game.player.score++ } diff --git a/trash.go b/trash.go index 2b7cf79..710db8a 100644 --- a/trash.go +++ b/trash.go @@ -16,8 +16,8 @@ func (game *Game) onTrash(pos Position) bool { return found } -func (game *Game) addTrash(pos Position) { - game.trash = append(game.trash, pos) +func (game *Game) addTrash(robot Robot) { + game.trash = append(game.trash, robot.position) } func (game *Game) cleanTrash() { diff --git a/types.go b/types.go index 10f9c9f..913a605 100644 --- a/types.go +++ b/types.go @@ -1,6 +1,8 @@ package main -import "github.com/gdamore/tcell/v2" +import ( + "github.com/gdamore/tcell/v2" +) const ( Up = iota @@ -32,7 +34,12 @@ type Game struct { keypresses chan int player Player level int - robots []Position + robots []Robot trash []Position gameover int } + +type Robot struct { + id int + position Position +}