2023-12-01 21:57:52 +01:00
|
|
|
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
|
2023-12-01 22:41:35 +01:00
|
|
|
game.drawBox(game)
|
|
|
|
game.drawPlayer(game)
|
|
|
|
game.drawCoords(game)
|
2023-12-01 21:57:52 +01:00
|
|
|
|
|
|
|
// Draw robots??
|
|
|
|
|
|
|
|
// Draw the screen
|
|
|
|
game.screen.Show()
|
|
|
|
|
|
|
|
// Process input
|
2023-12-01 22:41:35 +01:00
|
|
|
game.movePlayer(game, <-game.keypresses)
|
2023-12-01 21:57:52 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if game.gameover != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit(game *Game) {
|
|
|
|
maybePanic := recover()
|
|
|
|
game.screen.Fini()
|
|
|
|
if maybePanic != nil {
|
|
|
|
panic(maybePanic)
|
|
|
|
}
|
|
|
|
}
|