refactor structure
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
extends Node
|
||||
|
||||
@onready var back_button: Button = $BackButtonContainer/BackButton
|
||||
|
||||
func _ready() -> void:
|
||||
if not back_button.pressed.is_connected(_on_back_button_pressed):
|
||||
back_button.pressed.connect(_on_back_button_pressed)
|
||||
|
||||
func _on_back_button_pressed() -> void:
|
||||
AudioManager.play_ui_click()
|
||||
GameManager.save_game()
|
||||
GameManager.exit_to_main_menu()
|
||||
@@ -1 +0,0 @@
|
||||
uid://bvtr6yhlyuv4v
|
||||
27
scenes/game/game.gd
Normal file
27
scenes/game/game.gd
Normal file
@@ -0,0 +1,27 @@
|
||||
extends Node
|
||||
|
||||
@onready var back_button: Button = $BackButtonContainer/BackButton
|
||||
const MATCH3_SCENE_PATH := "res://scenes/match3/match3.tscn"
|
||||
var match3_instance: Node
|
||||
|
||||
func _ready() -> void:
|
||||
if not back_button.pressed.is_connected(_on_back_button_pressed):
|
||||
back_button.pressed.connect(_on_back_button_pressed)
|
||||
_load_match3_scene()
|
||||
|
||||
|
||||
func _on_back_button_pressed() -> void:
|
||||
AudioManager.play_ui_click()
|
||||
GameManager.save_game()
|
||||
if match3_instance and is_instance_valid(match3_instance):
|
||||
match3_instance.queue_free()
|
||||
GameManager.exit_to_main_menu()
|
||||
|
||||
func _load_match3_scene() -> void:
|
||||
var match3_scene := load(MATCH3_SCENE_PATH)
|
||||
if not match3_scene or not match3_scene is PackedScene:
|
||||
push_error("Failed to load Match3 scene at: %s" % MATCH3_SCENE_PATH)
|
||||
return
|
||||
|
||||
match3_instance = match3_scene.instantiate()
|
||||
add_child(match3_instance)
|
||||
@@ -1,6 +1,7 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://dmwkyeq2l7u04"]
|
||||
[gd_scene load_steps=3 format=3 uid="uid://dmwkyeq2l7u04"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bvtr6yhlyuv4v" path="res://scenes/game.gd" id="1_uwrxv"]
|
||||
[ext_resource type="Script" uid="uid://bvtr6yhlyuv4v" path="res://scenes/game/game.gd" id="1_uwrxv"]
|
||||
[ext_resource type="PackedScene" uid="uid://b4kv7g7kllwgb" path="res://scenes/match3/match3.tscn" id="2_yqjtg"]
|
||||
|
||||
[node name="Game" type="Node"]
|
||||
script = ExtResource("1_uwrxv")
|
||||
@@ -12,6 +13,9 @@ offset_right = 40.0
|
||||
offset_bottom = 40.0
|
||||
|
||||
[node name="BackButton" type="Button" parent="BackButtonContainer"]
|
||||
layout_mode = 0
|
||||
offset_right = 45.0
|
||||
offset_bottom = 31.0
|
||||
text = "back"
|
||||
|
||||
[node name="Match3" parent="." instance=ExtResource("2_yqjtg")]
|
||||
43
scenes/main/Main.gd
Normal file
43
scenes/main/Main.gd
Normal file
@@ -0,0 +1,43 @@
|
||||
extends Control
|
||||
|
||||
@onready var press_any_key_screen = $PressAnyKeyScreen
|
||||
var current_menu = null
|
||||
|
||||
const MAIN_MENU_SCENE = preload("res://scenes/ui/MainMenu.tscn")
|
||||
const SETTINGS_MENU_SCENE = preload("res://scenes/ui/SettingsMenu.tscn")
|
||||
|
||||
func _ready():
|
||||
print("Main scene ready")
|
||||
press_any_key_screen.any_key_pressed.connect(_on_any_key_pressed)
|
||||
|
||||
func _on_any_key_pressed():
|
||||
print("Transitioning to main menu")
|
||||
press_any_key_screen.queue_free()
|
||||
show_main_menu()
|
||||
|
||||
func show_main_menu():
|
||||
clear_current_menu()
|
||||
var main_menu = MAIN_MENU_SCENE.instantiate()
|
||||
main_menu.open_settings.connect(_on_open_settings)
|
||||
add_child(main_menu)
|
||||
current_menu = main_menu
|
||||
|
||||
func show_settings_menu():
|
||||
clear_current_menu()
|
||||
var settings_menu = SETTINGS_MENU_SCENE.instantiate()
|
||||
settings_menu.back_to_main_menu.connect(_on_back_to_main_menu)
|
||||
add_child(settings_menu)
|
||||
current_menu = settings_menu
|
||||
|
||||
func clear_current_menu():
|
||||
if current_menu:
|
||||
current_menu.queue_free()
|
||||
current_menu = null
|
||||
|
||||
func _on_open_settings():
|
||||
print("Opening settings menu")
|
||||
show_settings_menu()
|
||||
|
||||
func _on_back_to_main_menu():
|
||||
print("Back to main menu")
|
||||
show_main_menu()
|
||||
16
scenes/main/PressAnyKeyScreen.gd
Normal file
16
scenes/main/PressAnyKeyScreen.gd
Normal file
@@ -0,0 +1,16 @@
|
||||
extends Control
|
||||
|
||||
signal any_key_pressed
|
||||
|
||||
func _ready():
|
||||
print("PressAnyKeyScreen ready")
|
||||
update_text()
|
||||
|
||||
func _input(event):
|
||||
if event.is_action_pressed("any_key") or event is InputEventScreenTouch:
|
||||
print("Any key pressed: ", event)
|
||||
any_key_pressed.emit()
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
func update_text():
|
||||
$PressKeyContainer/PressKeyLabel.text = tr("press_ok_continue")
|
||||
134
scenes/main/PressAnyKeyScreen.tscn
Normal file
134
scenes/main/PressAnyKeyScreen.tscn
Normal file
@@ -0,0 +1,134 @@
|
||||
[gd_scene load_steps=15 format=3 uid="uid://gbe1jarrwqsi"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://buak21ajgvevl" path="res://scenes/main/PressAnyKeyScreen.gd" id="1_0a4p2"]
|
||||
[ext_resource type="Texture2D" uid="uid://bcr4bokw87m5n" path="res://assets/sprites/characters/skeleton/Skeleton Idle.png" id="2_rjjcb"]
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_l6pue"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(0, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_3h5mc"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(24, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_gqpp5"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(48, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_ff5uo"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(72, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_e7oxa"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(96, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_urgkb"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(120, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_xfg50"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(144, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_sy0v8"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(168, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_kfv8w"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(192, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_lvvje"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(216, 0, 24, 32)
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_vvh7w"]
|
||||
atlas = ExtResource("2_rjjcb")
|
||||
region = Rect2(240, 0, 24, 32)
|
||||
|
||||
[sub_resource type="SpriteFrames" id="SpriteFrames_wtrhp"]
|
||||
animations = [{
|
||||
"frames": [{
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_l6pue")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_3h5mc")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_gqpp5")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_ff5uo")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_e7oxa")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_urgkb")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_xfg50")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_sy0v8")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_kfv8w")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_lvvje")
|
||||
}, {
|
||||
"duration": 1.0,
|
||||
"texture": SubResource("AtlasTexture_vvh7w")
|
||||
}],
|
||||
"loop": true,
|
||||
"name": &"default",
|
||||
"speed": 5.0
|
||||
}]
|
||||
|
||||
[node name="PressAnyKeyScreen" type="Control" groups=["localizable"]]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_0a4p2")
|
||||
|
||||
[node name="PressKeyContainer" type="VBoxContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -80.5
|
||||
offset_top = -25.0
|
||||
offset_right = 80.5
|
||||
offset_bottom = 25.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
metadata/_edit_use_anchors_ = true
|
||||
|
||||
[node name="AspectRatioContainer" type="AspectRatioContainer" parent="PressKeyContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 4
|
||||
size_flags_vertical = 0
|
||||
alignment_horizontal = 0
|
||||
alignment_vertical = 0
|
||||
|
||||
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="PressKeyContainer/AspectRatioContainer"]
|
||||
sprite_frames = SubResource("SpriteFrames_wtrhp")
|
||||
autoplay = "default"
|
||||
offset = Vector2(0, -30)
|
||||
|
||||
[node name="TitleLabel" type="Label" parent="PressKeyContainer"]
|
||||
layout_mode = 2
|
||||
text = "Skelly"
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="PressKeyLabel" type="Label" parent="PressKeyContainer"]
|
||||
layout_mode = 2
|
||||
text = "`press_ok_continue`"
|
||||
@@ -1,8 +1,8 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://ci2gk11211n0d"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://cwlop1ettlqhg" path="res://scripts/Main.gd" id="1_0wfyh"]
|
||||
[ext_resource type="PackedScene" uid="uid://gbe1jarrwqsi" path="res://ui/PressAnyKeyScreen.tscn" id="1_o5qli"]
|
||||
[ext_resource type="Texture2D" uid="uid://c8y6tlvcgh2gn" path="res://resources/textures/beanstalk-dark.webp" id="2_sugp2"]
|
||||
[ext_resource type="Script" uid="uid://cwlop1ettlqhg" path="res://scenes/main/Main.gd" id="1_0wfyh"]
|
||||
[ext_resource type="PackedScene" uid="uid://gbe1jarrwqsi" path="res://scenes/main/PressAnyKeyScreen.tscn" id="1_o5qli"]
|
||||
[ext_resource type="Texture2D" uid="uid://c8y6tlvcgh2gn" path="res://assets/textures/backgrounds/beanstalk-dark.webp" id="2_sugp2"]
|
||||
|
||||
[node name="main" type="Control"]
|
||||
layout_mode = 3
|
||||
8
scenes/match3/Tile.tscn
Normal file
8
scenes/match3/Tile.tscn
Normal file
@@ -0,0 +1,8 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://bnk1gqom3oi6q"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://3bu5wgfarlgp" path="res://scenes/match3/tile.gd" id="1_tile_script"]
|
||||
|
||||
[node name="Tile" type="Node2D"]
|
||||
script = ExtResource("1_tile_script")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
102
scenes/match3/match3.gd
Normal file
102
scenes/match3/match3.gd
Normal file
@@ -0,0 +1,102 @@
|
||||
extends Node2D
|
||||
|
||||
const GRID_SIZE := Vector2i(8, 8)
|
||||
const TILE_TYPES := 5
|
||||
const TILE_SCENE := preload("res://scenes/match3/tile.tscn")
|
||||
|
||||
var grid := []
|
||||
|
||||
func _ready():
|
||||
_initialize_grid()
|
||||
|
||||
func _initialize_grid():
|
||||
for y in range(GRID_SIZE.y):
|
||||
grid.append([])
|
||||
for x in range(GRID_SIZE.x):
|
||||
var tile = TILE_SCENE.instantiate()
|
||||
tile.position = Vector2(x, y) * 64 # Adjust to your tile size
|
||||
tile.grid_position = Vector2i(x, y)
|
||||
tile.tile_type = randi() % TILE_TYPES
|
||||
add_child(tile)
|
||||
grid[y].append(tile)
|
||||
|
||||
func _has_match_at(pos: Vector2i) -> bool:
|
||||
var matches_horizontal = _get_match_line(pos, Vector2i(1, 0))
|
||||
if matches_horizontal.size() >= 3:
|
||||
return true
|
||||
|
||||
var matches_vertical = _get_match_line(pos, Vector2i(0, 1))
|
||||
return matches_vertical.size() >= 3
|
||||
|
||||
func _get_match_line(start: Vector2i, dir: Vector2i) -> Array:
|
||||
var line = [grid[start.y][start.x]]
|
||||
var type = grid[start.y][start.x].tile_type
|
||||
|
||||
for offset in [1, -1]:
|
||||
var current = start + dir * offset
|
||||
while current.x >= 0 and current.y >= 0 and current.x < GRID_SIZE.x and current.y < GRID_SIZE.y:
|
||||
var neighbor = grid[current.y][current.x]
|
||||
if neighbor.tile_type != type:
|
||||
break
|
||||
line.append(neighbor)
|
||||
current += dir * offset
|
||||
|
||||
return line
|
||||
|
||||
func _clear_matches():
|
||||
var to_clear := []
|
||||
|
||||
for y in range(GRID_SIZE.y):
|
||||
for x in range(GRID_SIZE.x):
|
||||
var pos = Vector2i(x, y)
|
||||
var horiz = _get_match_line(pos, Vector2i(1, 0))
|
||||
if horiz.size() >= 3:
|
||||
to_clear.append_array(horiz)
|
||||
var vert = _get_match_line(pos, Vector2i(0, 1))
|
||||
if vert.size() >= 3:
|
||||
to_clear.append_array(vert)
|
||||
|
||||
# Remove duplicates using dictionary keys trick
|
||||
var unique_dict := {}
|
||||
for tile in to_clear:
|
||||
unique_dict[tile] = true
|
||||
to_clear = unique_dict.keys()
|
||||
|
||||
for tile in to_clear:
|
||||
grid[tile.grid_position.y][tile.grid_position.x] = null
|
||||
tile.queue_free()
|
||||
|
||||
_drop_tiles()
|
||||
await get_tree().create_timer(0.2).timeout
|
||||
_fill_empty_cells()
|
||||
|
||||
func _drop_tiles():
|
||||
var moved = true
|
||||
while moved:
|
||||
moved = false
|
||||
for x in range(GRID_SIZE.x):
|
||||
for y in range(GRID_SIZE.y - 2, -1, -1):
|
||||
var tile = grid[y][x]
|
||||
if tile and not grid[y + 1][x]:
|
||||
grid[y + 1][x] = tile
|
||||
grid[y][x] = null
|
||||
tile.grid_position = Vector2i(x, y + 1)
|
||||
# You can animate position here using Tween for smooth drop:
|
||||
# tween.interpolate_property(tile, "position", tile.position, Vector2(x, y + 1) * 64, 0.2)
|
||||
tile.position = Vector2(x, y + 1) * 64
|
||||
moved = true
|
||||
|
||||
func _fill_empty_cells():
|
||||
for y in range(GRID_SIZE.y):
|
||||
for x in range(GRID_SIZE.x):
|
||||
if not grid[y][x]:
|
||||
var tile = TILE_SCENE.instantiate()
|
||||
tile.grid_position = Vector2i(x, y)
|
||||
tile.tile_type = randi() % TILE_TYPES
|
||||
tile.position = Vector2(x, y) * 64
|
||||
grid[y][x] = tile
|
||||
add_child(tile)
|
||||
|
||||
# Recheck matches
|
||||
await get_tree().create_timer(0.1).timeout
|
||||
_clear_matches()
|
||||
13
scenes/match3/match3.tscn
Normal file
13
scenes/match3/match3.tscn
Normal file
@@ -0,0 +1,13 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://b4kv7g7kllwgb"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bvyiwvpepqfdu" path="res://scenes/match3/match3.gd" id="1_mvfdp"]
|
||||
[ext_resource type="Texture2D" uid="uid://ccprr0qrj3lgm" path="res://assets/sprites/gems/rg_19.png" id="2_0fhn8"]
|
||||
|
||||
[node name="Match3" type="Node2D"]
|
||||
script = ExtResource("1_mvfdp")
|
||||
|
||||
[node name="GridContainer" type="Node2D" parent="."]
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
position = Vector2(584, 317)
|
||||
texture = ExtResource("2_0fhn8")
|
||||
33
scenes/match3/tile.gd
Normal file
33
scenes/match3/tile.gd
Normal file
@@ -0,0 +1,33 @@
|
||||
extends Node2D
|
||||
|
||||
@export var tile_type: int = 0
|
||||
var grid_position: Vector2i
|
||||
|
||||
@onready var sprite: Sprite2D = $Sprite2D
|
||||
|
||||
# Target size for each tile to fit in the 64x64 grid cells
|
||||
const TILE_SIZE = 48 # Slightly smaller than 64 to leave some padding
|
||||
|
||||
var tile_textures = [
|
||||
preload("res://assets/sprites/gems/bg_19.png"),
|
||||
preload("res://assets/sprites/gems/dg_19.png"),
|
||||
preload("res://assets/sprites/gems/gg_19.png"),
|
||||
preload("res://assets/sprites/gems/mg_19.png"),
|
||||
]
|
||||
|
||||
func _set_tile_type(value: int) -> void:
|
||||
tile_type = value
|
||||
if value >= 0 and value < tile_textures.size():
|
||||
sprite.texture = tile_textures[value]
|
||||
_scale_sprite_to_fit()
|
||||
|
||||
func _scale_sprite_to_fit() -> void:
|
||||
if sprite.texture:
|
||||
var texture_size = sprite.texture.get_size()
|
||||
var max_dimension = max(texture_size.x, texture_size.y)
|
||||
var scale_factor = TILE_SIZE / max_dimension
|
||||
sprite.scale = Vector2(scale_factor, scale_factor)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
_set_tile_type(tile_type)
|
||||
21
scenes/ui/MainMenu.gd
Normal file
21
scenes/ui/MainMenu.gd
Normal file
@@ -0,0 +1,21 @@
|
||||
extends Control
|
||||
|
||||
signal open_settings
|
||||
|
||||
func _ready():
|
||||
print("MainMenu ready")
|
||||
|
||||
func _on_new_game_button_pressed():
|
||||
AudioManager.play_ui_click()
|
||||
print("New Game pressed")
|
||||
GameManager.start_new_game()
|
||||
|
||||
func _on_settings_button_pressed():
|
||||
AudioManager.play_ui_click()
|
||||
print("Settings pressed")
|
||||
open_settings.emit()
|
||||
|
||||
func _on_exit_button_pressed():
|
||||
AudioManager.play_ui_click()
|
||||
print("Exit pressed")
|
||||
get_tree().quit()
|
||||
42
scenes/ui/MainMenu.tscn
Normal file
42
scenes/ui/MainMenu.tscn
Normal file
@@ -0,0 +1,42 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://m8lf3eh3al5j"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://ctu58xq7btp1n" path="res://scenes/ui/MainMenu.gd" id="1_b00nv"]
|
||||
|
||||
[node name="MainMenu" 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_b00nv")
|
||||
|
||||
[node name="MenuContainer" type="VBoxContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -20.0
|
||||
offset_top = -20.0
|
||||
offset_right = 20.0
|
||||
offset_bottom = 20.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="NewGameButton" type="Button" parent="MenuContainer"]
|
||||
layout_mode = 2
|
||||
text = "New Game"
|
||||
|
||||
[node name="SettingsButton" type="Button" parent="MenuContainer"]
|
||||
layout_mode = 2
|
||||
text = "Settings"
|
||||
|
||||
[node name="ExitButton" type="Button" parent="MenuContainer"]
|
||||
layout_mode = 2
|
||||
text = "Exit"
|
||||
|
||||
[connection signal="pressed" from="MenuContainer/NewGameButton" to="." method="_on_new_game_button_pressed"]
|
||||
[connection signal="pressed" from="MenuContainer/SettingsButton" to="." method="_on_settings_button_pressed"]
|
||||
[connection signal="pressed" from="MenuContainer/ExitButton" to="." method="_on_exit_button_pressed"]
|
||||
104
scenes/ui/SettingsMenu.gd
Normal file
104
scenes/ui/SettingsMenu.gd
Normal file
@@ -0,0 +1,104 @@
|
||||
extends Control
|
||||
|
||||
signal back_to_main_menu
|
||||
|
||||
@onready var master_slider = $SettingsContainer/MasterVolumeContainer/MasterVolumeSlider
|
||||
@onready var music_slider = $SettingsContainer/MusicVolumeContainer/MusicVolumeSlider
|
||||
@onready var sfx_slider = $SettingsContainer/SFXVolumeContainer/SFXVolumeSlider
|
||||
@onready var language_selector = $SettingsContainer/LanguageContainer/LanguageSelector
|
||||
|
||||
@export var settings_manager: Node = SettingsManager
|
||||
@export var localization_manager: Node = LocalizationManager
|
||||
|
||||
var language_codes = []
|
||||
|
||||
func _ready():
|
||||
add_to_group("localizable")
|
||||
print("SettingsMenu ready")
|
||||
setup_language_selector()
|
||||
|
||||
var master_callback = _on_volume_slider_changed.bind("master_volume")
|
||||
if not master_slider.value_changed.is_connected(master_callback):
|
||||
master_slider.value_changed.connect(master_callback)
|
||||
|
||||
var music_callback = _on_volume_slider_changed.bind("music_volume")
|
||||
if not music_slider.value_changed.is_connected(music_callback):
|
||||
music_slider.value_changed.connect(music_callback)
|
||||
|
||||
var sfx_callback = _on_volume_slider_changed.bind("sfx_volume")
|
||||
if not sfx_slider.value_changed.is_connected(sfx_callback):
|
||||
sfx_slider.value_changed.connect(sfx_callback)
|
||||
|
||||
if not language_selector.item_selected.is_connected(Callable(self, "_on_language_selector_item_selected")):
|
||||
language_selector.item_selected.connect(_on_language_selector_item_selected)
|
||||
|
||||
_update_controls_from_settings()
|
||||
update_text()
|
||||
|
||||
func _update_controls_from_settings():
|
||||
master_slider.value = settings_manager.get_setting("master_volume")
|
||||
music_slider.value = settings_manager.get_setting("music_volume")
|
||||
sfx_slider.value = settings_manager.get_setting("sfx_volume")
|
||||
|
||||
var current_lang = settings_manager.get_setting("language")
|
||||
var lang_index = language_codes.find(current_lang)
|
||||
if lang_index >= 0:
|
||||
language_selector.selected = lang_index
|
||||
|
||||
func _on_volume_slider_changed(value, setting_key):
|
||||
settings_manager.set_setting(setting_key, value)
|
||||
|
||||
func _exit_settings():
|
||||
print("Exiting settings")
|
||||
settings_manager.save_settings()
|
||||
back_to_main_menu.emit()
|
||||
|
||||
func _input(event):
|
||||
if event.is_action_pressed("ui_cancel") or event.is_action_pressed("ui_menu_toggle"):
|
||||
print("ESC pressed in settings")
|
||||
_exit_settings()
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
func _on_back_button_pressed():
|
||||
AudioManager.play_ui_click()
|
||||
print("Back button pressed")
|
||||
_exit_settings()
|
||||
|
||||
func setup_language_selector():
|
||||
var languages_data = settings_manager.get_languages_data()
|
||||
if languages_data.has("languages"):
|
||||
language_selector.clear()
|
||||
language_codes.clear()
|
||||
|
||||
for lang_code in languages_data.languages.keys():
|
||||
language_codes.append(lang_code)
|
||||
var display_name = languages_data.languages[lang_code]["display_name"]
|
||||
language_selector.add_item(display_name)
|
||||
|
||||
var current_lang = settings_manager.get_setting("language")
|
||||
var lang_index = language_codes.find(current_lang)
|
||||
if lang_index >= 0:
|
||||
language_selector.selected = lang_index
|
||||
|
||||
func _on_language_selector_item_selected(index: int):
|
||||
if index < language_codes.size():
|
||||
var selected_lang = language_codes[index]
|
||||
settings_manager.set_setting("language", selected_lang)
|
||||
print("Language changed to: ", selected_lang)
|
||||
localization_manager.change_language(selected_lang)
|
||||
|
||||
func update_text():
|
||||
$SettingsContainer/SettingsTitle.text = tr("settings_title")
|
||||
$SettingsContainer/MasterVolumeContainer/MasterVolume.text = tr("master_volume")
|
||||
$SettingsContainer/MusicVolumeContainer/MusicVolume.text = tr("music_volume")
|
||||
$SettingsContainer/SFXVolumeContainer/SFXVolume.text = tr("sfx_volume")
|
||||
$SettingsContainer/LanguageContainer/LanguageLabel.text = tr("language")
|
||||
$BackButtonContainer/BackButton.text = tr("back")
|
||||
|
||||
|
||||
func _on_reset_setting_button_pressed() -> void:
|
||||
AudioManager.play_ui_click()
|
||||
print("Resetting settings")
|
||||
settings_manager.reset_settings_to_defaults()
|
||||
_update_controls_from_settings()
|
||||
localization_manager.change_language(settings_manager.get_setting("language"))
|
||||
144
scenes/ui/SettingsMenu.tscn
Normal file
144
scenes/ui/SettingsMenu.tscn
Normal file
@@ -0,0 +1,144 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://57obmcwyos2g"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c8mlvlerv4y03" path="res://scenes/ui/SettingsMenu.gd" id="1_oqkcn"]
|
||||
|
||||
[node name="SettingsMenu" type="Control" groups=["localizable"]]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_oqkcn")
|
||||
|
||||
[node name="SettingsContainer" type="VBoxContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -34.5
|
||||
offset_top = -20.0
|
||||
offset_right = 34.5
|
||||
offset_bottom = 20.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="SettingsTitle" type="Label" parent="SettingsContainer"]
|
||||
custom_minimum_size = Vector2(300, 0)
|
||||
layout_mode = 2
|
||||
text = "Settings"
|
||||
|
||||
[node name="MasterVolumeContainer" type="HBoxContainer" parent="SettingsContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="MasterVolume" type="Label" parent="SettingsContainer/MasterVolumeContainer"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
text = "Master Volume"
|
||||
|
||||
[node name="MasterVolumeSlider" type="HSlider" parent="SettingsContainer/MasterVolumeContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
max_value = 1.0
|
||||
step = 0.1
|
||||
value = 0.5
|
||||
|
||||
[node name="MusicVolumeContainer" type="HBoxContainer" parent="SettingsContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="MusicVolume" type="Label" parent="SettingsContainer/MusicVolumeContainer"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
text = "Music Volume"
|
||||
|
||||
[node name="MusicVolumeSlider" type="HSlider" parent="SettingsContainer/MusicVolumeContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
max_value = 1.0
|
||||
step = 0.1
|
||||
value = 0.5
|
||||
|
||||
[node name="SFXVolumeContainer" type="HBoxContainer" parent="SettingsContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="SFXVolume" type="Label" parent="SettingsContainer/SFXVolumeContainer"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
text = "SFX Volume"
|
||||
|
||||
[node name="SFXVolumeSlider" type="HSlider" parent="SettingsContainer/SFXVolumeContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
max_value = 1.0
|
||||
step = 0.1
|
||||
value = 0.5
|
||||
|
||||
[node name="LanguageContainer" type="HBoxContainer" parent="SettingsContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 4
|
||||
|
||||
[node name="LanguageLabel" type="Label" parent="SettingsContainer/LanguageContainer"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
text = "Language"
|
||||
|
||||
[node name="LanguageSelector" type="OptionButton" parent="SettingsContainer/LanguageContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 4
|
||||
selected = 0
|
||||
item_count = 5
|
||||
popup/item_0/text = "English"
|
||||
popup/item_0/id = 0
|
||||
popup/item_1/text = "Español"
|
||||
popup/item_1/id = 1
|
||||
popup/item_2/text = "Français"
|
||||
popup/item_2/id = 2
|
||||
popup/item_3/text = "Deutsch"
|
||||
popup/item_3/id = 3
|
||||
popup/item_4/text = "Русский"
|
||||
popup/item_4/id = 2
|
||||
|
||||
[node name="BackButtonContainer" type="Control" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 0
|
||||
offset_right = 40.0
|
||||
offset_bottom = 40.0
|
||||
|
||||
[node name="BackButton" type="Button" parent="BackButtonContainer"]
|
||||
layout_mode = 0
|
||||
offset_right = 8.0
|
||||
offset_bottom = 8.0
|
||||
text = "back"
|
||||
|
||||
[node name="ResetSettingsContainer" type="Control" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 7
|
||||
anchor_left = 0.5
|
||||
anchor_top = 1.0
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -20.0
|
||||
offset_top = -40.0
|
||||
offset_right = 20.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 0
|
||||
|
||||
[node name="ResetSettingButton" type="Button" parent="ResetSettingsContainer"]
|
||||
layout_mode = 1
|
||||
anchors_preset = 5
|
||||
anchor_left = 0.5
|
||||
anchor_right = 0.5
|
||||
offset_left = -98.0
|
||||
offset_right = 98.0
|
||||
offset_bottom = 31.0
|
||||
grow_horizontal = 2
|
||||
text = "Reset settings to default"
|
||||
|
||||
[connection signal="value_changed" from="SettingsContainer/MasterVolumeContainer/MasterVolumeSlider" to="." method="_on_master_volume_changed"]
|
||||
[connection signal="value_changed" from="SettingsContainer/MusicVolumeContainer/MusicVolumeSlider" to="." method="_on_music_volume_changed"]
|
||||
[connection signal="value_changed" from="SettingsContainer/SFXVolumeContainer/SFXVolumeSlider" to="." method="_on_sfx_volume_changed"]
|
||||
[connection signal="item_selected" from="SettingsContainer/LanguageContainer/LanguageSelector" to="." method="_on_language_selector_item_selected"]
|
||||
[connection signal="pressed" from="BackButtonContainer/BackButton" to="." method="_on_back_button_pressed"]
|
||||
[connection signal="pressed" from="ResetSettingsContainer/ResetSettingButton" to="." method="_on_reset_setting_button_pressed"]
|
||||
Reference in New Issue
Block a user