diff --git a/internal/app/game_view.go b/internal/app/game_view.go index d78ddbd..e1eba8e 100644 --- a/internal/app/game_view.go +++ b/internal/app/game_view.go @@ -1,6 +1,8 @@ package app import ( + "fmt" + "github.com/gdamore/tcell/v2" "github.com/jordanorelli/astro-domu/internal/math" "github.com/jordanorelli/astro-domu/internal/sim" @@ -45,6 +47,9 @@ func (v *gameView) walkHandler(e *tcell.EventKey) change { case 'l': v.keyHandler = v.lookHandler v.statusLine = "(look)" + case 'p': + v.keyHandler = v.pickupHandler + v.statusLine = "(pickup)" } } return nil @@ -80,6 +85,36 @@ func (v *gameView) lookHandler(e *tcell.EventKey) change { return nil } +func (v *gameView) pickupHandler(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 &pickup{x: 0, y: -1} + case 'a': + v.keyHandler = v.walkHandler + v.statusLine = "(walk)" + return &pickup{x: -1, y: 0} + case 's': + v.keyHandler = v.walkHandler + v.statusLine = "(walk)" + return &pickup{x: 0, y: 1} + case 'd': + v.keyHandler = v.walkHandler + v.statusLine = "(walk)" + return &pickup{x: 1, y: 0} + } + } + return nil +} + func (v *gameView) draw(img canvas, st *state) { fill(img, tcell.StyleDefault.Background(tcell.NewRGBColor(0, 0, 12))) v.drawHeader(img, st) @@ -181,3 +216,28 @@ func (l *lookAt) draw(img canvas, st *state) { writeString(img, item.Name, math.Vec{0, i}, tcell.StyleDefault) } } + +type pickup struct { + x, y int +} + +func (p pickup) exec(ui *UI) { + go func() { + res, err := ui.client.Send(sim.Pickup{p.x, p.y}) + if err != nil { + ui.Error("look error: %v", err) + return + } + + pickedup, ok := res.Body.(*sim.Pickedup) + if !ok { + ui.Error("pickup response is not pickedup: %v", res.Body) + return + } + + ui.misc <- func(ui *UI) { + ui.state.detail = textView(fmt.Sprintf("you picked up: %s", pickedup.Name)) + ui.state.inventory.items = append(ui.state.inventory.items, item{name: pickedup.Name}) + } + }() +} diff --git a/internal/app/inventory.go b/internal/app/inventory.go index 87f07f5..66f6460 100644 --- a/internal/app/inventory.go +++ b/internal/app/inventory.go @@ -1,6 +1,8 @@ package app import ( + "fmt" + "github.com/gdamore/tcell/v2" "github.com/jordanorelli/astro-domu/internal/math" ) @@ -22,6 +24,10 @@ func (v *inventoryView) handleEvent(e tcell.Event) change { func (v *inventoryView) draw(img canvas, st *state) { writeString(img, "Inventory", math.Vec{0, 0}, tcell.StyleDefault) + for i, item := range st.inventory.items { + line := fmt.Sprintf("- %s", item.name) + writeString(img, line, math.Vec{0, i + 2}, tcell.StyleDefault) + } } type openInventory struct{} diff --git a/internal/app/text.go b/internal/app/text.go index f440748..9a13705 100644 --- a/internal/app/text.go +++ b/internal/app/text.go @@ -39,3 +39,11 @@ func (t *textInput) handleEvent(e tcell.Event) change { func (t *textInput) draw(img canvas, _ *state) { writeString(img, t.prompt+t.entered, math.Vec{0, 0}, tcell.StyleDefault) } + +type textView string + +func (textView) handleEvent(tcell.Event) change { return nil } + +func (t textView) draw(img canvas, _ *state) { + writeString(img, string(t), math.Vec{0, 0}, tcell.StyleDefault) +} diff --git a/internal/sim/player.go b/internal/sim/player.go index c4061ad..11979ea 100644 --- a/internal/sim/player.go +++ b/internal/sim/player.go @@ -248,10 +248,34 @@ type LookItem struct { Name string `json:"name"` } +type Pickup math.Vec + +func (Pickup) NetTag() string { return "pickup" } + +func (pu *Pickup) exec(w *world, r *room, pl *player, seq int) result { + pos := pl.avatar.Position + target := pos.Add(math.Vec(*pu)) + nextTile := r.getTile(target) + if len(nextTile.here) == 1 { + e := nextTile.here[0] + nextTile.here = nextTile.here[0:0] + return result{reply: Pickedup{Name: e.name}} + } + return result{reply: wire.Errorf("nothing here")} +} + +type Pickedup struct { + Name string `json:"name"` +} + +func (p Pickedup) NetTag() string { return "pickedup" } + var lastEntityID = 0 func init() { wire.Register(func() wire.Value { return new(Move) }) wire.Register(func() wire.Value { return new(Look) }) wire.Register(func() wire.Value { return new(LookAt) }) + wire.Register(func() wire.Value { return new(Pickup) }) + wire.Register(func() wire.Value { return new(Pickedup) }) }