can pick up items (sorta)

master
Jordan Orelli 4 years ago
parent 0346e619e4
commit 068eb26d38

@ -1,6 +1,8 @@
package app package app
import ( import (
"fmt"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math" "github.com/jordanorelli/astro-domu/internal/math"
"github.com/jordanorelli/astro-domu/internal/sim" "github.com/jordanorelli/astro-domu/internal/sim"
@ -45,6 +47,9 @@ func (v *gameView) walkHandler(e *tcell.EventKey) change {
case 'l': case 'l':
v.keyHandler = v.lookHandler v.keyHandler = v.lookHandler
v.statusLine = "(look)" v.statusLine = "(look)"
case 'p':
v.keyHandler = v.pickupHandler
v.statusLine = "(pickup)"
} }
} }
return nil return nil
@ -80,6 +85,36 @@ func (v *gameView) lookHandler(e *tcell.EventKey) change {
return nil 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) { func (v *gameView) draw(img canvas, st *state) {
fill(img, tcell.StyleDefault.Background(tcell.NewRGBColor(0, 0, 12))) fill(img, tcell.StyleDefault.Background(tcell.NewRGBColor(0, 0, 12)))
v.drawHeader(img, st) 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) 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})
}
}()
}

@ -1,6 +1,8 @@
package app package app
import ( import (
"fmt"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/jordanorelli/astro-domu/internal/math" "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) { func (v *inventoryView) draw(img canvas, st *state) {
writeString(img, "Inventory", math.Vec{0, 0}, tcell.StyleDefault) 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{} type openInventory struct{}

@ -39,3 +39,11 @@ func (t *textInput) handleEvent(e tcell.Event) change {
func (t *textInput) draw(img canvas, _ *state) { func (t *textInput) draw(img canvas, _ *state) {
writeString(img, t.prompt+t.entered, math.Vec{0, 0}, tcell.StyleDefault) 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)
}

@ -248,10 +248,34 @@ type LookItem struct {
Name string `json:"name"` 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 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(Look) })
wire.Register(func() wire.Value { return new(LookAt) }) wire.Register(func() wire.Value { return new(LookAt) })
wire.Register(func() wire.Value { return new(Pickup) })
wire.Register(func() wire.Value { return new(Pickedup) })
} }

Loading…
Cancel
Save