You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.2 KiB
76 lines
1.2 KiB
|
3 weeks ago
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
. "lcthw.dev/go/ttarpit/debug"
|
||
|
|
"fmt"
|
||
|
|
"github.com/gdamore/tcell/v2"
|
||
|
|
"github.com/gdamore/tcell/v2/encoding"
|
||
|
|
"lcthw.dev/go/ttarpit/game"
|
||
|
|
)
|
||
|
|
|
||
|
|
type UI struct {
|
||
|
|
Screen tcell.Screen
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ui *UI) DrawText(x int, y int, text string) {
|
||
|
|
for i, cell := range text {
|
||
|
|
ui.Screen.SetContent(x+i, y, cell, nil, tcell.StyleDefault)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ui *UI) Render(data *game.Game) {
|
||
|
|
ui.Screen.Clear()
|
||
|
|
|
||
|
|
ui.DrawText(0, 0, "Welcome to Turing's Tarpit")
|
||
|
|
ui.DrawText(0, 1, fmt.Sprintf("HP: %d", data.HP))
|
||
|
|
ui.DrawText(0, 2, fmt.Sprintf("Errors: %d", data.Errors))
|
||
|
|
|
||
|
|
ui.Screen.Show()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ui *UI) HandleKeys(ev *tcell.EventKey) bool {
|
||
|
|
switch ev.Key() {
|
||
|
|
case tcell.KeyEscape:
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
switch ev.Rune() {
|
||
|
|
case 'q':
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ui *UI) HandleEvents() bool {
|
||
|
|
switch ev := ui.Screen.PollEvent().(type) {
|
||
|
|
case *tcell.EventResize:
|
||
|
|
ui.Screen.Sync()
|
||
|
|
case *tcell.EventKey:
|
||
|
|
return ui.HandleKeys(ev)
|
||
|
|
}
|
||
|
|
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ui *UI) Exit() {
|
||
|
|
ui.Screen.Fini()
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
func MakeUI() *UI {
|
||
|
|
var err error
|
||
|
|
encoding.Register()
|
||
|
|
|
||
|
|
ui := new(UI)
|
||
|
|
|
||
|
|
ui.Screen, err = tcell.NewScreen()
|
||
|
|
if err != nil { Log.Fatal(err) }
|
||
|
|
|
||
|
|
err = ui.Screen.Init()
|
||
|
|
if err != nil { Log.Fatal(err) }
|
||
|
|
|
||
|
|
return ui
|
||
|
|
}
|
||
|
|
|