Basic AudioManager script. Cross fade music + 5 SFX tracks

This commit is contained in:
Sagi Dayan 2020-09-28 17:18:07 -04:00
parent 5f5cfab688
commit a54d7ea5ac
25 changed files with 313 additions and 8 deletions

Binary file not shown.

View file

@ -0,0 +1,15 @@
[remap]
importer="ogg_vorbis"
type="AudioStreamOGGVorbis"
path="res://.import/Dungeon - Abandoned One.ogg-51af075d5753272ddbceec00d00cf6e8.oggstr"
[deps]
source_file="res://assets/Audio/Dungeon - Abandoned One.ogg"
dest_files=[ "res://.import/Dungeon - Abandoned One.ogg-51af075d5753272ddbceec00d00cf6e8.oggstr" ]
[params]
loop=true
loop_offset=0

Binary file not shown.

View file

@ -0,0 +1,15 @@
[remap]
importer="ogg_vorbis"
type="AudioStreamOGGVorbis"
path="res://.import/Dungeon - Altus Stratum.ogg-6941e562b8e4df418f0169fbf2aab21f.oggstr"
[deps]
source_file="res://assets/Audio/Dungeon - Altus Stratum.ogg"
dest_files=[ "res://.import/Dungeon - Altus Stratum.ogg-6941e562b8e4df418f0169fbf2aab21f.oggstr" ]
[params]
loop=true
loop_offset=0

BIN
assets/Audio/SFX/coin.wav Normal file

Binary file not shown.

View file

@ -0,0 +1,21 @@
[remap]
importer="wav"
type="AudioStreamSample"
path="res://.import/coin.wav-3937161d9a68071780cbd3299538d439.sample"
[deps]
source_file="res://assets/Audio/SFX/coin.wav"
dest_files=[ "res://.import/coin.wav-3937161d9a68071780cbd3299538d439.sample" ]
[params]
force/8_bit=false
force/mono=false
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop=false
compress/mode=0

Binary file not shown.

View file

@ -0,0 +1,21 @@
[remap]
importer="wav"
type="AudioStreamSample"
path="res://.import/player_attack.wav-02e9869840ca69a46806f573e4f8dab6.sample"
[deps]
source_file="res://assets/Audio/SFX/player_attack.wav"
dest_files=[ "res://.import/player_attack.wav-02e9869840ca69a46806f573e4f8dab6.sample" ]
[params]
force/8_bit=false
force/mono=false
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop=false
compress/mode=0

Binary file not shown.

View file

@ -0,0 +1,21 @@
[remap]
importer="wav"
type="AudioStreamSample"
path="res://.import/menu_move_sound.wav-233c5d77d3465d46155690e46beb9321.sample"
[deps]
source_file="res://assets/Audio/UI_Sounds/menu_move_sound.wav"
dest_files=[ "res://.import/menu_move_sound.wav-233c5d77d3465d46155690e46beb9321.sample" ]
[params]
force/8_bit=false
force/mono=false
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop=false
compress/mode=0

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View file

@ -57,6 +57,10 @@ boot_splash/use_filter=false
boot_splash/bg_color=Color( 0.141176, 0.141176, 0.141176, 1 )
config/icon="res://icon.png"
[autoload]
AudioManager="*res://src/Singletons/AudioManager.tscn"
[display]
window/size/width=480

View file

@ -163,6 +163,7 @@ func update_sprite(_direction:Vector2) -> void:
return
if attack:
_is_attaking = true
AudioManager.play_sfx(AudioManager.Sfx.PLAYER_ATTACK)
var attack_animation = "attack_right" if $Sprite.scale.x > 0 else "attack_left"
$AnimationPlayer.play(attack_animation)
$AnimationPlayer.playback_speed = 2.5

View file

@ -5,6 +5,8 @@ export var next_scene:PackedScene
func _ready() -> void:
$SplashVoice.seek(1.5)
AudioManager.play_music(AudioManager.Music.Intro, 2)
func load_next_scene():
# var next_scene = load("res://src/Levels/LevelTemplate/LevelTemplate.tscn")
@ -13,3 +15,4 @@ func load_next_scene():
func stop_splash_voice():
$SplashVoice.stop()

View file

