diff --git a/game.go b/game.go index 189e319..bd2bbb6 100644 --- a/game.go +++ b/game.go @@ -19,11 +19,16 @@ func gameinit() { // Quit the screen quit(&game) + fmt.Println("\nDebug time: Trash") + for i, t := range game.trash { + fmt.Println("Trash: ", i, t) + } + // Give closure if game.gameover == 1 { - fmt.Println("You get rekt by a robot") + fmt.Println("\nYou got rekt by a robot") } else if game.gameover == 2 { - fmt.Println("You took the easy way out") + fmt.Println("\nYou took the easy way out") } fmt.Println("Score:", game.player.score) @@ -72,28 +77,38 @@ func initialize() (Game, error) { func (game *Game) gameDirector() { defer quit(game) - for { + for game.gameover == 0 { // Clean the screen game.screen.Clear() // Draw starter conditions game.drawBox() + game.drawScore() game.drawPlayer() - game.drawCoords() + //game.drawCoords() + //game.drawDebug() // Draw robots?? if len(game.robots) == 0 { + game.level++ + game.cleanTrash() game.initRobots() } game.drawRobots() + // Draw my ex-wife + game.drawTrash() + // Draw the screen game.screen.Show() // Process input game.movePlayer(game, <-game.keypresses) + // Move robots + game.moveRobots() + } } @@ -101,7 +116,7 @@ func (game *Game) gameDirector() { func (game *Game) keyboardProcessor() { defer close(game.keypresses) - for { + for game.gameover == 0 { // Poll for an event ev := game.screen.PollEvent() @@ -154,7 +169,7 @@ func quit(game *Game) { } func (game *Game) randPos() Position { - var safe bool + var safe bool = false var pos Position for !safe { @@ -164,9 +179,11 @@ func (game *Game) randPos() Position { if x == 0 || x == 79 || y == 0 || y == 23 { safe = false } else { - pos = Position{x: x, y: y} + pos.x = x + pos.y = y safe = true } } + return pos } diff --git a/player.go b/player.go index 78cee47..16ea10b 100644 --- a/player.go +++ b/player.go @@ -1,6 +1,8 @@ package main -import "math/rand" +import ( + "math/rand" +) func (game *Game) drawPlayer() { game.screen.SetContent(game.player.position.x, game.player.position.y, '@', nil, game.style) @@ -59,8 +61,24 @@ func (g *Game) movePlayer(game *Game, press int) { func (game *Game) teleport() { // Draw something nice - game.player.position.x = rand.Intn(80) - game.player.position.y = rand.Intn(24) + // Use 1+rand.Intn(77) instead this + + var safe bool = false + var x, y int + + for !safe { + x = rand.Intn(80) + y = rand.Intn(24) + + if x == 0 || x == 79 || y == 0 || y == 23 { + safe = false + } else { + safe = true + } + } + + game.player.position.x = x + game.player.position.y = y } func (game *Game) onPlayer(pos Position) bool { diff --git a/render.go b/render.go index d348aac..4155e68 100644 --- a/render.go +++ b/render.go @@ -1,6 +1,9 @@ package main -import "strconv" +import ( + "runtime" + "strconv" +) // drawBox Draw the outline of the box the snake can move in func (game *Game) drawBox() { @@ -41,7 +44,23 @@ func (game *Game) drawBox() { // drawCoords Print the coordinates of the player func (game *Game) drawCoords() { var x, y int = 25, 24 - for _, r := range []rune("[ x:" + strconv.FormatInt(int64(game.player.position.x), 10) + " y: " + strconv.FormatInt(int64(game.player.position.y), 10) + " ]") { + for _, r := range []rune("[ x:" + strconv.FormatInt(int64(game.player.position.x), 10) + " y:" + strconv.FormatInt(int64(game.player.position.y), 10) + " ]") { + game.screen.SetContent(x, y-1, r, nil, game.style) + x++ + } +} + +func (game *Game) drawDebug() { + var x, y int = 40, 24 + for _, r := range []rune("[ NumGoRoutine: " + strconv.FormatInt(int64(runtime.NumGoroutine()), 10) + " ]") { + game.screen.SetContent(x, y-1, r, nil, game.style) + x++ + } +} + +func (game *Game) drawScore() { + var x, y int = 5, 24 + for _, r := range []rune("[ Score: " + strconv.FormatInt(int64(game.player.score), 10) + " Level: " + strconv.FormatInt(int64(game.level), 10) + " ]") { game.screen.SetContent(x, y-1, r, nil, game.style) x++ } diff --git a/robots.go b/robots.go index 8e69946..bf354e4 100644 --- a/robots.go +++ b/robots.go @@ -1,24 +1,18 @@ package main -import "fmt" - func (game *Game) initRobots() { var fabricate int = game.level * 16 for i := 0; i < fabricate; i++ { var found bool var rndPos Position for !found { - rndPos := game.randPos() + rndPos = game.randPos() if !game.onPlayer(rndPos) { found = true } } game.robots = append(game.robots, rndPos) } - - for _, r := range game.robots { - fmt.Println("xy", r.x, r.y) - } } func (game *Game) drawRobots() { @@ -27,8 +21,71 @@ func (game *Game) drawRobots() { } } -func (game *Game) drawTrash() { - for _, t := range game.trash { - game.screen.SetContent(t.x, t.y, '*', nil, game.style) +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) + } + } + + game.robots = nil + game.robots = rowboz + +} diff --git a/trash.go b/trash.go new file mode 100644 index 0000000..81e4d1a --- /dev/null +++ b/trash.go @@ -0,0 +1,25 @@ +package main + +func (game *Game) drawTrash() { + for _, t := range game.trash { + game.screen.SetContent(t.x, t.y, '*', nil, game.style) + } +} + +func (game *Game) onTrash(pos Position) bool { + var found bool + for _, t := range game.trash { + if t.x == pos.x && t.y == pos.y { + found = true + } + } + return found +} + +func (game *Game) addTrash(pos Position) { + game.trash = append(game.trash, pos) +} + +func (game *Game) cleanTrash() { + game.trash = nil +} diff --git a/types.go b/types.go index dda07dc..10f9c9f 100644 --- a/types.go +++ b/types.go @@ -26,10 +26,6 @@ type Player struct { teleports int } -type Robot struct { - position Position -} - type Game struct { screen tcell.Screen style tcell.Style