Add Sound Manager

pull/15/head
wadeaston 2025-04-03 13:15:49 +07:00
parent 6f6b2716bb
commit 748b58e045
6 changed files with 75 additions and 5 deletions

@ -18,6 +18,10 @@ boot_splash/bg_color=Color(0.92549, 0.92549, 0.960784, 1)
boot_splash/image="res://splash-screen.png"
config/icon="res://icon.png"
[autoload]
SoundManager="*res://scripts/sound_manager.gd"
[display]
window/size/viewport_width=1920

@ -18,3 +18,5 @@ shape = SubResource("CapsuleShape3D_8rf01")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0530187, 0, 0.0432749)
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
[editable path="character-female-d2"]

@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=3 uid="uid://cgk66f6rg03mj"]
[gd_scene load_steps=11 format=3 uid="uid://cgk66f6rg03mj"]
[ext_resource type="Script" path="res://scripts/hud_manager.gd" id="1_6vdxc"]
[ext_resource type="Texture2D" uid="uid://hendpftbt4iw" path="res://sprites/population_icon.png" id="2_28oy1"]
@ -6,6 +6,7 @@
[ext_resource type="FontFile" uid="uid://d0cxd77jybrcn" path="res://fonts/lilita_one_regular.ttf" id="4_qfmf5"]
[ext_resource type="Texture2D" uid="uid://bng0d1sou7c8a" path="res://sprites/coin.png" id="5_vd1bj"]
[ext_resource type="Texture2D" uid="uid://cgpwknjrqnuae" path="res://sprites/help_icon.png" id="6_i1y88"]
[ext_resource type="Texture2D" uid="uid://bwp2j1v3vnqbf" path="res://sprites/unmuted.png" id="7_80m3c"]
[sub_resource type="LabelSettings" id="LabelSettings_q176i"]
font = ExtResource("4_qfmf5")
@ -119,6 +120,25 @@ color = Color(1, 0, 0, 1)
layout_mode = 2
theme_override_constants/separation = 10
[node name="SoundItem" type="HBoxContainer" parent="HBoxContainer"]
layout_mode = 2
mouse_filter = 0
theme_override_constants/separation = 8
[node name="SoundButton" type="TextureButton" parent="HBoxContainer/SoundItem"]
custom_minimum_size = Vector2(40, 40)
layout_mode = 2
size_flags_vertical = 4
mouse_filter = 0
mouse_default_cursor_shape = 2
texture_normal = ExtResource("7_80m3c")
ignore_texture_size = true
stretch_mode = 5
[node name="Separator4" type="VSeparator" parent="HBoxContainer"]
layout_mode = 2
theme_override_constants/separation = 10
[node name="HelpItem" type="HBoxContainer" parent="HBoxContainer"]
layout_mode = 2
mouse_filter = 0
@ -188,4 +208,5 @@ autowrap_mode = 3
[connection signal="mouse_exited" from="HBoxContainer/PopulationItem/PopulationIcon" to="." method="_on_population_icon_mouse_exited"]
[connection signal="mouse_entered" from="HBoxContainer/ElectricityItem/ElectricityIcon" to="." method="_on_electricity_icon_mouse_entered"]
[connection signal="mouse_exited" from="HBoxContainer/ElectricityItem/ElectricityIcon" to="." method="_on_electricity_icon_mouse_exited"]
[connection signal="pressed" from="HBoxContainer/HelpItem/HelpButton" to="." method="_on_help_button_pressed"]
[connection signal="pressed" from="HBoxContainer/SoundItem/SoundButton" to="." method="_on_sound_button_pressed"]
[connection signal="pressed" from="HBoxContainer/HelpItem/HelpButton" to="." method="_on_help_button_pressed"]