@ -139,6 +139,7 @@ tracks/9/keys = {
}
[node name="Intro" type="Node2D"]
pause_mode = 2
script = ExtResource( 2 )
next_scene = ExtResource( 5 )
@ -151,16 +152,15 @@ texture = ExtResource( 1 )
centered = false
[node name="SAGI" type="Sprite" parent="."]
self_modulate = Color( 1, 1, 1, 0 )
self_modulate = Color( 1, 1, 1, 0.5 )
position = Vector2( 2, 0 )
texture = ExtResource( 3 )
centered = false
[node name="Light2D" type="Light2D" parent="SAGI"]
position = Vector2( 239, 136 )
enabled = false
texture = ExtResource( 3 )
texture_scale = 3.0
texture_scale = 2.0
[node name="SplashVoice" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 4 )
@ -183,4 +183,5 @@ centered = false
[node name="Light2D" type="Light2D" parent="Title/Sprite2"]
position = Vector2( 42, 68.4516 )
scale = Vector2( 3.50556, 3.50556 )
enabled = false
texture = ExtResource( 8 )

View file

@ -9,6 +9,7 @@ signal collected
func _on_body_entered(body: Node) -> void:
if body.name == "Player":
$Label.text = str(value)
AudioManager.play_sfx(AudioManager.Sfx.COIN_COLLECTION)
emit_signal("collected", value)
$AnimationPlayer.play("collected")
pass # Replace with function body.

View file

@ -190,11 +190,11 @@ collision_layer = 16
script = ExtResource( 2 )
[node name="Sprite" type="Sprite" parent="."]
position = Vector2( 0, -4.56314 )
position = Vector2( 0, -4 )
texture = ExtResource( 1 )
vframes = 2
hframes = 6
frame = 7
frame = 6
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
visible = false

View file

@ -12,6 +12,7 @@ func _physics_process(delta: float) -> void:
_update_camera(delta)
func _ready() -> void:
AudioManager.play_music(AudioManager.Music.Abandon)
for coin in $Coins.get_children():
coin.connect("collected", self, "_on_coin_collected")
for checkpoint in $Checkpoints.get_children():

View file

@ -9,6 +9,7 @@ func _on_ExitToMainMenuBtn_pressed() -> void:
$AnimationPlayer.play_backwards("fade")
yield($AnimationPlayer, "animation_finished");
var next_scene = load("res://src/Menu/MainMenu.tscn")
AudioManager.play(AudioManager.Music.Intro, .2)
get_tree().change_scene_to(next_scene)
queue_free()

View file

@ -3,7 +3,6 @@ extends Node2D
func _ready() -> void:
$CanvasLayer/Control/VBoxContainer/NewGameBtn.grab_focus()
func _on_ExitGameBtn_pressed() -> void:
get_tree().quit(0)
pass # Replace with function body.
@ -18,3 +17,7 @@ func _on_NewGameBtn_pressed() -> void:
var next_scene = load("res://src/Levels/LevelTemplate/LevelTemplate.tscn")
get_tree().change_scene_to(next_scene)
queue_free()
func _input(event: InputEvent) -> void:
if Input.is_action_just_pressed("ui_up") or Input.is_action_just_pressed("ui_down"):
AudioManager.play_sfx(AudioManager.Sfx.UI_MOVE)

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=2]
[gd_scene load_steps=16 format=2]
[ext_resource path="res://assets/Tiles/background_0.png" type="Texture" id=1]
[ext_resource path="res://assets/Tiles/background_2.png" type="Texture" id=2]
@ -8,6 +8,43 @@
[ext_resource path="res://src/Menu/MainMenu.gd" type="Script" id=6]
[ext_resource path="res://assets/Theme/menu_font.tres" type="DynamicFont" id=7]
[sub_resource type="Gradient" id=2]
colors = PoolColorArray( 0.996094, 0.996094, 0.996094, 1, 1, 1, 1, 0 )
[sub_resource type="GradientTexture" id=3]
gradient = SubResource( 2 )
[sub_resource type="Curve" id=7]
min_value = -200.0
max_value = 200.0
_data = [ Vector2( 0, 7.83957 ), 0.0, 166.434, 0, 0, Vector2( 1, 44.2032 ), -4.82953, 0.0, 0, 0 ]
[sub_resource type="CurveTexture" id=8]
curve = SubResource( 7 )
[sub_resource type="Curve" id=4]
_data = [ Vector2( 0, 0.285227 ), 0.0, 0.0, 0, 0, Vector2( 1, 1 ), 0.0, 0.0, 0, 0 ]
[sub_resource type="CurveTexture" id=5]
curve = SubResource( 4 )
[sub_resource type="ParticlesMaterial" id=6]
emission_shape = 2
emission_box_extents = Vector3( 10, 1, 1 )
flag_disable_z = true
direction = Vector3( 0, -1, 0 )
spread = 40.0
gravity = Vector3( 0, 0, 0 )
initial_velocity = 20.0
initial_velocity_random = 0.7
orbit_velocity = 0.0
orbit_velocity_random = 0.0
linear_accel_curve = SubResource( 8 )
scale = 3.0
scale_random = 1.0
scale_curve = SubResource( 5 )
color_ramp = SubResource( 3 )
[sub_resource type="Animation" id=1]
resource_name = "main"
length = 6.0
@ -117,6 +154,14 @@ color = Color( 0.762092, 0.913725, 0.398693, 1 )
range_item_cull_mask = 3
shadow_enabled = true
[node name="Particles2D" type="Particles2D" parent="Node2D"]
position = Vector2( 87, 175.5 )
amount = 50
lifetime = 2.5
speed_scale = 0.4
explosiveness = 0.1
process_material = SubResource( 6 )
[node name="Sprite3" type="Sprite" parent="Node2D"]
light_mask = 4
position = Vector2( 0, -4.5 )

