|
|
|
@ -5,51 +5,136 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
|
"github.com/jordanorelli/astro-domu/internal/math"
|
|
|
|
|
"github.com/jordanorelli/astro-domu/internal/wire"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type inventory struct {
|
|
|
|
|
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 {
|
|
|
|
|
ID int
|
|
|
|
|
name string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type inventoryView struct {
|
|
|
|
|
highlight int
|
|
|
|
|
avatar *wire.Entity // probably but not necessarily the player avatar
|
|
|
|
|
*inventory
|
|
|
|
|
keyHandler func(*tcell.EventKey) change
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (v *inventoryView) handleEvent(e tcell.Event) change {
|
|
|
|
|
if v.keyHandler == nil {
|
|
|
|
|
v.keyHandler = v.selectHandler
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch t := e.(type) {
|
|
|
|
|
case *tcell.EventKey:
|
|
|
|
|
key := t.Key()
|
|
|
|
|
switch key {
|
|
|
|
|
case tcell.KeyEnter:
|
|
|
|
|
case tcell.KeyDown:
|
|
|
|
|
if len(v.items) > 0 {
|
|
|
|
|
v.highlight = (v.highlight + 1) % len(v.items)
|
|
|
|
|
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 {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (v *inventoryView) putdownHandler(e *tcell.EventKey) change {
|
|
|
|
|
key := e.Key()
|
|
|
|
|
|
|
|
|
|
case tcell.KeyUp:
|
|
|
|
|
if len(v.items) > 0 {
|
|
|
|
|
v.highlight = (v.highlight - 1 + len(v.items)) % len(v.items)
|
|
|
|
|
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.KeyESC:
|
|
|
|
|
return changeFn(func(ui *UI) {
|
|
|
|
|
if ui.root == inGameView {
|
|
|
|
|
inGameView.focus(0)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
for i, item := range st.inventory.items {
|
|
|
|
|
line := fmt.Sprintf("- %s", item.name)
|
|
|
|
@ -64,7 +149,10 @@ type openInventory struct{}
|
|
|
|
|
|
|
|
|
|
func (openInventory) exec(ui *UI) {
|
|
|
|
|
if ui.root == inGameView {
|
|
|
|
|
ui.state.detail = &inventoryView{}
|
|
|
|
|
ui.state.detail = &inventoryView{
|
|
|
|
|
avatar: ui.state.avatar,
|
|
|
|
|
inventory: &ui.state.inventory,
|
|
|
|
|
}
|
|
|
|
|
inGameView.focus(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|