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")
