Skip to content

Commit

Permalink
Merge pull request #79 from ligen131/feat-20240917-sound-manager
Browse files Browse the repository at this point in the history
feat(sound_manager): 添加 Sound Manager 类
  • Loading branch information
cutekibry authored Sep 17, 2024
2 parents 6ba2d29 + fd27fa5 commit de14a70
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 32 deletions.
15 changes: 15 additions & 0 deletions bus/default_bus_layout.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[gd_resource type="AudioBusLayout" format=3 uid="uid://cvlcglbd4fsie"]

[resource]
bus/1/name = &"bgm"
bus/1/solo = false
bus/1/mute = false
bus/1/bypass_fx = false
bus/1/volume_db = 0.0
bus/1/send = &"Master"
bus/2/name = &"sfx"
bus/2/solo = false
bus/2/mute = false
bus/2/bypass_fx = false
bus/2/volume_db = 0.0
bus/2/send = &"Master"
24 changes: 13 additions & 11 deletions levels/base_level/base_level.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
extends Node
class_name BaseLevel extends Node


class_name BaseLevel
@export var sfx_level_clear: AudioStream
@export var sfx_level_clear_db: float = 0.0
@export var sfx_wrong_answer: AudioStream
@export var sfx_wrong_answer_db: float = 0.0


# FIXME
# 由于 Godot preload 容易出错,此处改为 load
Expand All @@ -19,12 +23,10 @@ const MARGIN := 12
const CARDS_SEP := 34



var expr := ""
var req_pos := [] # Array[int],金色框的位置列表
var chap_id : int ## 当前章节编号
var lvl_id : int ## 当前关卡编号

var chap_id: int ## 当前章节编号
var lvl_id: int ## 当前关卡编号


func count(choices: String) -> Dictionary:
Expand Down Expand Up @@ -117,7 +119,7 @@ func _process(_delta):


func stage_clear() -> void:
$SFXs/LevelClear.play()
SoundManager.play_sfx(sfx_level_clear, sfx_level_clear_db)
for card_base: CardBase in $CardBases.get_children():
card_base.set_victory()

Expand All @@ -127,7 +129,7 @@ func stage_clear() -> void:

func _on_block_occupied_card_changed(_node) -> void:
var block_array = []
for block : Block in $Blocks.get_children():
for block: Block in $Blocks.get_children():
if block.is_empty():
#print(block.quest_pos, " is not occupied")
return
Expand All @@ -142,7 +144,7 @@ func _on_block_occupied_card_changed(_node) -> void:
# prints("info:", info)

if info[0] != "OK":
$SFXs/WrongAnswer.play()
SoundManager.play_sfx(sfx_wrong_answer, sfx_wrong_answer_db)

if info[0] == "INVALID":
for block: Block in $Blocks.get_children():
Expand All @@ -162,9 +164,9 @@ func _on_block_occupied_card_changed(_node) -> void:
# print(block_array)
for i in range(len(block_array)):
if i != 0:
if ExprValidator.is_smile(expr[i-1]+expr[i]):
if ExprValidator.is_smile(expr[i - 1] + expr[i]):
# print(expr[i-1]+expr[i])
block_array[i-1].set_color(ImageLib.get_palette_color_by_name("golden"))
block_array[i - 1].set_color(ImageLib.get_palette_color_by_name("golden"))
block_array[i].set_color(ImageLib.get_palette_color_by_name("golden"))

