Ouroboros/Scripts/sound_player.gd

30 lines
985 B
GDScript

extends Node
const sounds : Dictionary[String, AudioStream] = {"click": preload("res://Audio/Effects_r1_Click.wav"),
"die": preload("res://Audio/Effects_r1_Die.wav"),
"eat": preload("res://Audio/Effects_r1_Eat.wav"),
"move": preload("res://Audio/Effects_r1_Move.wav"),
"ouroboros": preload("res://Audio/Effects_r1_Ouroboros.wav")}
var players : Dictionary[String, AudioStreamPlayer] = {}
func _enter_tree() -> void:
get_tree().node_added.connect(_on_node_added)
func _ready() -> void:
for sound in sounds.keys():
players[sound] = AudioStreamPlayer.new()
players[sound].stream = sounds[sound]
add_child(players[sound])
func play_sound(sound: String):
if sound in sounds.keys():
players[sound].play()
func _on_node_added(node:Node) -> void:
if node is Button:
# If the added node is a button we connect to its mouse_entered and pressed signals
# and play a sound
node.pressed.connect(play_sound.bind("click"))