130 lines
2.2 KiB
Go
130 lines
2.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
|
||
|
"github.com/gdamore/tcell/v2"
|
||
|
)
|
||
|
|
||
|
func gameinit() {
|
||
|
game, err := initialize()
|
||
|
if err != nil {
|
||
|
log.Fatalln("Initilization error: ", err)
|
||
|
}
|
||
|
go gameDirector(&game)
|
||
|
keyboardProcessor(&game)
|
||
|
|
||
|
// Quit the screen
|
||
|
quit(&game)
|
||
|
|
||
|
// Give closure
|
||
|
fmt.Println("You get rekt lol.")
|
||
|
|
||
|
}
|
||
|
|
||
|
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,
|
||
|
},
|
||
|
moves: 0,
|
||
|
teleports: 0,
|
||
|
},
|
||
|
// robots: []Robot, // I need this maybe
|
||
|
// trash: []Position, // I will need this
|
||
|
gameover: 0,
|
||
|
}
|
||
|
|
||
|
return game, err
|
||
|
}
|
||
|
|
||
|
func gameDirector(game *Game) {
|
||
|
defer quit(game)
|
||
|
|
||
|
for {
|
||
|
|
||
|
// Clean the screen
|
||
|
game.screen.Clear()
|
||
|
|
||
|
// Draw starter conditions
|
||
|
drawBox(game)
|
||
|
drawPlayer(game)
|
||
|
drawCoords(game)
|
||
|
|
||
|
// Draw robots??
|
||
|
|
||
|
// Draw the screen
|
||
|
game.screen.Show()
|
||
|
|
||
|
// Process input
|
||
|
movePlayer(game, <-game.keypresses)
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func keyboardProcessor(game *Game) {
|
||
|
defer close(game.keypresses)
|
||
|
|
||
|
for {
|
||
|
|
||
|
// 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:
|
||
|
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
|
||
|
return
|
||
|
} else if ev.Rune() == 'q' || ev.Rune() == 'Q' {
|
||
|
return
|
||
|
} else if ev.Key() == tcell.KeyCtrlL {
|
||
|
game.screen.Sync()
|
||
|
} else if ev.Rune() == 'C' || ev.Rune() == 'c' {
|
||
|
game.screen.Clear()
|
||
|
} else if ev.Key() == tcell.KeyLeft {
|
||
|
game.keypresses <- Left
|
||
|
} else if ev.Key() == tcell.KeyRight {
|
||
|
game.keypresses <- Right
|
||
|
} else if ev.Key() == tcell.KeyDown {
|
||
|
game.keypresses <- Down
|
||
|
} else if ev.Key() == tcell.KeyUp {
|
||
|
game.keypresses <- Up
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if game.gameover != 0 {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func quit(game *Game) {
|
||
|
maybePanic := recover()
|
||
|
game.screen.Fini()
|
||
|
if maybePanic != nil {
|
||
|
panic(maybePanic)
|
||
|
}
|
||
|
}
|