last_light/game/ui/menu/menu_player_inventory_menu.go

229 lines
5.8 KiB
Go
Raw Normal View History

2024-05-06 20:43:35 +03:00
package menu
import (
"fmt"
"mvvasilev/last_light/engine"
2024-06-06 23:17:22 +03:00
"mvvasilev/last_light/game/model"
"mvvasilev/last_light/game/systems"
2024-05-06 20:43:35 +03:00
"mvvasilev/last_light/game/ui"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
"github.com/google/uuid"
)
type PlayerInventoryMenu struct {
2024-06-06 23:17:22 +03:00
inventory *model.EquippedInventory
2024-05-06 20:43:35 +03:00
inventoryMenu *ui.UIWindow
armourLabel *ui.UILabel
armourGrid *engine.Grid
leftHandLabel *ui.UILabel
leftHandBox engine.Rectangle
rightHandLabel *ui.UILabel
rightHandBox engine.Rectangle
inventoryGrid *engine.Grid
playerItems *engine.ArbitraryDrawable
selectedItem *engine.ArbitraryDrawable
equipmentSlots *engine.ArbitraryDrawable
2024-05-06 21:47:20 +03:00
selectedInventorySlot engine.Position
highlightStyle tcell.Style
2024-05-06 20:43:35 +03:00
}
2024-06-06 23:17:22 +03:00
func CreatePlayerInventoryMenu(x, y int, playerInventory *model.EquippedInventory, style tcell.Style, highlightStyle tcell.Style) *PlayerInventoryMenu {
2024-05-06 20:43:35 +03:00
menu := new(PlayerInventoryMenu)
menu.inventory = playerInventory
menu.inventoryMenu = ui.CreateWindow(x, y, 37, 24, "INVENTORY", style)
menu.armourLabel = ui.CreateSingleLineUILabel(x+15, y+1, "ARMOUR", style)
menu.armourGrid = engine.CreateGrid(
x+10, y+2, 3, 1, 4, 1, '┌', '─', '┬', '┐', '│', ' ', '│', '│', '├', '─', '┼', '┤', '└', '─', '┴', '┘', style, highlightStyle, //style.Background(tcell.ColorDarkSlateGray),
)
menu.leftHandLabel = ui.CreateUILabel(
x+3, y+1, 5, 1, "OFF", style,
)
menu.leftHandBox = engine.CreateRectangle(
x+2, y+2, 5, 3,
'┌', '─', '┐',
'│', ' ', '│',
'└', '─', '┘',
false, true,
style,
)
menu.rightHandLabel = ui.CreateUILabel(
x+31, y+1, 5, 1, "DOM", style,
)
menu.rightHandBox = engine.CreateRectangle(
x+30, y+2, 5, 3,
'┌', '─', '┐',
'│', ' ', '│',
'└', '─', '┘',
false, true,
style,
)
menu.inventoryGrid = engine.CreateGrid(
x+2, y+5, 3, 1, 8, 4, '┌', '─', '┬', '┐', '│', ' ', '│', '│', '├', '─', '┼', '┤', '└', '─', '┴', '┘', style, highlightStyle,
)
menu.highlightStyle = highlightStyle
2024-05-06 20:43:35 +03:00
menu.playerItems = engine.CreateDrawingInstructions(func(v views.View) {
for y := range menu.inventory.Shape().Height() {
for x := range menu.inventory.Shape().Width() {
item := menu.inventory.ItemAt(x, y)
2024-05-06 20:43:35 +03:00
isHighlighted := x == menu.selectedInventorySlot.X() && y == menu.selectedInventorySlot.Y()
if item == nil {
if isHighlighted {
ui.CreateSingleLineUILabel(
menu.inventoryGrid.Position().X()+1+x*4,
menu.inventoryGrid.Position().Y()+1+y*2,
" ",
menu.highlightStyle,
2024-05-06 20:43:35 +03:00
).Draw(v)
}
continue
}
2024-06-06 23:17:22 +03:00
style := item.Style()
2024-05-06 20:43:35 +03:00
if isHighlighted {
style = menu.highlightStyle
2024-05-06 20:43:35 +03:00
}
2024-06-06 23:17:22 +03:00
menu.drawItemSlot(
2024-05-06 20:43:35 +03:00
menu.inventoryGrid.Position().X()+1+x*4,
menu.inventoryGrid.Position().Y()+y*2,
2024-06-06 23:17:22 +03:00
item,
2024-05-06 20:43:35 +03:00
style,
2024-06-06 23:17:22 +03:00
v,
)
2024-05-06 20:43:35 +03:00
}
}
})
menu.selectedItem = engine.CreateDrawingInstructions(func(v views.View) {
item := menu.inventory.ItemAt(menu.selectedInventorySlot.XY())
2024-05-06 20:43:35 +03:00
if item == nil {
return
}
2024-06-06 23:17:22 +03:00
ui.CreateUIItem(x+2, y+14, item, style).Draw(v)
2024-05-06 20:43:35 +03:00
})
menu.equipmentSlots = engine.CreateDrawingInstructions(func(v views.View) {
drawEquipmentSlot(menu.rightHandBox.Position().X()+1, menu.rightHandBox.Position().Y(), menu.inventory.AtSlot(model.EquippedSlotDominantHand), false, menu.highlightStyle, v)
drawEquipmentSlot(menu.leftHandBox.Position().X()+1, menu.leftHandBox.Position().Y(), menu.inventory.AtSlot(model.EquippedSlotOffhand), false, menu.highlightStyle, v)
drawEquipmentSlot(x+10+1, y+3, menu.inventory.AtSlot(model.EquippedSlotHead), false, menu.highlightStyle, v)
drawEquipmentSlot(x+10+4, y+3, menu.inventory.AtSlot(model.EquippedSlotChestplate), false, menu.highlightStyle, v)
drawEquipmentSlot(x+10+7, y+3, menu.inventory.AtSlot(model.EquippedSlotLeggings), false, menu.highlightStyle, v)
drawEquipmentSlot(x+10+10, y+3, menu.inventory.AtSlot(model.EquippedSlotShoes), false, menu.highlightStyle, v)
})
2024-05-06 20:43:35 +03:00
return menu
}
func drawEquipmentSlot(screenX, screenY int, item model.Item, highlighted bool, highlightStyle tcell.Style, v views.View) {
if item == nil {
return
}
style := item.Style()
if highlighted {
style = highlightStyle
}
if item.Quantifiable() != nil {
ui.CreateSingleLineUILabel(
screenX,
screenY,
fmt.Sprintf("%03d", item.Quantifiable().CurrentQuantity),
style,
).Draw(v)
}
ui.CreateSingleLineUILabel(
screenX,
screenY+1,
item.Icon(),
style,
).Draw(v)
}
2024-06-06 23:28:06 +03:00
func (pim *PlayerInventoryMenu) drawItemSlot(screenX, screenY int, item model.Item, style tcell.Style, v views.View) {
2024-06-06 23:17:22 +03:00
if item.Quantifiable() != nil {
ui.CreateSingleLineUILabel(
screenX,
screenY,
fmt.Sprintf("%03d", item.Quantifiable().CurrentQuantity),
style,
).Draw(v)
}
ui.CreateSingleLineUILabel(
screenX,
screenY+1,
item.Icon(),
style,
).Draw(v)
}
2024-05-06 20:43:35 +03:00
func (pim *PlayerInventoryMenu) MoveTo(x int, y int) {
}
2024-05-06 21:47:20 +03:00
func (pim *PlayerInventoryMenu) Position() engine.Position {
2024-05-06 20:43:35 +03:00
return pim.inventoryMenu.Position()
}
2024-05-06 21:47:20 +03:00
func (pim *PlayerInventoryMenu) Size() engine.Size {
2024-05-06 20:43:35 +03:00
return pim.inventoryMenu.Size()
}
2024-06-06 23:17:22 +03:00
func (pim *PlayerInventoryMenu) Input(inputAction systems.InputAction) {
2024-05-06 20:43:35 +03:00
}
func (pim *PlayerInventoryMenu) UniqueId() uuid.UUID {
return pim.inventoryMenu.UniqueId()
}
func (pim *PlayerInventoryMenu) SelectSlot(x, y int) {
2024-05-30 23:39:54 +03:00
if pim.selectedInventorySlot.X() == x && pim.selectedInventorySlot.Y() == y {
return
}
2024-05-06 20:43:35 +03:00
pim.inventoryGrid.Unhighlight()
2024-05-06 21:47:20 +03:00
pim.selectedInventorySlot = engine.PositionAt(x, y)
2024-05-06 20:43:35 +03:00
pim.inventoryGrid.Highlight(pim.selectedInventorySlot)
}
func (pim *PlayerInventoryMenu) Draw(v views.View) {
pim.inventoryMenu.Draw(v)
pim.armourLabel.Draw(v)
pim.armourGrid.Draw(v)
pim.leftHandLabel.Draw(v)
pim.leftHandBox.Draw(v)
pim.rightHandLabel.Draw(v)
pim.rightHandBox.Draw(v)
pim.inventoryGrid.Draw(v)
pim.playerItems.Draw(v)
pim.selectedItem.Draw(v)
pim.equipmentSlots.Draw(v)
2024-05-06 20:43:35 +03:00
}