last_light/game/state/inventory_screen_state.go

94 lines
2.6 KiB
Go
Raw Normal View History

package state
import (
2024-05-06 20:43:35 +03:00
"mvvasilev/last_light/engine"
2024-05-30 23:39:54 +03:00
"mvvasilev/last_light/game/input"
2024-05-21 23:08:51 +03:00
"mvvasilev/last_light/game/player"
2024-05-30 23:39:54 +03:00
"mvvasilev/last_light/game/turns"
2024-05-06 20:43:35 +03:00
"mvvasilev/last_light/game/ui/menu"
"github.com/gdamore/tcell/v2"
)
type InventoryScreenState struct {
2024-05-30 23:39:54 +03:00
inputSystem *input.InputSystem
turnSystem *turns.TurnSystem
prevState GameState
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-05-21 23:08:51 +03:00
player *player.Player
}
2024-05-30 23:39:54 +03:00
func CreateInventoryScreenState(inputSystem *input.InputSystem, turnSystem *turns.TurnSystem, player *player.Player, prevState GameState) *InventoryScreenState {
iss := new(InventoryScreenState)
2024-05-30 23:39:54 +03:00
iss.inputSystem = inputSystem
iss.turnSystem = turnSystem
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
}
2024-05-30 23:39:54 +03:00
func (s *InventoryScreenState) InputContext() input.Context {
return input.InputContext_Inventory
}
2024-05-30 23:39:54 +03:00
func (iss *InventoryScreenState) OnTick(dt int64) (nextState GameState) {
nextAction := iss.inputSystem.NextAction()
nextState = iss
2024-05-30 23:39:54 +03:00
switch nextAction {
case input.InputAction_Menu_Exit:
nextState = iss.prevState
case input.InputAction_DropItem:
2024-05-03 13:46:32 +03:00
iss.player.Inventory().Drop(iss.selectedInventorySlot.XY())
2024-05-30 23:39:54 +03:00
case input.InputAction_Menu_HighlightUp:
if iss.selectedInventorySlot.Y() == 0 {
break
}
iss.selectedInventorySlot = iss.selectedInventorySlot.WithOffset(0, -1)
iss.inventoryMenu.SelectSlot(iss.selectedInventorySlot.XY())
case input.InputAction_Menu_HighlightDown:
if iss.selectedInventorySlot.Y() == iss.player.Inventory().Shape().Height()-1 {
break
}
2024-05-03 13:46:32 +03:00
2024-05-30 23:39:54 +03:00
iss.selectedInventorySlot = iss.selectedInventorySlot.WithOffset(0, +1)
iss.inventoryMenu.SelectSlot(iss.selectedInventorySlot.XY())
case input.InputAction_Menu_HighlightLeft:
if iss.selectedInventorySlot.X() == 0 {
break
2024-05-03 13:46:32 +03:00
}
2024-05-30 23:39:54 +03:00
iss.selectedInventorySlot = iss.selectedInventorySlot.WithOffset(-1, 0)
iss.inventoryMenu.SelectSlot(iss.selectedInventorySlot.XY())
case input.InputAction_Menu_HighlightRight:
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
}
2024-05-30 23:39:54 +03:00
return
}
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
}