aboutsummaryrefslogtreecommitdiff
path: root/client/audio/sound.gd
blob: 8c7649aa72885f515bb08f717533f0d4694c8ad9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Hurry Curry! - a game about cooking
# Copyright (C) 2025 Hurry Curry! contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends Node

@onready var click_sound = $UI/Click
@onready var hover_sound = $UI/Hover
@onready var game_start_sound = $UI/GameStart
@onready var music_node : Node = $Music

var item_sounds: Dictionary = {}
var item_id: int = 0

func play_music(music : String): # If music == "stop", this stops all music
	for stream in music_node.get_children():
		stream.stop()
	
	if music_node.get_node_or_null(music) == null:
		return
	else:
		music_node.get_node(music).play()

func set_volume(bus : int, value:float):
	AudioServer.set_bus_volume_db(bus, value)
	
	if value == -30:
		AudioServer.set_bus_mute(bus, true)
	else:
		AudioServer.set_bus_mute(bus, false)

func play_click():
	click_sound.play()

func play_hover():
	hover_sound.play()

func play_game_start():
	game_start_sound.play()

func play_hover_maybe(element):
	# Don't play hover sound when focus is changed by clicking using a mouse.
	# TODO: Clean up this code and find a proper solution.
	if element is Button:
		if element.is_hovered():
			return
	play_hover()

func item_progress(item: Item, running: AudioStream, stopping: AudioStream, volume=0.) -> int:
	item_id += 1
	var running_player: AudioStreamPlayer3D = AudioStreamPlayer3D.new()
	running_player.stream = running
	running_player.name = "Running%d" % item_id
	running_player.volume_db = volume
	add_child(running_player)
	var stopping_player: AudioStreamPlayer3D = AudioStreamPlayer3D.new()
	stopping_player.stream = stopping
	stopping_player.name = "Stopping%d" % item_id
	stopping_player.volume_db = volume
	running_player.play()
	add_child(stopping_player)
	item_sounds[item_id] = [item, running_player, stopping_player, false]
	return item_id

func item_finished(id: int):
	var running_player: AudioStreamPlayer3D = item_sounds[id][1]
	var stopping_player: AudioStreamPlayer3D = item_sounds[id][2]
	item_sounds[id][3] = true
	running_player.stop()
	stopping_player.play()
	stopping_player.finished.connect(func():
		free_sound(id)
	)

func free_sound(id: int):
	var running_player: AudioStreamPlayer3D = item_sounds[id][1]
	var stopping_player: AudioStreamPlayer3D = item_sounds[id][2]
	running_player.queue_free()
	stopping_player.queue_free()
	item_sounds.erase(id)

func _physics_process(_delta):
	for k in item_sounds.keys():
		if item_sounds[k][0] != null:
			var position: Vector3 = item_sounds[k][0].position
			item_sounds[k][1].position = position
			item_sounds[k][2].position = position
		elif not item_sounds[k][3]:
			item_finished(k)