last_light/game/state/inventory_screen_state.go

116 lines
2.8 KiB
Go
Raw Normal View History

package state
import (
2024-05-06 20:43:35 +03:00
"mvvasilev/last_light/engine"
"mvvasilev/last_light/game/model"
2024-05-06 20:43:35 +03:00
"mvvasilev/last_light/game/ui/menu"
"github.com/gdamore/tcell/v2"
)
type InventoryScreenState struct {
2024-05-03 13:46:32 +03:00
prevState PausableState
exitMenu bool
2024-05-06 20:43:35 +03:00
inventoryMenu *menu.PlayerInventoryMenu
2024-05-06 21:47:20 +03:00
selectedInventorySlot engine.Position
player *model.Player
2024-05-03 13:46:32 +03:00
moveInventorySlotDirection model.Direction
dropSelectedInventorySlot bool
}
2024-05-03 13:46:32 +03:00
func CreateInventoryScreenState(player *model.Player, prevState PausableState) *InventoryScreenState {
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))
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
}
}
func (iss *InventoryScreenState) OnTick(dt int64) GameState {
if iss.exitMenu {
2024-05-03 13:46:32 +03:00
iss.prevState.Unpause()
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
}
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(
iss.prevState.CollectDrawables(),
iss.inventoryMenu,
)
2024-05-03 13:46:32 +03:00
return drawables
}