Randoloot/scripts/ui/ship_selection.gd
2026-10-10 21:21:58 +02:00

66 lines
2.2 KiB
GDScript

extends Control
const GAME_SCENE := "res://scenes/game/game.tscn"
const SHIP_DIRECTORY := "res://assets/Ships"
@onready var ship_grid: GridContainer = %ShipGrid
@onready var selected_label: Label = %SelectedLabel
@onready var continue_button: Button = %ContinueButton
var button_group := ButtonGroup.new()
func _ready() -> void:
button_group.allow_unpress = false
continue_button.pressed.connect(_start_game)
_load_ships()
func _load_ships() -> void:
var directory := DirAccess.open(SHIP_DIRECTORY)
if directory == null:
selected_label.text = "Nie można znaleźć katalogu statków."
return
var ship_files: Array[String] = []
for file_name in directory.get_files():
if file_name.get_extension().to_lower() == "png":
ship_files.append(file_name)
ship_files.sort_custom(func(a: String, b: String) -> bool:
return _ship_number(a) < _ship_number(b)
)
for file_name in ship_files:
var ship_path := SHIP_DIRECTORY.path_join(file_name)
var texture := load(ship_path) as Texture2D
if texture == null:
continue
var ship_number := _ship_number(file_name)
var button := Button.new()
button.custom_minimum_size = Vector2(150, 125)
button.toggle_mode = true
button.button_group = button_group
button.icon = texture
button.expand_icon = true
button.add_theme_constant_override("icon_max_width", 92)
button.vertical_icon_alignment = VERTICAL_ALIGNMENT_TOP
button.text = "Statek %d" % ship_number
button.add_theme_font_size_override("font_size", 16)
button.pressed.connect(_select_ship.bind(ship_path, ship_number, button))
ship_grid.add_child(button)
if GameState.selected_ship_path == ship_path or GameState.selected_ship_path.is_empty():
_select_ship(ship_path, ship_number, button)
func _ship_number(file_name: String) -> int:
var digits := file_name.get_basename().trim_prefix("Spaceship_")
return int(digits) if digits.is_valid_int() else 9999
func _select_ship(ship_path: String, ship_number: int, button: Button) -> void:
GameState.selected_ship_path = ship_path
button.button_pressed = true
selected_label.text = "Wybrano: Statek %d" % ship_number
continue_button.disabled = false
func _start_game() -> void:
GameState.start_new_run()
get_tree().change_scene_to_file(GAME_SCENE)