|
|
@ -1,7 +1,7 @@
|
|
|
|
package app
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
"github.com/jordanorelli/astro-domu/internal/exit"
|
|
|
|
"github.com/jordanorelli/astro-domu/internal/exit"
|
|
|
@ -13,87 +13,58 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
type UI struct {
|
|
|
|
type UI struct {
|
|
|
|
*blammo.Log
|
|
|
|
*blammo.Log
|
|
|
|
PlayerName string
|
|
|
|
screen tcell.Screen
|
|
|
|
screen tcell.Screen
|
|
|
|
client *wire.Client
|
|
|
|
room *wire.Room
|
|
|
|
notifications <-chan wire.Response
|
|
|
|
client *wire.Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
statusBar *statusBar
|
|
|
|
state state
|
|
|
|
gameView *gameView
|
|
|
|
root *node
|
|
|
|
testList *menuList
|
|
|
|
|
|
|
|
chatView *chatView
|
|
|
|
|
|
|
|
focussed view
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ui *UI) Run() {
|
|
|
|
func (ui *UI) Run() {
|
|
|
|
ui.setupTerminal()
|
|
|
|
ui.setupTerminal()
|
|
|
|
defer ui.clearTerminal()
|
|
|
|
defer ui.clearTerminal()
|
|
|
|
|
|
|
|
|
|
|
|
ui.room = new(wire.Room)
|
|
|
|
ui.root = mainMenu
|
|
|
|
|
|
|
|
|
|
|
|
if err := ui.connect(); err != nil {
|
|
|
|
input := make(chan tcell.Event)
|
|
|
|
return
|
|
|
|
go ui.pollInput(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res, err := ui.client.Send(wire.Login{Name: ui.PlayerName})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
ui.Error("login error: %v", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
welcome, ok := res.Body.(*wire.Welcome)
|
|
|
|
tick := time.Tick(time.Second / time.Duration(30))
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
ui.Error("unexpected initial message of type %t", res.Body)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Info("welcome: %v", welcome)
|
|
|
|
|
|
|
|
meta := welcome.Players[ui.PlayerName]
|
|
|
|
|
|
|
|
room := welcome.Rooms[meta.Room]
|
|
|
|
|
|
|
|
ui.room = &room
|
|
|
|
|
|
|
|
ui.gameView = &gameView{
|
|
|
|
|
|
|
|
Log: ui.Child("game-view"),
|
|
|
|
|
|
|
|
room: &room,
|
|
|
|
|
|
|
|
me: &wire.Entity{
|
|
|
|
|
|
|
|
ID: meta.Avatar,
|
|
|
|
|
|
|
|
Glyph: room.Entities[meta.Avatar].Glyph,
|
|
|
|
|
|
|
|
Position: room.Entities[meta.Avatar].Position,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.chatView = &chatView{
|
|
|
|
|
|
|
|
Log: ui.Child("chat-view"),
|
|
|
|
|
|
|
|
history: make([]sim.ChatMessage, 0, 32),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.statusBar = &statusBar{}
|
|
|
|
|
|
|
|
ui.testList = &menuList{
|
|
|
|
|
|
|
|
Log: ui.Child("menu-list"),
|
|
|
|
|
|
|
|
choices: []string{"apple", "banana", "orange"},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.focussed = ui.gameView
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ui.Info("running ui")
|
|
|
|
width, height := ui.screen.Size()
|
|
|
|
if ui.handleUserInput() {
|
|
|
|
b := newBuffer(width, height)
|
|
|
|
ui.Info("user requested close")
|
|
|
|
|
|
|
|
ui.Info("closing client")
|
|
|
|
|
|
|
|
ui.client.Close()
|
|
|
|
|
|
|
|
ui.Info("client closed")
|
|
|
|
|
|
|
|
ui.Info("finalizing screen")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Info("run loop done, shutting down")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ui *UI) connect() error {
|
|
|
|
// b, prev := newBuffer(width, height), newBuffer(width, height)
|
|
|
|
ui.client = &wire.Client{
|
|
|
|
|
|
|
|
Log: ui.Child("client"),
|
|
|
|
|
|
|
|
Host: "cdm.jordanorelli.com",
|
|
|
|
|
|
|
|
Port: 12805,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c, err := ui.client.Dial()
|
|
|
|
for {
|
|
|
|
if err != nil {
|
|
|
|
notify := ui.notifications
|
|
|
|
return fmt.Errorf("unable to dial server: %v", err)
|
|
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case e := <-input:
|
|
|
|
|
|
|
|
switch v := e.(type) {
|
|
|
|
|
|
|
|
case *tcell.EventKey:
|
|
|
|
|
|
|
|
key := v.Key()
|
|
|
|
|
|
|
|
if key == tcell.KeyCtrlC {
|
|
|
|
|
|
|
|
ui.Info("saw ctrl+c keyboard input, shutting down")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Info("input event: %v", e)
|
|
|
|
|
|
|
|
wants := ui.root.handleEvent(e)
|
|
|
|
|
|
|
|
if wants != nil {
|
|
|
|
|
|
|
|
wants.exec(ui)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-tick:
|
|
|
|
|
|
|
|
b.clear()
|
|
|
|
|
|
|
|
ui.root.draw(b, &ui.state)
|
|
|
|
|
|
|
|
b.blit(ui.screen, math.Vec{0, 0})
|
|
|
|
|
|
|
|
ui.screen.Show()
|
|
|
|
|
|
|
|
case n := <-notify:
|
|
|
|
|
|
|
|
ui.Info("notification: %v", n)
|
|
|
|
|
|
|
|
ui.handleNotification(n.Body)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go ui.handleNotifications(c)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ui *UI) setupTerminal() {
|
|
|
|
func (ui *UI) setupTerminal() {
|
|
|
@ -116,63 +87,45 @@ func (ui *UI) setupTerminal() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ui *UI) clearTerminal() {
|
|
|
|
func (ui *UI) clearTerminal() {
|
|
|
|
ui.statusBar.clearCount++
|
|
|
|
|
|
|
|
ui.screen.Clear()
|
|
|
|
ui.screen.Clear()
|
|
|
|
ui.screen.Fini()
|
|
|
|
ui.screen.Fini()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ui *UI) handleNotifications(c <-chan wire.Response) {
|
|
|
|
|
|
|
|
for n := range c {
|
|
|
|
|
|
|
|
ui.statusBar.msgCount++
|
|
|
|
|
|
|
|
if ui.handleNotification(n.Body) {
|
|
|
|
|
|
|
|
if ui.gameView != nil {
|
|
|
|
|
|
|
|
ui.render()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Info("notifications channel is closed so we must be done")
|
|
|
|
|
|
|
|
ui.Info("clearing and finalizing screen from notifications goroutine")
|
|
|
|
|
|
|
|
ui.statusBar.clearCount++
|
|
|
|
|
|
|
|
ui.screen.Clear()
|
|
|
|
|
|
|
|
ui.screen.Fini()
|
|
|
|
|
|
|
|
ui.Info("screen finalized")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ui *UI) handleNotification(v wire.Value) bool {
|
|
|
|
func (ui *UI) handleNotification(v wire.Value) bool {
|
|
|
|
switch n := v.(type) {
|
|
|
|
switch n := v.(type) {
|
|
|
|
|
|
|
|
|
|
|
|
case *wire.Entity:
|
|
|
|
case *wire.Entity:
|
|
|
|
ui.room.Entities[n.ID] = *n
|
|
|
|
ui.state.room.Entities[n.ID] = *n
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
case *wire.Frame:
|
|
|
|
case *wire.Frame:
|
|
|
|
if ui.room == nil {
|
|
|
|
if ui.state.room == nil {
|
|
|
|
ui.room = new(wire.Room)
|
|
|
|
ui.state.room = new(wire.Room)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ui.room.Name = n.RoomName
|
|
|
|
ui.state.room.Name = n.RoomName
|
|
|
|
ui.room.Rect = n.RoomSize
|
|
|
|
ui.state.room.Rect = n.RoomSize
|
|
|
|
ui.room.Entities = n.Entities
|
|
|
|
ui.state.room.Entities = n.Entities
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
case *wire.Delta:
|
|
|
|
case *wire.Delta:
|
|
|
|
if n.RoomSize != nil {
|
|
|
|
if n.RoomSize != nil {
|
|
|
|
ui.room.Rect = *n.RoomSize
|
|
|
|
ui.state.room.Rect = *n.RoomSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(n.Entities) > 0 {
|
|
|
|
if len(n.Entities) > 0 {
|
|
|
|
for id, e := range n.Entities {
|
|
|
|
for id, e := range n.Entities {
|
|
|
|
if e != nil {
|
|
|
|
if e != nil {
|
|
|
|
ui.room.Entities[id] = *e
|
|
|
|
ui.state.room.Entities[id] = *e
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
delete(ui.room.Entities, id)
|
|
|
|
delete(ui.state.room.Entities, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
case *sim.ChatMessage:
|
|
|
|
case *sim.ChatMessage:
|
|
|
|
ui.chatView.history = append(ui.chatView.history, *n)
|
|
|
|
// ui.chatView.history = append(ui.chatView.history, *n)
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
default:
|
|
|
@ -198,79 +151,47 @@ func (ui *UI) writeString(x, y int, s string, style tcell.Style) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ui *UI) handleUserInput() bool {
|
|
|
|
func (ui *UI) pollInput(c chan tcell.Event) {
|
|
|
|
ui.statusBar.clearCount++
|
|
|
|
defer close(c)
|
|
|
|
ui.screen.Clear()
|
|
|
|
|
|
|
|
ui.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
e := ui.screen.PollEvent()
|
|
|
|
e := ui.screen.PollEvent()
|
|
|
|
if e == nil {
|
|
|
|
if e == nil {
|
|
|
|
ui.Info("run loop sees nil event, breaking out")
|
|
|
|
ui.Info("run loop sees nil event, breaking out")
|
|
|
|
// someone else shut us down, so return false
|
|
|
|
return
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch v := e.(type) {
|
|
|
|
|
|
|
|
case *tcell.EventKey:
|
|
|
|
|
|
|
|
key := v.Key()
|
|
|
|
|
|
|
|
if key == tcell.KeyCtrlC {
|
|
|
|
|
|
|
|
ui.Info("saw ctrl+c keyboard input, shutting down")
|
|
|
|
|
|
|
|
// we want to shut things down
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == tcell.KeyTab {
|
|
|
|
|
|
|
|
ui.Info("saw tab from keyboard input, switching focussed view")
|
|
|
|
|
|
|
|
ui.focussed.setFocus(false)
|
|
|
|
|
|
|
|
switch ui.focussed {
|
|
|
|
|
|
|
|
case ui.gameView:
|
|
|
|
|
|
|
|
ui.focussed = ui.chatView
|
|
|
|
|
|
|
|
case ui.chatView:
|
|
|
|
|
|
|
|
ui.focussed = ui.testList
|
|
|
|
|
|
|
|
case ui.testList:
|
|
|
|
|
|
|
|
ui.focussed = ui.gameView
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.focussed.setFocus(true)
|
|
|
|
|
|
|
|
goto HANDLED
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c <- e
|
|
|
|
ui.focussed.handleEvent(ui, e)
|
|
|
|
|
|
|
|
ui.statusBar.clearCount++
|
|
|
|
|
|
|
|
ui.screen.Clear()
|
|
|
|
|
|
|
|
ui.render()
|
|
|
|
|
|
|
|
ui.statusBar.showCount++
|
|
|
|
|
|
|
|
ui.screen.Show()
|
|
|
|
|
|
|
|
HANDLED:
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ui *UI) render() {
|
|
|
|
var mainMenu = &node{
|
|
|
|
width, height := ui.screen.Size()
|
|
|
|
view: &menuList{
|
|
|
|
|
|
|
|
choices: []menuItem{
|
|
|
|
{
|
|
|
|
menuItem{
|
|
|
|
b := newBuffer(width, 1)
|
|
|
|
name: "join",
|
|
|
|
ui.statusBar.draw(b)
|
|
|
|
onSelect: changeFn(func(ui *UI) {
|
|
|
|
b.blit(ui.screen, math.Vec{0, 0})
|
|
|
|
ui.root = joinForm
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
},
|
|
|
|
gameViewHeight := math.Max((height-1)/2, 8)
|
|
|
|
menuItem{
|
|
|
|
{
|
|
|
|
name: "exit",
|
|
|
|
b := newBuffer(width/2, gameViewHeight)
|
|
|
|
onSelect: changeFn(func(ui *UI) {
|
|
|
|
ui.gameView.draw(b)
|
|
|
|
panic("this is bad programming")
|
|
|
|
b.blit(ui.screen, math.Vec{0, 1})
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
},
|
|
|
|
b := newBuffer(width/2, gameViewHeight)
|
|
|
|
},
|
|
|
|
ui.testList.draw(b)
|
|
|
|
}
|
|
|
|
b.blit(ui.screen, math.Vec{width / 2, 1})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
b := newBuffer(width, height-gameViewHeight-1)
|
|
|
|
|
|
|
|
ui.chatView.draw(b)
|
|
|
|
|
|
|
|
b.blit(ui.screen, math.Vec{0, gameViewHeight + 1})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ui.statusBar.showCount++
|
|
|
|
var joinForm = &node{
|
|
|
|
ui.screen.Show()
|
|
|
|
view: &form{
|
|
|
|
|
|
|
|
fields: []textField{
|
|
|
|
|
|
|
|
textField{
|
|
|
|
|
|
|
|
label: "What is your name?",
|
|
|
|
|
|
|
|
textInput: textInput{
|
|
|
|
|
|
|
|
prompt: "> ",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|