View file

@ -4,16 +4,21 @@ signal dismissed
func _ready() -> void:
$CanvasLayer/VBoxContainer/ResumeBtn.grab_focus()
AudioManager.set_volume()
func dismiss()->void:
_on_ResumeBtn_pressed()
func _input(event: InputEvent) -> void:
if Input.is_action_just_pressed("pause"):
_on_ResumeBtn_pressed()
return
if Input.is_action_just_pressed("ui_up") or Input.is_action_just_pressed("ui_down"):
AudioManager.play_sfx(AudioManager.Sfx.UI_MOVE)
func _on_ResumeBtn_pressed() -> void:
$AnimationPlayer.play_backwards("fade")
AudioManager.set_volume(.2, AudioManager._DESIRED_VOLUME)
yield($AnimationPlayer, "animation_finished");
emit_signal("dismissed")
queue_free()
@ -25,6 +30,7 @@ func _on_ExitToMainMenuBtn_pressed() -> void:
yield($AnimationPlayer, "animation_finished");
var next_scene = load("res://src/Menu/MainMenu.tscn")
emit_signal("dismissed")
AudioManager.play_music(AudioManager.Music.Intro, .2)
get_tree().change_scene_to(next_scene)
queue_free()
@ -35,3 +41,8 @@ func _on_NewGameBtn_pressed() -> void:
emit_signal("dismissed")
get_tree().reload_current_scene()
pass # Replace with function body.
func _on_btn_focus_entered() -> void:
pass # Replace with function body.

View file

@ -134,6 +134,7 @@ text = "Exit To Main Menu"
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
autoplay = "fade"
anims/fade = SubResource( 2 )
[connection signal="focus_entered" from="CanvasLayer/VBoxContainer/ResumeBtn" to="." method="_on_btn_focus_entered"]
[connection signal="pressed" from="CanvasLayer/VBoxContainer/ResumeBtn" to="." method="_on_ResumeBtn_pressed"]
[connection signal="pressed" from="CanvasLayer/VBoxContainer/NewGameBtn" to="." method="_on_NewGameBtn_pressed"]
[connection signal="pressed" from="CanvasLayer/VBoxContainer/ExitToMainMenuBtn" to="." method="_on_ExitToMainMenuBtn_pressed"]

View file