@ -1,4 +1,4 @@
[gd_scene load_steps=26 format=3 uid="uid://vgwrcfy1qawf"]
[gd_scene load_steps=27 format=3 uid="uid://vgwrcfy1qawf"]
[ext_resource type="Script" path="res://scripts/builder.gd" id="1_jybm7"]
[ext_resource type="Environment" uid="uid://jbptgqvstei3" path="res://scenes/main-environment.tres" id="1_yndf3"]
@ -21,6 +21,7 @@
[ext_resource type="PackedScene" uid="uid://cgk66f6rg03mj" path="res://scenes/hud.tscn" id="18_hud"]
[ext_resource type="PackedScene" uid="uid://bqjnp7uypupog" path="res://scenes/controls_panel.tscn" id="19_controls"]
[ext_resource type="Script" path="res://scripts/game_manager.gd" id="20_game_manager"]
[ext_resource type="PackedScene" path="res://scenes/sound_panel.tscn" id="21_sound_panel"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_mission"]
bg_color = Color(0.145098, 0.172549, 0.231373, 0.941176)
@ -120,6 +121,8 @@ shadow_opacity = 0.75
[node name="ControlsPanel" parent="CanvasLayer" instance=ExtResource("19_controls")]
[node name="SoundPanel" parent="CanvasLayer" instance=ExtResource("21_sound_panel")]
[node name="MissionManager" type="Node" parent="." node_paths=PackedStringArray("mission_ui", "builder")]
script = ExtResource("10_oe3re")
missions = Array[ExtResource("11_msovb")]([ExtResource("12_ms7i7"), ExtResource("13_s13s0"), ExtResource("14_bnke0"), ExtResource("15_plrw2"), ExtResource("16_5fmk3")])

@ -7,12 +7,14 @@ var building_sfx: AudioStreamPlayer
var construction_sfx: AudioStreamPlayer
func _ready():
# Reference to the controls panel and HUD
# Reference to the controls panel, sound panel, and HUD
var controls_panel = $CanvasLayer/ControlsPanel
var sound_panel = $CanvasLayer/SoundPanel
var hud = $CanvasLayer/HUD
# Set up the HUD's reference to the controls panel
# Set up the HUD's reference to the panels
hud.controls_panel = controls_panel
hud.sound_panel = sound_panel
# Auto-show controls at start
if controls_panel:
@ -33,11 +35,37 @@ func _ready():
# Connect to construction signals via deferred call to make sure everything is ready
call_deferred("_setup_construction_signals")
# Make sure sound buses are properly configured
call_deferred("_setup_sound_buses")
# This function is called when the controls panel is closed
func _on_controls_panel_closed():
print("Controls panel closed by player")
# Function to set up the sound buses
func _setup_sound_buses():
# Wait a moment to ensure SoundManager is ready
await get_tree().process_frame
# Get reference to SoundManager singleton
var sound_manager = get_node_or_null("/root/SoundManager")
if !sound_manager:
print("ERROR: SoundManager singleton not found!")
return
# Move audio players to the appropriate buses
if music_player:
music_player.bus = "Music"
if building_sfx:
building_sfx.bus = "SFX"
if construction_sfx:
construction_sfx.bus = "SFX"
print("Sound buses configured successfully")
# Setup background music player
func setup_background_music():
music_player = AudioStreamPlayer.new()
@ -51,6 +79,7 @@ func setup_background_music():
music_player.stream = music
music_player.volume_db = -12 # 25% volume (approx)
music_player.bus = "Music" # Use the Music bus
music_player.play()
print("Playing background music: jazz_new_orleans.mp3")
else:
@ -65,6 +94,7 @@ func setup_building_sfx():
if sfx:
building_sfx.stream = sfx
building_sfx.volume_db = -5
building_sfx.bus = "SFX" # Use the SFX bus
print("Building placement SFX loaded successfully")
else:
print("ERROR: Could not load building placement SFX")
@ -78,6 +108,7 @@ func setup_construction_sfx():
if sfx:
construction_sfx.stream = sfx
construction_sfx.volume_db = -5 # Louder volume
construction_sfx.bus = "SFX" # Use the SFX bus
print("Construction SFX loaded successfully")
else:
print("ERROR: Could not load construction SFX")

@ -17,6 +17,7 @@ var electricity_indicator: ColorRect
var population_tooltip: Control
var electricity_tooltip: Control
var controls_panel: PanelContainer
var sound_panel: PanelContainer
func _ready():
# Connect to signals from the builder
@ -171,6 +172,14 @@ func _on_electricity_icon_mouse_exited():
if electricity_tooltip:
electricity_tooltip.visible = false
# Called when the sound button is pressed
func _on_sound_button_pressed():
# Consume the event to prevent click-through to the world
get_viewport().set_input_as_handled()
if sound_panel:
sound_panel.show_panel()
# Called when the help button is pressed
func _on_help_button_pressed():
# Consume the event to prevent click-through to the world