Sound effects

This commit is contained in:
Felipe M 2021-05-23 00:38:30 +02:00
parent 6ea21b3181
commit 6671b455c4
Signed by: fmartingr
GPG key ID: 716BC147715E716F
17 changed files with 94 additions and 6 deletions

View file

@ -0,0 +1,33 @@
extends Node
onready var soundPlayers = get_children()
const SOUNDS_PATH = "res://Assets/Music and Sounds/"
const DEFAULT_VOLUME_DB = -20
const DEFAULT_PITCH_SCALE = 1
var cache = {}
func load_sound(filename):
var path = SOUNDS_PATH + filename
if not (filename in cache):
cache[filename] = load(path)
return cache[filename]
func play_fx(sound, pitch_scale = DEFAULT_PITCH_SCALE, volume_db = DEFAULT_VOLUME_DB):
filename = sound + ".wav"
play(load_sound(filename), pitch_scale, volume_db)
func play_music(sound, pitch_scale = DEFAULT_PITCH_SCALE, volume_db = DEFAULT_VOLUME_DB):
filename = sound + ".ogg"
play(load_sound(filename), pitch_scale, volume_db)
func play(sound_res, pitch_scale, volume_db):
for soundPlayer in soundPlayers:
if not soundPlayer.playing:
soundPlayer.pitch_scale = pitch_scale
soundPlayer.volume_db = volume_db
soundPlayer.stream = sound_res
soundPlayer.play()
return
print("Too many sounds")

View file

@ -0,0 +1,27 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://Scenes/Music/SoundFX.gd" type="Script" id=1]
[node name="SoundFX" type="Node"]
pause_mode = 2
script = ExtResource( 1 )
[node name="AudioStreamPlayer1" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer2" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer3" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer4" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer5" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer6" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer7" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer8" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer9" type="AudioStreamPlayer" parent="."]
[node name="AudioStreamPlayer10" type="AudioStreamPlayer" parent="."]