Refactored how robots are handled. Closes #3
This commit is contained in:
parent
fa37a4740b
commit
84a9ee5c52
54
robots.go
54
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++
|
||||
|
||||
}
|
||||
|
4
trash.go
4
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() {
|
||||
|
11
types.go
11
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user