getting look data now

master
Jordan Orelli 4 years ago
parent fa732377c7
commit f132a11929

@ -2,6 +2,7 @@ package app
import ( import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math"
"github.com/jordanorelli/astro-domu/internal/sim" "github.com/jordanorelli/astro-domu/internal/sim"
"github.com/jordanorelli/blammo" "github.com/jordanorelli/blammo"
) )
@ -9,14 +10,28 @@ import (
type gameView struct { type gameView struct {
*blammo.Log *blammo.Log
inFocus bool inFocus bool
keyHandler func(*tcell.EventKey) change
statusLine string
} }
func (v *gameView) handleEvent(e tcell.Event) change { func (v *gameView) handleEvent(e tcell.Event) change {
if v.keyHandler == nil {
v.keyHandler = v.walkHandler
v.statusLine = "(walk)"
}
switch t := e.(type) { switch t := e.(type) {
case *tcell.EventKey: case *tcell.EventKey:
key := t.Key() return v.keyHandler(t)
if key == tcell.KeyRune { default:
switch t.Rune() { // ui.Debug("screen saw unhandled event of type %T", e)
}
return nil
}
func (v *gameView) walkHandler(e *tcell.EventKey) change {
if e.Key() == tcell.KeyRune {
switch e.Rune() {
case 'w': case 'w':
return move{0, -1} return move{0, -1}
case 'a': case 'a':
@ -25,10 +40,40 @@ func (v *gameView) handleEvent(e tcell.Event) change {
return move{0, 1} return move{0, 1}
case 'd': case 'd':
return move{1, 0} return move{1, 0}
case 'l':
v.keyHandler = v.lookHandler
v.statusLine = "(look)"
} }
} }
default: return nil
// ui.Debug("screen saw unhandled event of type %T", e) }
func (v *gameView) lookHandler(e *tcell.EventKey) change {
if e.Key() == tcell.KeyESC {
v.keyHandler = v.walkHandler
v.statusLine = "(walk)"
return nil
}
if e.Key() == tcell.KeyRune {
switch e.Rune() {
case 'w':
v.keyHandler = v.walkHandler
v.statusLine = "(walk)"
return lookAt{0, -1}
case 'a':
v.keyHandler = v.walkHandler
v.statusLine = "(walk)"
return lookAt{-1, 0}
case 's':
v.keyHandler = v.walkHandler
v.statusLine = "(walk)"
return lookAt{0, 1}
case 'd':
v.keyHandler = v.walkHandler
v.statusLine = "(walk)"
return lookAt{1, 0}
}
} }
return nil return nil
} }
@ -59,6 +104,7 @@ func (v *gameView) draw(img canvas, st *state) {
pos := e.Position pos := e.Position
img.setTile(pos.X+1, pos.Y+2, tile{r: e.Glyph, style: tcell.StyleDefault}) img.setTile(pos.X+1, pos.Y+2, tile{r: e.Glyph, style: tcell.StyleDefault})
} }
writeString(img, v.statusLine, math.Vec{0, img.bounds().Height - 1}, tcell.StyleDefault)
} }
func (v *gameView) drawHeader(img canvas, st *state) { func (v *gameView) drawHeader(img canvas, st *state) {
@ -89,3 +135,26 @@ type move struct {
func (m move) exec(ui *UI) { func (m move) exec(ui *UI) {
go ui.client.Send(sim.Move{m.x, m.y}) go ui.client.Send(sim.Move{m.x, m.y})
} }
type lookAt struct {
x int
y int
}
func (l lookAt) exec(ui *UI) {
go func() {
res, err := ui.client.Send(sim.LookAt{l.x, l.y})
if err != nil {
ui.Error("look error: %v", err)
return
}
look, ok := res.Body.(*sim.Look)
if !ok {
ui.Error("look response is not look: %v", res.Body)
return
}
ui.Info("look: %v", look)
}()
}

@ -16,6 +16,7 @@ type UI struct {
screen tcell.Screen screen tcell.Screen
client *wire.Client client *wire.Client
notifications <-chan wire.Response notifications <-chan wire.Response
misc chan func(*UI)
state state state state
root view root view
@ -24,6 +25,7 @@ type UI struct {
func (ui *UI) Run() { func (ui *UI) Run() {
ui.setupTerminal() ui.setupTerminal()
defer ui.clearTerminal() defer ui.clearTerminal()
ui.misc = make(chan func(*UI))
ui.root = mainMenu ui.root = mainMenu
@ -131,6 +133,10 @@ func (ui *UI) handleNotification(v wire.Value) bool {
ui.state.history = append(ui.state.history, *n) ui.state.history = append(ui.state.history, *n)
return true return true
case *sim.Look:
ui.Info("got look back: %v", n)
return true
default: default:
ui.Info("ignoring notification: %v", n) ui.Info("ignoring notification: %v", n)
return false return false

@ -10,6 +10,8 @@ type entity struct {
ID int `json:"id"` ID int `json:"id"`
Position math.Vec `json:"pos"` Position math.Vec `json:"pos"`
Glyph rune `json:"glyph"` Glyph rune `json:"glyph"`
name string
description string
solid bool `json:"-"` solid bool `json:"-"`
overlapped map[int]*entity overlapped map[int]*entity
behavior behavior

@ -221,8 +221,37 @@ func (m *Move) exec(w *world, r *room, p *player, seq int) result {
return result{reply: wire.OK{}} return result{reply: wire.OK{}}
} }
type LookAt math.Vec
func (LookAt) NetTag() string { return "look-at" }
func (l *LookAt) exec(w *world, r *room, p *player, seq int) result {
pos := p.avatar.Position
target := pos.Add(math.Vec(*l))
nextTile := r.getTile(target)
p.Info("looked at: %v", nextTile)
look := &Look{Here: make([]LookItem, 0, len(nextTile.here))}
for _, e := range nextTile.here {
look.Here = append(look.Here, LookItem{Name: e.name})
}
return result{reply: look}
}
type Look struct {
Here []LookItem `json:"here"`
}
func (l Look) NetTag() string { return "look" }
type LookItem struct {
Name string `json:"name"`
}
var lastEntityID = 0 var lastEntityID = 0
func init() { func init() {
wire.Register(func() wire.Value { return new(Move) }) wire.Register(func() wire.Value { return new(Move) })
wire.Register(func() wire.Value { return new(Look) })
wire.Register(func() wire.Value { return new(LookAt) })
} }

@ -42,29 +42,31 @@ func newWorld(log *blammo.Log) *world {
foyer.addEntity(&entity{ foyer.addEntity(&entity{
ID: -1, ID: -1,
Position: math.Vec{5, 4}, Position: math.Vec{5, 4},
Glyph: 'd', Glyph: 'o',
solid: true, solid: true,
name: "a rock",
behavior: doNothing{}, behavior: doNothing{},
}) })
foyer.addEntity(&entity{ // foyer.addEntity(&entity{
ID: -2, // ID: -2,
Position: math.Vec{9, 0}, // Position: math.Vec{9, 0},
Glyph: '+', // Glyph: '+',
behavior: doNothing{}, // behavior: doNothing{},
}) // })
foyer.addEntity(&entity{ // foyer.addEntity(&entity{
ID: -3, // ID: -3,
Position: math.Vec{9, 1}, // Position: math.Vec{9, 1},
Glyph: '-', // Glyph: '-',
behavior: doNothing{}, // behavior: doNothing{},
}) // })
foyer.addEntity(&entity{ foyer.addEntity(&entity{
ID: -4, ID: -4,
Position: math.Vec{9, 5}, Position: math.Vec{9, 5},
Glyph: '◇', Glyph: '◇',
name: "Door to Hall",
behavior: &door{ behavior: &door{
Log: log.Child("door"), Log: log.Child("door"),
to: "hall", to: "hall",
@ -84,6 +86,7 @@ func newWorld(log *blammo.Log) *world {
ID: -5, ID: -5,
Position: math.Vec{0, 2}, Position: math.Vec{0, 2},
Glyph: '◇', Glyph: '◇',
name: "Door to Foyer",
behavior: &door{ behavior: &door{
Log: log.Child("door"), Log: log.Child("door"),
to: "foyer", to: "foyer",
@ -103,6 +106,7 @@ func newWorld(log *blammo.Log) *world {
ID: -6, ID: -6,
Position: math.Vec{9, 7}, Position: math.Vec{9, 7},
Glyph: '◇', Glyph: '◇',
name: "Door to Kitchen",
behavior: &door{ behavior: &door{
Log: log.Child("door"), Log: log.Child("door"),
to: "kitchen", to: "kitchen",
@ -114,6 +118,7 @@ func newWorld(log *blammo.Log) *world {
ID: -7, ID: -7,
Position: math.Vec{0, 2}, Position: math.Vec{0, 2},
Glyph: '◇', Glyph: '◇',
name: "Door to Foyer",
behavior: &door{ behavior: &door{
Log: log.Child("door"), Log: log.Child("door"),
to: "foyer", to: "foyer",

Loading…
Cancel
Save