2024-04-11 08:41:58 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer g.Close()
|
|
|
|
|
|
|
|
g.SetManagerFunc(layout)
|
|
|
|
g.Mouse = true
|
|
|
|
g.Cursor = true
|
|
|
|
initKeybindings(g)
|
|
|
|
|
|
|
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
playerX int = 0
|
|
|
|
playerY int = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
func layout(g *gocui.Gui) error {
|
|
|
|
|
|
|
|
maxX, maxY := g.Size()
|
|
|
|
if player, err := g.SetView("player", maxX/2+7+playerX, maxY/2+2-playerY, maxX/2+9+playerX, maxY/2+4-playerY); err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
player.Frame = false
|
|
|
|
player.BgColor = gocui.ColorRed
|
|
|
|
fmt.Fprintln(player, "@")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit(*gocui.Gui, *gocui.View) error {
|
|
|
|
return gocui.ErrQuit
|
|
|
|
}
|
|
|
|
|
|
|
|
func initKeybindings(g *gocui.Gui) error {
|
|
|
|
|
|
|
|
var movementStep int = 1
|
|
|
|
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.SetKeybinding("", 'a', gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
playerX -= movementStep
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2024-04-11 15:19:04 +02:00
|
|
|
log.Panicln(err)
|
2024-04-11 08:41:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.SetKeybinding("", 'd', gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
playerX += movementStep
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2024-04-11 15:19:04 +02:00
|
|
|
log.Panicln(err)
|
2024-04-11 08:41:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.SetKeybinding("", 'w', gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
playerY += movementStep
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2024-04-11 15:19:04 +02:00
|
|
|
log.Panicln(err)
|
2024-04-11 08:41:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.SetKeybinding("", 's', gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
playerY -= movementStep
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2024-04-11 15:19:04 +02:00
|
|
|
log.Panicln(err)
|
2024-04-11 08:41:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|