2023-12-01 21:57:52 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-12-02 00:19:33 +01:00
|
|
|
"math/rand"
|
2023-12-01 21:57:52 +01:00
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func gameinit() {
|
|
|
|
game, err := initialize()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Initilization error: ", err)
|
|
|
|
}
|
2023-12-02 00:19:33 +01:00
|
|
|
go game.gameDirector()
|
|
|
|
game.keyboardProcessor()
|
2023-12-01 21:57:52 +01:00
|
|
|
|
|
|
|
// Quit the screen
|
|
|
|
quit(&game)
|
|
|
|
|
2023-12-06 19:24:46 +01:00
|
|
|
// fmt.Println("\nDebug time: Trash")
|
|
|
|
// for i, t := range game.trash {
|
|
|
|
// fmt.Println("Trash: ", i, t)
|
|
|
|
// }
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-01 21:57:52 +01:00
|
|
|
// Give closure
|
2023-12-02 00:19:33 +01:00
|
|
|
if game.gameover == 1 {
|
2023-12-03 22:20:44 +01:00
|
|
|
fmt.Println("\nYou got rekt by a robot")
|
2023-12-02 00:19:33 +01:00
|
|
|
} else if game.gameover == 2 {
|
2023-12-03 22:20:44 +01:00
|
|
|
fmt.Println("\nYou took the easy way out")
|
2023-12-02 00:19:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Score:", game.player.score)
|
2023-12-06 20:35:31 +01:00
|
|
|
fmt.Println("Level: ", game.level)
|
2023-12-02 00:19:33 +01:00
|
|
|
fmt.Println("Moves:", game.player.moves)
|
|
|
|
fmt.Println("Teleports:", game.player.teleports)
|
2023-12-01 21:57:52 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func initialize() (Game, error) {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
screen, err := tcell.NewScreen()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error creating screen: ", err)
|
|
|
|
}
|
|
|
|
err = screen.Init()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error initializing screen: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
style := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
|
|
|
|
|
|
|
|
game := Game{
|
|
|
|
screen: screen,
|
|
|
|
style: style,
|
|
|
|
keypresses: make(chan int),
|
|
|
|
player: Player{
|
|
|
|
position: Position{
|
|
|
|
x: 40,
|
|
|
|
y: 12,
|
|
|
|
},
|
2023-12-01 23:08:17 +01:00
|
|
|
score: 0,
|
2023-12-01 21:57:52 +01:00
|
|
|
moves: 0,
|
|
|
|
teleports: 0,
|
|
|
|
},
|
2023-12-02 00:19:33 +01:00
|
|
|
level: 1,
|
2023-12-01 21:57:52 +01:00
|
|
|
// robots: []Robot, // I need this maybe
|
|
|
|
// trash: []Position, // I will need this
|
|
|
|
gameover: 0,
|
|
|
|
}
|
|
|
|
|
2023-12-05 23:29:45 +01:00
|
|
|
game.initRobots()
|
|
|
|
|
2023-12-01 21:57:52 +01:00
|
|
|
return game, err
|
|
|
|
}
|
|
|
|
|
2023-12-02 00:19:33 +01:00
|
|
|
func (game *Game) gameDirector() {
|
2023-12-01 21:57:52 +01:00
|
|
|
defer quit(game)
|
|
|
|
|
2023-12-04 01:32:04 +01:00
|
|
|
// TODO reorder this
|
2023-12-03 22:20:44 +01:00
|
|
|
for game.gameover == 0 {
|
2023-12-01 21:57:52 +01:00
|
|
|
|
|
|
|
// Clean the screen
|
|
|
|
game.screen.Clear()
|
|
|
|
|
|
|
|
// Draw starter conditions
|
2023-12-02 00:19:33 +01:00
|
|
|
game.drawBox()
|
|
|
|
game.drawPlayer()
|
2023-12-03 22:20:44 +01:00
|
|
|
//game.drawCoords()
|
2023-12-01 21:57:52 +01:00
|
|
|
|
|
|
|
// Draw robots??
|
2023-12-02 00:19:33 +01:00
|
|
|
if len(game.robots) == 0 {
|
2023-12-03 22:20:44 +01:00
|
|
|
game.level++
|
|
|
|
game.cleanTrash()
|
2023-12-02 00:19:33 +01:00
|
|
|
game.initRobots()
|
|
|
|
}
|
|
|
|
game.drawRobots()
|
2023-12-01 21:57:52 +01:00
|
|
|
|
2023-12-03 22:20:44 +01:00
|
|
|
// Draw my ex-wife
|
|
|
|
game.drawTrash()
|
|
|
|
|
2023-12-05 23:49:57 +01:00
|
|
|
// Draw stuff last
|
|
|
|
game.drawScore()
|
|
|
|
game.drawDebug()
|
|
|
|
|
2023-12-01 21:57:52 +01:00
|
|
|
// Draw the screen
|
|
|
|
game.screen.Show()
|
|
|
|
|
|
|
|
// Process input
|
2023-12-05 23:20:58 +01:00
|
|
|
game.movePlayer(<-game.keypresses)
|
2023-12-01 21:57:52 +01:00
|
|
|
|
2023-12-03 22:20:44 +01:00
|
|
|
// Move robots
|
|
|
|
game.moveRobots()
|
|
|
|
|
2023-12-01 21:57:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-02 00:19:33 +01:00
|
|
|
func (game *Game) keyboardProcessor() {
|
2023-12-01 21:57:52 +01:00
|
|
|
defer close(game.keypresses)
|
|
|
|
|
2023-12-03 22:20:44 +01:00
|
|
|
for game.gameover == 0 {
|
2023-12-01 21:57:52 +01:00
|
|
|
|
|
|
|
// Poll for an event
|
|
|
|
ev := game.screen.PollEvent()
|
|
|
|
|
|
|
|
// Fetch the type, and check if any other actions are required.
|
|
|
|
switch ev := ev.(type) {
|
|
|
|
case *tcell.EventKey:
|
2023-12-01 22:41:35 +01:00
|
|
|
// Keys to bug out
|
2023-12-01 21:57:52 +01:00
|
|
|
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
|
2023-12-02 00:19:33 +01:00
|
|
|
game.gameover = 2
|
2023-12-01 21:57:52 +01:00
|
|
|
return
|
2023-12-01 22:41:35 +01:00
|
|
|
|
|
|
|
// Screen management
|
2023-12-01 21:57:52 +01:00
|
|
|
} else if ev.Key() == tcell.KeyCtrlL {
|
|
|
|
game.screen.Sync()
|
2023-12-01 22:41:35 +01:00
|
|
|
|
|
|
|
// Player Movement
|
|
|
|
} else if ev.Rune() == 'q' {
|
|
|
|
game.keypresses <- upleft
|
|
|
|
} else if ev.Rune() == 'e' {
|
|
|
|
game.keypresses <- upright
|
|
|
|
} else if ev.Rune() == 'z' {
|
|
|
|
game.keypresses <- downleft
|
|
|
|
} else if ev.Rune() == 'c' {
|
|
|
|
game.keypresses <- downright
|
|
|
|
} else if ev.Key() == tcell.KeyUp || ev.Rune() == 'w' {
|
|
|
|
game.keypresses <- Up
|
|
|
|
} else if ev.Key() == tcell.KeyRight || ev.Rune() == 'd' {
|
2023-12-01 21:57:52 +01:00
|
|
|
game.keypresses <- Right
|
2023-12-01 22:41:35 +01:00
|
|
|
} else if ev.Key() == tcell.KeyDown || ev.Rune() == 's' || ev.Rune() == 'x' {
|
2023-12-01 21:57:52 +01:00
|
|
|
game.keypresses <- Down
|
2023-12-01 22:41:35 +01:00
|
|
|
} else if ev.Key() == tcell.KeyLeft || ev.Rune() == 'a' {
|
|
|
|
game.keypresses <- Left
|
|
|
|
|
|
|
|
// Teleport the player
|
|
|
|
} else if ev.Rune() == 't' {
|
|
|
|
game.keypresses <- teleport
|
2023-12-01 21:57:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit(game *Game) {
|
2023-12-06 19:24:46 +01:00
|
|
|
// game.screen.Clear()
|
2023-12-01 21:57:52 +01:00
|
|
|
maybePanic := recover()
|
|
|
|
game.screen.Fini()
|
|
|
|
if maybePanic != nil {
|
|
|
|
panic(maybePanic)
|
|
|
|
}
|
|
|
|
}
|
2023-12-02 00:19:33 +01:00
|
|
|
|
|
|
|
func (game *Game) randPos() Position {
|
|
|
|
var pos Position
|
|
|
|
|
2023-12-04 01:01:45 +01:00
|
|
|
x := 1 + rand.Intn(77)
|
|
|
|
y := 1 + rand.Intn(22)
|
|
|
|
|
|
|
|
pos.x = x
|
|
|
|
pos.y = y
|
2023-12-03 22:20:44 +01:00
|
|
|
|
2023-12-02 00:19:33 +01:00
|
|
|
return pos
|
|
|
|
}
|