Some code organization

This commit is contained in:
2025-12-26 16:12:46 +03:00
parent 099cda6368
commit e2f9f75c4f
55 changed files with 56 additions and 47 deletions
@@ -0,0 +1,62 @@
class_name WeaponSelectionScreen
extends Control
signal weapon_selected(weapon_data: WeaponData)
const WEAPON_SELECTOR = preload("res://game/menu/weapon_selection/weapon_selector.tscn")
@export var world_data : WorldData:
set = _set_world_data
@onready var weapon_selectors : Control = $%WeaponSelectors
func _input(event: InputEvent) -> void:
if event.is_action_pressed("ui_left") and _play_left_sound():
SoundManager.play_ui_stream(SoundManager.ui_stream_previous)
if event.is_action_pressed("ui_right") and _play_right_sound():
SoundManager.play_ui_stream(SoundManager.ui_stream_next)
func _play_left_sound() -> bool:
return _play_side_sound(1, 0)
func _play_right_sound() -> bool:
return _play_side_sound(0, 1)
func _play_side_sound(offset_begin: int, offset_end: int) -> bool:
for i in range(offset_begin, weapon_selectors.get_child_count() - offset_end):
var child := weapon_selectors.get_child(i)
if child is WeaponSelector and child.button.has_focus():
return true
return false
func _set_world_data(data: WorldData) -> void:
world_data = data
for child in weapon_selectors.get_children():
child.queue_free()
if world_data == null: return
var selectors : Array[WeaponSelector] = []
for weapon_data in world_data.player_start_weapons:
var selector : WeaponSelector = WEAPON_SELECTOR.instantiate()
weapon_selectors.add_child(selector)
selector.weapon_data = weapon_data
selectors.append(selector)
selector.weapon_selected.connect(_on_weapon_selected)
if selectors.size() > 0:
selectors[0].button.grab_focus()
func _on_weapon_selected(weapon_data: WeaponData) -> void:
weapon_selected.emit(weapon_data)
@@ -0,0 +1 @@
uid://6isk1tmc2ik1
@@ -0,0 +1,40 @@
[gd_scene load_steps=3 format=3 uid="uid://cv122gw47cnun"]
[ext_resource type="Script" uid="uid://6isk1tmc2ik1" path="res://game/menu/weapon_selection/weapon_selection_screen.gd" id="1_hl2ql"]
[ext_resource type="Theme" uid="uid://dtnd3tqllufey" path="res://styles/menu_theme.tres" id="2_mi7cn"]
[node name="WeaponSelectionScreen" 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_hl2ql")
[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 0
offset_right = 640.0
offset_bottom = 360.0
theme_override_constants/margin_left = 16
theme_override_constants/margin_top = 16
theme_override_constants/margin_right = 16
theme_override_constants/margin_bottom = 16
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
layout_mode = 2
alignment = 1
[node name="Label" type="Label" parent="MarginContainer/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 0
theme = ExtResource("2_mi7cn")
text = "Select your first weapon"
[node name="WeaponSelectors" type="HBoxContainer" parent="MarginContainer/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 4
alignment = 1
@@ -0,0 +1,28 @@
class_name WeaponSelector
extends Control
signal weapon_selected(weapon_data: WeaponData)
@export var weapon_data: WeaponData:
set = _set_weapon_data
@onready var button : Button = $%Button
@onready var name_label : Label = $%NameLabel
@onready var description_label : Label = $%DescriptionLabel
func _set_weapon_data(data: WeaponData) -> void:
weapon_data = data
if weapon_data == null: return
name_label.text = weapon_data.name
description_label.text = weapon_data.description
func _on_button_pressed() -> void:
SoundManager.play_ui_stream(SoundManager.ui_stream_accept)
weapon_selected.emit(weapon_data)
@@ -0,0 +1 @@
uid://beiydi6w6gqqc
@@ -0,0 +1,48 @@
[gd_scene load_steps=4 format=3 uid="uid://1o3idp54lil2"]
[ext_resource type="Script" uid="uid://beiydi6w6gqqc" path="res://game/menu/weapon_selection/weapon_selector.gd" id="1_4acia"]
[ext_resource type="Theme" uid="uid://dtnd3tqllufey" path="res://styles/menu_theme.tres" id="2_g83h6"]
[ext_resource type="Theme" uid="uid://bqqtjq710luul" path="res://styles/label_theme.tres" id="3_dsio1"]
[node name="WeaponSelector" type="MarginContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_right = -440.0
offset_bottom = -240.0
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/margin_left = 10
theme_override_constants/margin_top = 10
theme_override_constants/margin_right = 10
theme_override_constants/margin_bottom = 10
script = ExtResource("1_4acia")
[node name="VBoxContainer" type="VBoxContainer" parent="."]
layout_mode = 2
[node name="NameLabel" type="Label" parent="VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
theme = ExtResource("2_g83h6")
horizontal_alignment = 1
[node name="DescriptionLabel" type="Label" parent="VBoxContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(160, 0)
layout_mode = 2
theme = ExtResource("3_dsio1")
theme_override_font_sizes/font_size = 8
horizontal_alignment = 1
[node name="Control" type="Control" parent="VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3
[node name="Button" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
theme = ExtResource("2_g83h6")
text = "Select"
[connection signal="pressed" from="VBoxContainer/Button" to="." method="_on_button_pressed"]