|
|
@ -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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|