diff --git a/project.godot b/project.godot index 4f3ce3a..109e6cb 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,10 @@ config/features=PackedStringArray("4.7", "Forward Plus") config/icon="res://icon.svg" run/main_scene="res://scenes/ui/main_menu.tscn" +[autoload] + +GameState="*res://scripts/game/game_state.gd" + [display] window/size/viewport_width=1280 diff --git a/scenes/ui/ship_selection.tscn b/scenes/ui/ship_selection.tscn new file mode 100644 index 0000000..313b381 --- /dev/null +++ b/scenes/ui/ship_selection.tscn @@ -0,0 +1,77 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://scripts/ui/ship_selection.gd" id="1"] + +[node name="ShipSelection" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1") + +[node name="Background" type="ColorRect" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +color = Color(0.018, 0.035, 0.065, 1) + +[node name="Layout" type="VBoxContainer" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = 48.0 +offset_top = 28.0 +offset_right = -48.0 +offset_bottom = -28.0 +grow_horizontal = 2 +grow_vertical = 2 +theme_override_constants/separation = 14 + +[node name="Heading" type="Label" parent="Layout"] +layout_mode = 2 +theme_override_colors/font_color = Color(0.92, 0.96, 1, 1) +theme_override_font_sizes/font_size = 34 +text = "Wybierz statek" + +[node name="Instructions" type="Label" parent="Layout"] +layout_mode = 2 +theme_override_colors/font_color = Color(0.55, 0.7, 0.82, 1) +text = "Wybierz jednostkę, którą rozpoczniesz nową grę." + +[node name="ShipScroll" type="ScrollContainer" parent="Layout"] +layout_mode = 2 +size_flags_vertical = 3 + +[node name="ShipGrid" type="GridContainer" parent="Layout/ShipScroll"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +theme_override_constants/h_separation = 14 +theme_override_constants/v_separation = 14 +columns = 6 + +[node name="Footer" type="HBoxContainer" parent="Layout"] +layout_mode = 2 +theme_override_constants/separation = 20 + +[node name="SelectedLabel" type="Label" parent="Layout/Footer"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +vertical_alignment = 1 +text = "Wybierz statek" + +[node name="ContinueButton" type="Button" parent="Layout/Footer"] +unique_name_in_owner = true +custom_minimum_size = Vector2(220, 52) +layout_mode = 2 +theme_override_font_sizes/font_size = 20 +text = "Rozpocznij grę" + diff --git a/scripts/game/game.gd b/scripts/game/game.gd index de076f5..19d1805 100644 --- a/scripts/game/game.gd +++ b/scripts/game/game.gd @@ -1,10 +1,25 @@ extends Control -func _ready() -> void: - var label := Label.new() - label.text = "Nowa gra" - label.add_theme_font_size_override("font_size", 32) - label.set_anchors_preset(Control.PRESET_CENTER) - label.position = Vector2(-80, -20) - add_child(label) +var ship_sprite: Sprite2D + +func _ready() -> void: + if GameState.selected_ship_path.is_empty() or not ResourceLoader.exists(GameState.selected_ship_path): + push_warning("Nie wybrano statku do nowej gry.") + return + + var ship_texture := load(GameState.selected_ship_path) as Texture2D + if ship_texture == null: + push_warning("Nie udało się wczytać wybranego statku.") + return + + ship_sprite = Sprite2D.new() + ship_sprite.name = "PlayerShip" + ship_sprite.texture = ship_texture + add_child(ship_sprite) + _update_ship_position() + get_viewport().size_changed.connect(_update_ship_position) + +func _update_ship_position() -> void: + if ship_sprite: + ship_sprite.position = get_viewport_rect().size * 0.5 diff --git a/scripts/game/game_state.gd b/scripts/game/game_state.gd new file mode 100644 index 0000000..5d3df07 --- /dev/null +++ b/scripts/game/game_state.gd @@ -0,0 +1,4 @@ +extends Node + +var selected_ship_path: String = "" + diff --git a/scripts/game/game_state.gd.uid b/scripts/game/game_state.gd.uid new file mode 100644 index 0000000..dc6b239 --- /dev/null +++ b/scripts/game/game_state.gd.uid @@ -0,0 +1 @@ +uid://cb1wtbdh8778r diff --git a/scripts/ui/main_menu.gd b/scripts/ui/main_menu.gd index 8e42871..0d872db 100644 --- a/scripts/ui/main_menu.gd +++ b/scripts/ui/main_menu.gd @@ -1,6 +1,6 @@ extends Control -const GAME_SCENE := "res://scenes/game/game.tscn" +const SHIP_SELECTION_SCENE := "res://scenes/ui/ship_selection.tscn" @onready var start_button: Button = %StartButton @onready var exit_button: Button = %ExitButton @@ -10,7 +10,7 @@ func _ready() -> void: exit_button.pressed.connect(_exit_game) func _start_game() -> void: - get_tree().change_scene_to_file(GAME_SCENE) + get_tree().change_scene_to_file(SHIP_SELECTION_SCENE) func _exit_game() -> void: get_tree().quit() diff --git a/scripts/ui/ship_selection.gd b/scripts/ui/ship_selection.gd new file mode 100644 index 0000000..b1996d4 --- /dev/null +++ b/scripts/ui/ship_selection.gd @@ -0,0 +1,64 @@ +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: + get_tree().change_scene_to_file(GAME_SCENE) diff --git a/scripts/ui/ship_selection.gd.uid b/scripts/ui/ship_selection.gd.uid new file mode 100644 index 0000000..98eb060 --- /dev/null +++ b/scripts/ui/ship_selection.gd.uid @@ -0,0 +1 @@ +uid://cd5rwc8o0hios