131 lines
5.3 KiB
GDScript
131 lines
5.3 KiB
GDScript
extends PanelContainer
|
|
|
|
const SLOT_SCENE := preload("res://scenes/ui/inventory_slot_view.tscn")
|
|
const CARGO_SIZE := 20
|
|
|
|
@onready var cargo_grid: GridContainer = $Contents/CargoGrid
|
|
@onready var close_button: Button = $Contents/Header/CloseButton
|
|
@onready var title_label: Label = $Contents/Header/Title
|
|
|
|
var _inventory: ShipInventory
|
|
var _drawer_open := false
|
|
var _slide_tween: Tween
|
|
var _resource_cards: Dictionary = {}
|
|
var drawer_width := 560.0
|
|
|
|
func _ready() -> void:
|
|
close_button.pressed.connect(close_drawer)
|
|
preload("res://scripts/ui/hud_button_style.gd").apply(close_button, Color("91dbea"))
|
|
close_button.add_theme_font_size_override("font_size", 10)
|
|
_create_resource_section()
|
|
_inventory = get_tree().get_first_node_in_group("ship_inventory") as ShipInventory
|
|
if _inventory:
|
|
_inventory.inventory_changed.connect(_refresh_title)
|
|
_inventory.ammunition_changed.connect(_refresh_title)
|
|
GameState.captain.changed.connect(_refresh_title)
|
|
for index in CARGO_SIZE:
|
|
var slot := SLOT_SCENE.instantiate()
|
|
slot.name = "Cargo%d" % (index + 1)
|
|
slot.set("slot_kind", "cargo")
|
|
slot.set("slot_index", index)
|
|
cargo_grid.add_child(slot)
|
|
_refresh_title()
|
|
|
|
func _refresh_title() -> void:
|
|
for key in ["iron", "titanium", "aluminium", "uranium"]:
|
|
_resource_cards[key].set_amount(float(GameState.captain.minerals.get(key, 0)))
|
|
_resource_cards["parts"].set_amount(GameState.captain.parts)
|
|
if _inventory:
|
|
title_label.text = "MAGAZYN · %d / %d" % [_inventory.get_cargo_used(), CARGO_SIZE]
|
|
_resource_cards["ammunition"].set_amount(_inventory.ammunition)
|
|
_resource_cards["irradiated_ammunition"].set_amount(_inventory.irradiated_ammunition)
|
|
_resource_cards["missile"].set_amount(_inventory.get_missile_count())
|
|
|
|
func _create_resource_section() -> void:
|
|
var frame := PanelContainer.new()
|
|
frame.name = "ResourceSection"
|
|
var style := StyleBoxFlat.new()
|
|
style.bg_color = Color("101e2d")
|
|
style.border_color = Color("2d4c60")
|
|
style.set_border_width_all(1)
|
|
style.set_corner_radius_all(5)
|
|
style.set_content_margin_all(8)
|
|
frame.add_theme_stylebox_override("panel", style)
|
|
$Contents.add_child(frame)
|
|
$Contents.move_child(frame, cargo_grid.get_index())
|
|
var section := VBoxContainer.new()
|
|
section.name = "Resources"
|
|
section.add_theme_constant_override("separation", 6)
|
|
frame.add_child(section)
|
|
var minerals := _resource_row(section, "SUROWCE", Color("d4b58a"))
|
|
for data in [
|
|
["iron", "Żelazo", "Surowiec pozyskiwany z asteroid.", Color("c5b5a0")],
|
|
["titanium", "Tytan", "Surowiec pozyskiwany z asteroid.", Color("9ecbe9")],
|
|
["aluminium", "Aluminium", "Surowiec pozyskiwany z asteroid.", Color("dce8ef")],
|
|
["uranium", "Uran", "Surowiec z asteroid. Do produkcji napromieniowanej amunicji.", Color("a9ef83")],
|
|
["parts", "Części", "Odzyskiwane z przedmiotów i wraków. Do produkcji amunicji i napraw.", Color("efb568")],
|
|
]:
|
|
_add_resource_card(minerals, data)
|
|
var ammunition := _resource_row(section, "AMUNICJA", Color("91dbea"))
|
|
for data in [
|
|
["ammunition", "Zwykła amunicja", "Pociski do działka maszynowego. Obrażenia kinetyczne.", Color("efb568")],
|
|
["irradiated_ammunition", "Napromieniowana amunicja", "Kapsuły do działka gamma. Obrażenia radiacyjne.", Color("a9ef83")],
|
|
["missile", "Rakiety", "Amunicja do wyrzutni rakiet. Obrażenia kinetyczne.", Color("ffac8b")],
|
|
]:
|
|
_add_resource_card(ammunition, data)
|
|
var cargo_title := Label.new()
|
|
cargo_title.text = "PRZEDMIOTY"
|
|
cargo_title.add_theme_font_size_override("font_size", 10)
|
|
cargo_title.add_theme_color_override("font_color", Color("91dbea"))
|
|
$Contents.add_child(cargo_title)
|
|
$Contents.move_child(cargo_title, cargo_grid.get_index())
|
|
|
|
func _resource_row(section: VBoxContainer, caption: String, color: Color) -> HBoxContainer:
|
|
var title := Label.new()
|
|
title.text = caption
|
|
title.add_theme_font_size_override("font_size", 10)
|
|
title.add_theme_color_override("font_color", color)
|
|
section.add_child(title)
|
|
var row := HBoxContainer.new()
|
|
row.add_theme_constant_override("separation", 5)
|
|
section.add_child(row)
|
|
return row
|
|
|
|
func _add_resource_card(row: HBoxContainer, data: Array) -> void:
|
|
var card := PanelContainer.new()
|
|
card.set_script(preload("res://scripts/ui/inventory_resource_card.gd"))
|
|
card.name = str(data[0]).to_pascal_case()
|
|
card.set("resource_name", data[1])
|
|
card.set("description", data[2])
|
|
card.set("accent", data[3])
|
|
card.set("icon_texture", load("res://assets/icons/resources/%s.png" % data[0]))
|
|
row.add_child(card)
|
|
_resource_cards[data[0]] = card
|
|
|
|
func toggle_drawer() -> void:
|
|
_set_drawer_open(not _drawer_open)
|
|
|
|
func close_drawer(immediately: bool = false) -> void:
|
|
if immediately:
|
|
if _slide_tween != null and _slide_tween.is_running():
|
|
_slide_tween.kill()
|
|
_drawer_open = false
|
|
GameState.hud_panel_state["inventory_open"] = false
|
|
position.x = drawer_width
|
|
hide()
|
|
return
|
|
_set_drawer_open(false)
|
|
|
|
func _set_drawer_open(value: bool) -> void:
|
|
if _slide_tween != null and _slide_tween.is_running():
|
|
_slide_tween.kill()
|
|
_drawer_open = value
|
|
GameState.hud_panel_state["inventory_open"] = value
|
|
if value and not visible:
|
|
position = Vector2(drawer_width, 0)
|
|
show()
|
|
_slide_tween = create_tween()
|
|
_slide_tween.tween_property(self, "position:x", 0.0 if value else drawer_width, 0.25).set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT if value else Tween.EASE_IN)
|
|
if not value:
|
|
_slide_tween.tween_callback(hide)
|