@ -0,0 +1,111 @@
extends Node
onready var _current_music_player = $MusicPlayer1
onready var _next_music_player= $MusicPlayer2
var _SILENT = -40.0
var _DESIRED_VOLUME = -15.0
var _DESIRED_SFX_VOLUME = -10.0
var _DESIRED_PAUSE_VOLUME = -25.0
enum Music{
Intro,
Abandon
}
var _music_files := {
Music.Intro: load("res://assets/Audio/Dungeon - Altus Stratum.ogg"),
Music.Abandon: load("res://assets/Audio/Dungeon - Abandoned One.ogg")
}
enum Sfx{
COIN_COLLECTION,
PLAYER_JUMP,
PLAYER_LAND,
PLAYER_DASH,
PLAYER_ATTACK,
PLAYER_DIE,
UI_MOVE,
UI_SELECT
}
var _sfx_files := {
Sfx.COIN_COLLECTION : load("res://assets/Audio/SFX/coin.wav"),
Sfx.PLAYER_JUMP : '',
Sfx.PLAYER_LAND : '',
Sfx.PLAYER_DASH : '',
Sfx.PLAYER_ATTACK : load("res://assets/Audio/SFX/player_attack.wav"),
Sfx.PLAYER_DIE : '',
Sfx.UI_MOVE : load("res://assets/Audio/UI_Sounds/menu_move_sound.wav"),
Sfx.UI_SELECT : '',
}
var _current_music = null
var _next_music = null
func _ready() -> void:
_current_music_player.volume_db = _SILENT
_next_music_player.volume_db = _SILENT
var children = $SfxPlayers.get_children()
for child in children:
var player:AudioStreamPlayer = child
player.volume_db = _DESIRED_SFX_VOLUME
func play_music(music:int, transition_time:float = .5) -> void:
_transition_to(music, transition_time)
pass
func set_volume(duration:float=.2, volume:float = _DESIRED_PAUSE_VOLUME):
_current_music_player.get_node("Tween").stop_all()
_next_music_player.get_node("Tween").stop_all()
_current_music_player.get_node("Tween").interpolate_property(_current_music_player, 'volume_db', _current_music_player.volume_db, volume, duration)
_current_music_player.get_node("Tween").start()
func play_sfx(sfx:int, priority:int=0):
var sfx_player = _get_available_sfx_player()
if sfx_player != null:
sfx_player.stream = _sfx_files[sfx]
sfx_player.play()
pass
func _transition_to(music:int, duration:float):
if _current_music == null:
_play_first_music(music, duration)
return
_next_music = music
_next_music_player.stream = _music_files[music]
_next_music_player.play()
_current_music_player.get_node("Tween").stop_all()
_next_music_player.get_node("Tween").stop_all()
_current_music_player.get_node("Tween").interpolate_property(_current_music_player, 'volume_db', _current_music_player.volume_db, _SILENT, duration)
_next_music_player.get_node("Tween").interpolate_property(_next_music_player, 'volume_db', _next_music_player.volume_db, _DESIRED_VOLUME, duration)
_current_music_player.get_node("Tween").start()
_next_music_player.get_node("Tween").start()
func _play_first_music(music:int, duration:float):
_current_music = music
_current_music_player.stream = _music_files[music]
_current_music_player.play()
_current_music_player.get_node("Tween").interpolate_property(_current_music_player, 'volume_db', _current_music_player.volume_db, _DESIRED_VOLUME, duration)
_current_music_player.get_node("Tween").start()
func _get_available_sfx_player()->AudioStreamPlayer:
var children = $SfxPlayers.get_children()
for child in children:
var player:AudioStreamPlayer = child
if not player.playing: return player
return null
func _on_Tween_completed(object: Object, key: NodePath) -> void:
if object.volume_db == _DESIRED_VOLUME and _next_music != null:
var tmp = _current_music_player
_current_music_player = _next_music_player
_next_music_player = tmp
_next_music_player.stop()
_current_music = _next_music
_next_music = null
pass # Replace with function body.

View file

@ -0,0 +1,29 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://src/Singletons/AudioManager.gd" type="Script" id=1]
[node name="AudioManager" type="Node"]
pause_mode = 2
script = ExtResource( 1 )
[node name="MusicPlayer1" type="AudioStreamPlayer" parent="."]
[node name="Tween" type="Tween" parent="MusicPlayer1"]
[node name="MusicPlayer2" type="AudioStreamPlayer" parent="."]
[node name="Tween" type="Tween" parent="MusicPlayer2"]
[node name="SfxPlayers" type="Node" parent="."]
[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="SfxPlayers"]
[node name="AudioStreamPlayer2" type="AudioStreamPlayer" parent="SfxPlayers"]
[node name="AudioStreamPlayer3" type="AudioStreamPlayer" parent="SfxPlayers"]
[node name="AudioStreamPlayer4" type="AudioStreamPlayer" parent="SfxPlayers"]
[node name="AudioStreamPlayer5" type="AudioStreamPlayer" parent="SfxPlayers"]
[connection signal="tween_completed" from="MusicPlayer1/Tween" to="." method="_on_Tween_completed"]
[connection signal="tween_completed" from="MusicPlayer2/Tween" to="." method="_on_Tween_completed"]