2024-04-27 22:32:05 +03:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2024-05-06 20:43:35 +03:00
|
|
|
"mvvasilev/last_light/engine"
|
2024-04-27 22:32:05 +03:00
|
|
|
"mvvasilev/last_light/game/model"
|
2024-05-06 20:43:35 +03:00
|
|
|
"mvvasilev/last_light/game/ui/menu"
|
2024-04-27 22:32:05 +03:00
|
|
|
|
|
|
|
"github.com/gdamore/tcell/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InventoryScreenState struct {
|
2024-05-03 13:46:32 +03:00
|
|
|
prevState PausableState
|
2024-04-27 22:32:05 +03:00
|
|
|
exitMenu bool
|
|
|
|
|
2024-05-06 20:43:35 +03:00
|
|
|
inventoryMenu *menu.PlayerInventoryMenu
|
2024-05-06 21:47:20 +03:00
|
|
|
selectedInventorySlot engine.Position
|
2024-04-27 22:32:05 +03:00
|
|
|
|
|
|
|
player *model.Player
|
2024-05-03 13:46:32 +03:00
|
|
|
|
|
|
|
moveInventorySlotDirection model.Direction
|
|
|
|
dropSelectedInventorySlot bool
|
2024-04-27 22:32:05 +03:00
|
|
|
}
|
|
|
|
|
2024-05-03 13:46:32 +03:00
|
|
|
func CreateInventoryScreenState(player *model.Player, prevState PausableState) *InventoryScreenState {
|
2024-04-27 22:32:05 +03:00
|
|
|
iss := new(InventoryScreenState)
|
|
|
|
|
|
|
|
iss.prevState = prevState
|
|
|
|
iss.player = player
|
2024-05-06 21:47:20 +03:00
|
|
|
iss.selectedInventorySlot = engine.PositionAt(0, 0)
|
2024-05-06 20:43:35 +03:00
|
|
|
iss.exitMenu = false
|
|
|
|
iss.inventoryMenu = menu.CreatePlayerInventoryMenu(43, 0, player.Inventory(), tcell.StyleDefault, tcell.StyleDefault.Background(tcell.ColorDarkSlateGray))
|
2024-04-27 22:32:05 +03:00
|
|
|
|
|
|
|
return iss
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iss *InventoryScreenState) OnInput(e *tcell.EventKey) {
|
|
|
|
if e.Key() == tcell.KeyEsc || (e.Key() == tcell.KeyRune && e.Rune() == 'i') {
|
|
|
|
iss.exitMenu = true
|
|
|
|
}
|
2024-05-03 13:46:32 +03:00
|
|
|
|
|
|
|
if e.Key() == tcell.KeyRune && e.Rune() == 'x' {
|
|
|
|
iss.dropSelectedInventorySlot = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Key() != tcell.KeyRune {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.Rune() {
|
|
|
|
case 'k':
|
|
|
|
iss.moveInventorySlotDirection = model.DirectionUp
|
|
|
|
case 'j':
|
|
|
|
iss.moveInventorySlotDirection = model.DirectionDown
|
|
|
|
case 'h':
|
|
|
|
iss.moveInventorySlotDirection = model.DirectionLeft
|
|
|
|
case 'l':
|
|
|
|
iss.moveInventorySlotDirection = model.DirectionRight
|
|
|
|
}
|
2024-04-27 22:32:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (iss *InventoryScreenState) OnTick(dt int64) GameState {
|
|
|
|
if iss.exitMenu {
|
2024-05-03 13:46:32 +03:00
|
|
|
iss.prevState.Unpause()
|
2024-04-27 22:32:05 +03:00
|
|
|
return iss.prevState
|
|
|
|
}
|
|
|
|
|
2024-05-03 13:46:32 +03:00
|
|
|
if iss.dropSelectedInventorySlot {
|
|
|
|
iss.player.Inventory().Drop(iss.selectedInventorySlot.XY())
|
|
|
|
iss.dropSelectedInventorySlot = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if iss.moveInventorySlotDirection != model.DirectionNone {
|
|
|
|
|
|
|
|
switch iss.moveInventorySlotDirection {
|
|
|
|
case model.DirectionUp:
|
|
|
|
if iss.selectedInventorySlot.Y() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
iss.selectedInventorySlot = iss.selectedInventorySlot.WithOffset(0, -1)
|
|
|
|
case model.DirectionDown:
|
|
|
|
if iss.selectedInventorySlot.Y() == iss.player.Inventory().Shape().Height()-1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
iss.selectedInventorySlot = iss.selectedInventorySlot.WithOffset(0, +1)
|
|
|
|
case model.DirectionLeft:
|
|
|
|
if iss.selectedInventorySlot.X() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
iss.selectedInventorySlot = iss.selectedInventorySlot.WithOffset(-1, 0)
|
|
|
|
case model.DirectionRight:
|
|
|
|
if iss.selectedInventorySlot.X() == iss.player.Inventory().Shape().Width()-1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
iss.selectedInventorySlot = iss.selectedInventorySlot.WithOffset(+1, 0)
|
|
|
|
}
|
|
|
|
|
2024-05-06 20:43:35 +03:00
|
|
|
iss.inventoryMenu.SelectSlot(iss.selectedInventorySlot.XY())
|
2024-05-03 13:46:32 +03:00
|
|
|
iss.moveInventorySlotDirection = model.DirectionNone
|
|
|
|
}
|
|
|
|
|
2024-04-27 22:32:05 +03:00
|
|
|
return iss
|
|
|
|
}
|
|
|
|
|
2024-05-06 20:43:35 +03:00
|
|
|
func (iss *InventoryScreenState) CollectDrawables() []engine.Drawable {
|
2024-05-03 13:46:32 +03:00
|
|
|
drawables := append(
|
2024-04-27 22:32:05 +03:00
|
|
|
iss.prevState.CollectDrawables(),
|
|
|
|
iss.inventoryMenu,
|
|
|
|
)
|
2024-05-03 13:46:32 +03:00
|
|
|
|
|
|
|
return drawables
|
2024-04-27 22:32:05 +03:00
|
|
|
}
|