spaceship/ui/menus/CraftMenu.gd
2026-08-31 02:18:08 +02:00

144 lines
4.7 KiB
GDScript

extends "res://ui/menus/ModalMenu.gd"
signal craft_requested(vessel_type: String)
signal queue_cancel_requested(index: int)
const VESSEL_DEFINITIONS = preload("res://vessels/VesselDefinitions.gd")
const MENU_LAYOUT = preload("res://ui/menus/MenuLayout.gd")
const PRICE_TAG = preload("res://ui/PriceTag.gd")
const COLUMNS := 3
const ROW_PITCH := 260.0
const COLUMN_X := 190.0
const BUTTON_HALF := 56.0
const BUTTON_HEIGHT := 100.0
const LABEL_HALF := 92.0
const LABEL_HEIGHT := 44.0
const LABEL_OFFSET := 106.0
const PRICE_HALF := 92.0
const PRICE_HEIGHT := 86.0
const PRICE_OFFSET := 154.0
@onready var status: Label = $StatusLabel
@onready var queue_detail: Label = $QueueDetail
@onready var resource_bar: Control = $Bar
@onready var vessel_buttons: Array = [$InterceptorButton, $ShieldButton,
$GasBomberButton, $CarrierButton, $LaserButton, $DistorderButton]
@onready var vessel_labels: Array = [$InterceptorLabel, $ShieldLabel,
$GasBomberLabel, $CarrierLabel, $LaserLabel, $DistorderLabel]
@onready var queue_slots: Array = [$QueueSlot0, $QueueSlot1, $QueueSlot2, $QueueSlot3]
var building_id: int = 0
var message: String = ""
var message_until: int = 0
var price_tags: Array = []
func _ready() -> void:
super._ready()
_build_price_tags()
for i in range(mini(vessel_buttons.size(), VESSEL_DEFINITIONS.fleet().size())):
var definition: Resource = VESSEL_DEFINITIONS.fleet()[i]
vessel_buttons[i].pressed.connect(
func(): craft_requested.emit(String(definition.definition_id)))
for i in range(queue_slots.size()):
queue_slots[i].pressed.connect(func(): queue_cancel_requested.emit(i))
_layout_entries()
func open_for(next_building_id: int) -> void:
building_id = next_building_id
resource_bar.building_id = building_id
open_menu()
refresh()
func close_menu() -> void:
super.close_menu()
building_id = 0
func say(next_message: String) -> void:
message = next_message
message_until = Time.get_ticks_msec() + 3000
refresh()
func refresh() -> void:
status.text = ""
var definitions: Array = VESSEL_DEFINITIONS.fleet()
for i in range(mini(definitions.size(), vessel_buttons.size())):
var definition: Resource = definitions[i]
var button: Control = vessel_buttons[i]
vessel_labels[i].text = String(definition.display_name)
if i < price_tags.size():
price_tags[i].set_costs(GameState.product_costs(definition.definition_id))
button.icon = definition.icon
button.glow_color = definition.glow_color
button.enabled = GameState.vessel_is_unlocked(definition.definition_id)
button.queue_redraw()
refresh_queue()
if message != "":
if Time.get_ticks_msec() < message_until:
status.text = "%s\n%s" % [message, status.text]
else:
message = ""
func refresh_queue() -> void:
var orders: Array = GameState.order_list(building_id)
for i in range(queue_slots.size()):
var slot: Control = queue_slots[i]
slot.visible = i < orders.size()
if not slot.visible:
continue
var order: Dictionary = orders[i]
var definition: Resource = VESSEL_DEFINITIONS.get_definition(String(order.product))
slot.icon = definition.icon
slot.glow_color = vessel_colour(String(order.product))
slot.enabled = bool(order.started)
slot.queue_redraw()
if orders.is_empty():
queue_detail.text = "Nothing queued"
return
var head: Dictionary = orders[0]
if bool(head.started):
queue_detail.text = "Building %s %d%%\nTap to cancel" % [
GameState.vessel_name(String(head.product)),
int(round(float(head.progress) * 100.0))]
else:
queue_detail.text = "%s waiting for resources\nTap to cancel" \
% GameState.vessel_name(String(head.product))
func vessel_colour(vessel_type: String) -> Color:
var definition: Resource = VESSEL_DEFINITIONS.get_definition(vessel_type)
return definition.glow_color if definition != null else Color(0.25, 0.85, 1.0)
func _build_price_tags() -> void:
for i in range(vessel_buttons.size()):
var tag: Control = PRICE_TAG.new()
tag.name = "CraftPrice%d" % i
tag.set_anchors_preset(Control.PRESET_CENTER)
add_child(tag)
price_tags.append(tag)
func row_height() -> float:
return PRICE_OFFSET + PRICE_HEIGHT
func _layout_entries() -> void:
var rows: int = maxi(int(ceil(float(vessel_buttons.size()) / float(COLUMNS))), 1)
var band: Vector2 = MENU_LAYOUT.grid_band(status.offset_bottom, size.y)
for i in range(vessel_buttons.size()):
var row: int = i / COLUMNS
var column: int = i % COLUMNS
var cx: float = (float(column) - 1.0) * COLUMN_X
var top: float = MENU_LAYOUT.grid_row_top(row, rows, row_height(), band, ROW_PITCH)
MENU_LAYOUT.place_entry(vessel_buttons[i], cx, top, BUTTON_HALF, BUTTON_HEIGHT)
MENU_LAYOUT.place_entry(vessel_labels[i], cx, top + LABEL_OFFSET,
LABEL_HALF, LABEL_HEIGHT)
if i < price_tags.size():
MENU_LAYOUT.place_entry(price_tags[i], cx, top + PRICE_OFFSET,
PRICE_HALF, PRICE_HEIGHT)