func _input(event: InputEvent):
Expand Down
3 changes: 3 additions & 0 deletions levels/base_level/base_level.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ animations = [{

[node name="BaseLevel" type="Node"]
script = ExtResource("1_diojb")
sfx_level_clear = ExtResource("3_ou3kh")
sfx_level_clear_db = -7.0
sfx_wrong_answer = ExtResource("4_yxsxp")

[node name="HUDs" type="Node" parent="."]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init(chapter_id: int, level_id : int, pos : Vector2, type : int) -> void :
func _on_pressed():
# print("choose: ",chap_id,"-",lvl_id)
# print("! ")
$SFXButtonDown.play()
SoundManager.play_sfx(self.sfx_button_down)
if button_type == 1:
enter_level.emit(chap_id, lvl_id)
else:
Expand Down
25 changes: 22 additions & 3 deletions levels/chapter_menu/level_menu/level_button/level_button.tscn
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
[gd_scene load_steps=4 format=3 uid="uid://bepyyenjtc0p3"]
[gd_scene load_steps=8 format=3 uid="uid://bepyyenjtc0p3"]

[ext_resource type="PackedScene" uid="uid://drl1xe103umpi" path="res://objects/styled_button/styled_button.tscn" id="1_4bgxw"]
[ext_resource type="Script" path="res://levels/chapter_menu/level_menu/level_button/level_button.gd" id="1_wwqfn"]
[ext_resource type="PackedScene" uid="uid://cvx7wowcbfo0r" path="res://objects/word/word.tscn" id="3_js2i2"]
[ext_resource type="Shader" path="res://objects/word/word.gdshader" id="4_6ctfu"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_c4861"]
resource_local_to_scene = true
shader = ExtResource("4_6ctfu")
shader_parameter/color = null

[sub_resource type="ShaderMaterial" id="ShaderMaterial_a2kcy"]
resource_local_to_scene = true
shader = ExtResource("4_6ctfu")
shader_parameter/color = null

[sub_resource type="ShaderMaterial" id="ShaderMaterial_y3jd6"]
resource_local_to_scene = true
shader = ExtResource("4_6ctfu")
shader_parameter/color = null

[node name="LevelButton" instance=ExtResource("1_4bgxw")]
script = ExtResource("1_wwqfn")

[node name="Word" parent="." index="0" instance=ExtResource("3_js2i2")]
material = SubResource("ShaderMaterial_c4861")
position = Vector2(17, 17)

[node name="Word1" parent="." index="2" instance=ExtResource("3_js2i2")]
[node name="Word1" parent="." index="1" instance=ExtResource("3_js2i2")]
visible = false
material = SubResource("ShaderMaterial_a2kcy")
position = Vector2(11, 17)

[node name="Word2" parent="." index="3" instance=ExtResource("3_js2i2")]
[node name="Word2" parent="." index="2" instance=ExtResource("3_js2i2")]
visible = false
material = SubResource("ShaderMaterial_y3jd6")
position = Vector2(23, 17)
6 changes: 5 additions & 1 deletion objects/card/card.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
class_name Card extends Area2D


@export var sfx_put_down : AudioStream ## 放下卡牌时的音效
@export var sfx_put_down_db : float = 0.0 ## 放下卡牌时的音效音量



var current_block: Block = null ## 当前所占用的 Block 对象。

Expand Down Expand Up @@ -63,7 +67,7 @@ func put_down():

self.last_block = null

$SFXPutDown.play()
SoundManager.play_sfx(sfx_put_down, sfx_put_down_db)


else:
Expand Down
2 changes: 2 additions & 0 deletions objects/card/card.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ size = Vector2(24, 24)
[node name="Card" type="Area2D"]
z_index = 50
script = ExtResource("1_0bviv")
sfx_put_down = ExtResource("7_u4ylf")
sfx_put_down_db = 4.0

[node name="CardBackSprite" type="AnimatedSprite2D" parent="."]
sprite_frames = SubResource("SpriteFrames_fo0r0")
Expand Down
6 changes: 3 additions & 3 deletions objects/styled_button/styled_button.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends Button
class_name StyledButton extends Button

class_name StyledButton
@export var sfx_button_down : AudioStream


func _on_pressed():
$SFXButtonDown.play()
SoundManager.play_sfx(self.sfx_button_down)
23 changes: 10 additions & 13 deletions objects/styled_button/styled_button.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
[ext_resource type="Script" path="res://objects/styled_button/styled_button.gd" id="3_rtcig"]
[ext_resource type="AudioStream" uid="uid://dpwscp4bw7pay" path="res://objects/styled_button/button_down.wav" id="4_no3m1"]

[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_c0uot"]
texture = ExtResource("1_64thl")
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_tfhr0"]

[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_filad"]
texture = ExtResource("3_840jk")
texture_margin_left = 6.0
texture_margin_top = 6.0
texture_margin_right = 6.0
Expand All @@ -20,27 +22,22 @@ texture_margin_top = 6.0
texture_margin_right = 6.0
texture_margin_bottom = 6.0

[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_filad"]
texture = ExtResource("3_840jk")
[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_c0uot"]
texture = ExtResource("1_64thl")
texture_margin_left = 6.0
texture_margin_top = 6.0
texture_margin_right = 6.0
texture_margin_bottom = 6.0

[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_tfhr0"]

[node name="StyledButton" type="Button"]
offset_right = 34.0
offset_bottom = 34.0
theme_override_styles/normal = SubResource("StyleBoxTexture_c0uot")
theme_override_styles/focus = SubResource("StyleBoxEmpty_tfhr0")
theme_override_styles/disabled = SubResource("StyleBoxTexture_filad")
theme_override_styles/hover = SubResource("StyleBoxTexture_eifmj")
theme_override_styles/pressed = SubResource("StyleBoxTexture_eifmj")
theme_override_styles/disabled = SubResource("StyleBoxTexture_filad")
theme_override_styles/focus = SubResource("StyleBoxEmpty_tfhr0")
theme_override_styles/normal = SubResource("StyleBoxTexture_c0uot")
script = ExtResource("3_rtcig")

[node name="SFXButtonDown" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource("4_no3m1")
volume_db = 10.306
sfx_button_down = ExtResource("4_no3m1")

[connection signal="pressed" from="." to="." method="_on_pressed"]
8 changes: 8 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.3", "Forward Plus")
config/icon="res://=P3.png"

[autoload]

SoundManager="*res://singletons/sound_manager/sound_manager.gd"

[debug]

gdscript/warnings/integer_division=0
Expand All @@ -27,6 +31,10 @@ window/stretch/mode="viewport"
window/stretch/scale_mode="integer"
mouse_cursor/custom_image="res://bg/cursor/cursor_arrow.png"

[editor_plugins]

enabled=PackedStringArray("res://addons/AsepriteWizard/plugin.cfg")

[gui]

theme/custom="res://themes/default_theme.tres"
Expand Down
24 changes: 24 additions & 0 deletions singletons/sound_manager/sound_manager.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extends Node

var base_sfx_db := 0.0
var base_bgm_db := 0.0

func play_sfx(sfx: AudioStream, db: float = 0.0):
var sfx_player := AudioStreamPlayer.new()
self.add_child(sfx_player)

sfx_player.stream = sfx
sfx_player.volume_db = db
sfx_player.bus = "sfx"
sfx_player.play()
sfx_player.finished.connect(sfx_player.queue_free.bind())

func play_bgm(bgm: AudioStream, db: float = 0.0):
var bgm_player := AudioStreamPlayer.new()
self.add_child(bgm_player)

bgm_player.stream = bgm
bgm_player.volume_db = db
bgm_player.bus = "bgm"
bgm_player.play()
bgm_player.loop = true

0 comments on commit de14a70

Please sign in to comment.