slowly going crazy now with this putting down a rock thing

master
Jordan Orelli 4 years ago
parent bf05df3292
commit ddde174d9e

@ -36,13 +36,13 @@ func (v *gameView) walkHandler(e *tcell.EventKey) change {
if e.Key() == tcell.KeyRune { if e.Key() == tcell.KeyRune {
switch e.Rune() { switch e.Rune() {
case 'w': case 'w':
return move{0, -1} return move(math.Up)
case 'a': case 'a':
return move{-1, 0} return move(math.Left)
case 's': case 's':
return move{0, 1} return move(math.Down)
case 'd': case 'd':
return move{1, 0} return move(math.Right)
case 'i': case 'i':
return openInventory{} return openInventory{}
case 'l': case 'l':
@ -175,13 +175,10 @@ func (v *gameView) drawHeader(img canvas, st *state) {
func (v *gameView) setFocus(yes bool) { v.inFocus = yes } func (v *gameView) setFocus(yes bool) { v.inFocus = yes }
type move struct { type move math.Vec
x int
y int
}
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))
} }
type lookAt struct { type lookAt struct {
@ -243,7 +240,7 @@ func (p pickup) exec(ui *UI) {
case *sim.Pickedup: case *sim.Pickedup:
ui.misc <- func(ui *UI) { ui.misc <- func(ui *UI) {
ui.state.detail = textView(fmt.Sprintf("you picked up: %s", v.Name)) ui.state.detail = textView(fmt.Sprintf("you picked up: %s", v.Name))
ui.state.inventory.items = append(ui.state.inventory.items, item{name: v.Name}) ui.state.inventory.items = append(ui.state.inventory.items, item{ID: v.ID, name: v.Name})
} }
case wire.Error: case wire.Error:
ui.misc <- func(ui *UI) { ui.misc <- func(ui *UI) {
@ -256,3 +253,29 @@ func (p pickup) exec(ui *UI) {
} }
}() }()
} }
type putdown sim.Putdown
func (p putdown) exec(ui *UI) {
go func() {
res, err := ui.client.Send(sim.Putdown(p))
if err != nil {
ui.state.detail = textView(err.Error())
return
}
switch v := res.Body.(type) {
case *wire.OK:
ui.state.inventory.removeItem(p.ID)
case wire.Error:
ui.misc <- func(ui *UI) {
ui.state.detail = textView(v.Error())
}
default:
ui.misc <- func(ui *UI) {
ui.state.detail = textView(fmt.Sprintf("unexpected putdown response type: %T", res.Body))
}
}
}()
}

@ -5,25 +5,60 @@ import (
"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/wire"
) )
type inventory struct { type inventory struct {
items []item items []item
} }
func (inv *inventory) removeItem(id int) bool {
for i, item := range inv.items {
if item.ID == id {
inv.items = append(inv.items[:i], inv.items[i+1:]...)
return true
}
}
return false
}
type item struct { type item struct {
ID int
name string name string
} }
type inventoryView struct { type inventoryView struct {
highlight int highlight int
avatar *wire.Entity // probably but not necessarily the player avatar
*inventory *inventory
keyHandler func(*tcell.EventKey) change
} }
func (v *inventoryView) handleEvent(e tcell.Event) change { func (v *inventoryView) handleEvent(e tcell.Event) change {
if v.keyHandler == nil {
v.keyHandler = v.selectHandler
}
switch t := e.(type) { switch t := e.(type) {
case *tcell.EventKey: case *tcell.EventKey:
key := t.Key() return v.keyHandler(t)
default:
// ui.Debug("screen saw unhandled event of type %T", e)
}
return nil
}
func (v *inventoryView) selectHandler(e *tcell.EventKey) change {
key := e.Key()
if key == tcell.KeyRune {
switch e.Rune() {
case 'p':
v.keyHandler = v.putdownHandler
return nil
}
}
switch key { switch key {
case tcell.KeyEnter: case tcell.KeyEnter:
case tcell.KeyDown: case tcell.KeyDown:
@ -43,13 +78,63 @@ func (v *inventoryView) handleEvent(e tcell.Event) change {
} }
}) })
} }
return nil
}
func (v *inventoryView) putdownHandler(e *tcell.EventKey) change {
key := e.Key()
if key == tcell.KeyRune {
switch e.Rune() {
case 'w':
return putdown{
ID: v.items[v.highlight].ID,
Location: v.avatar.Position.Add(math.Up),
}
case 'a':
return putdown{
ID: v.items[v.highlight].ID,
Location: v.avatar.Position.Add(math.Left),
}
case 's':
return putdown{
ID: v.items[v.highlight].ID,
Location: v.avatar.Position.Add(math.Down),
}
case 'd':
return putdown{
ID: v.items[v.highlight].ID,
Location: v.avatar.Position.Add(math.Left),
}
}
}
switch key {
case tcell.KeyEnter:
case tcell.KeyDown:
if len(v.items) > 0 {
v.highlight = (v.highlight + 1) % len(v.items)
}
case tcell.KeyUp:
if len(v.items) > 0 {
v.highlight = (v.highlight - 1 + len(v.items)) % len(v.items)
}
case tcell.KeyESC:
return changeFn(func(ui *UI) {
if ui.root == inGameView {
inGameView.focus(0)
}
})
} }
return nil return nil
} }
func (v *inventoryView) draw(img canvas, st *state) { func (v *inventoryView) draw(img canvas, st *state) {
v.inventory = &st.inventory v.avatar = st.avatar
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 { for i, item := range st.inventory.items {
line := fmt.Sprintf("- %s", item.name) line := fmt.Sprintf("- %s", item.name)
@ -64,7 +149,10 @@ type openInventory struct{}
func (openInventory) exec(ui *UI) { func (openInventory) exec(ui *UI) {
if ui.root == inGameView { if ui.root == inGameView {
ui.state.detail = &inventoryView{} ui.state.detail = &inventoryView{
avatar: ui.state.avatar,
inventory: &ui.state.inventory,
}
inGameView.focus(1) inGameView.focus(1)
} }
} }

@ -32,9 +32,9 @@ func (l login) exec(ui *UI) {
ui.state.room = new(wire.Room) ui.state.room = new(wire.Room)
} }
p := welcome.Players[l.name] p := welcome.Players[l.name]
// avi := p.Avatar
room := welcome.Rooms[p.Room] room := welcome.Rooms[p.Room]
// e := room.Entities[p.Avatar] e := room.Entities[p.Avatar]
ui.state.avatar = &e
ui.state.room = &room ui.state.room = &room
ui.root = inGameView ui.root = inGameView

@ -8,6 +8,7 @@ import (
type state struct { type state struct {
playerName string playerName string
avatar *wire.Entity
room *wire.Room room *wire.Room
history []sim.ChatMessage history []sim.ChatMessage
inventory inventory inventory inventory

@ -102,6 +102,9 @@ func (ui *UI) handleNotification(v wire.Value) bool {
switch n := v.(type) { switch n := v.(type) {
case *wire.Entity: case *wire.Entity:
if ui.state.avatar != nil && n.ID == ui.state.avatar.ID {
*ui.state.avatar = *n
}
ui.state.room.Entities[n.ID] = *n ui.state.room.Entities[n.ID] = *n
return true return true
@ -113,6 +116,13 @@ func (ui *UI) handleNotification(v wire.Value) bool {
ui.state.room.Name = n.RoomName ui.state.room.Name = n.RoomName
ui.state.room.Rect = n.RoomSize ui.state.room.Rect = n.RoomSize
ui.state.room.Entities = n.Entities ui.state.room.Entities = n.Entities
if ui.state.avatar != nil {
e, ok := n.Entities[ui.state.avatar.ID]
if ok {
*ui.state.avatar = e
}
}
return true return true
case *wire.Delta: case *wire.Delta:
@ -120,6 +130,13 @@ func (ui *UI) handleNotification(v wire.Value) bool {
ui.state.room.Rect = *n.RoomSize ui.state.room.Rect = *n.RoomSize
} }
if ui.state.avatar != nil {
id := ui.state.avatar.ID
if ep, ok := n.Entities[id]; ok {
*ui.state.avatar = *ep
}
}
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 {

@ -5,6 +5,14 @@ import (
"fmt" "fmt"
) )
var (
Origin = Vec{0, 0}
Up = Vec{0, -1} // w
Left = Vec{-1, 0} // a
Down = Vec{0, 1} // s
Right = Vec{1, 0} // d
)
type Vec struct { type Vec struct {
X int X int
Y int Y int

@ -295,12 +295,13 @@ func (pu *Pickup) exec(w *world, r *room, pl *player, seq int) result {
} }
nextTile.here = nextTile.here[0:0] nextTile.here = nextTile.here[0:0]
pl.inventory = append(pl.inventory, e) pl.inventory = append(pl.inventory, e)
return result{reply: Pickedup{Name: e.name}} return result{reply: Pickedup{ID: e.ID, Name: e.name}}
} }
return result{reply: wire.Errorf("nothing here")} return result{reply: wire.Errorf("nothing here")}
} }
type Pickedup struct { type Pickedup struct {
ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} }

Loading…
Cancel
Save