-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1c4cac
commit 90ffd34
Showing
13 changed files
with
317 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,35 @@ | ||
[gd_scene load_steps=5 format=3 uid="uid://bp61l7vkkscuw"] | ||
[gd_scene load_steps=8 format=3 uid="uid://bp61l7vkkscuw"] | ||
|
||
[ext_resource type="Script" path="res://global/autoload/audio/audio.gd" id="1_7i6rq"] | ||
[ext_resource type="PackedScene" uid="uid://f1stqtkpi6rl" path="res://global/autoload/audio/audio_queue/audio_queue.tscn" id="2_bb4fe"] | ||
[ext_resource type="PackedScene" uid="uid://cragwwd3j3bqt" path="res://global/autoload/audio/music/music.tscn" id="2_dmcs5"] | ||
[ext_resource type="PackedScene" uid="uid://bu2l4l0jhesyw" path="res://global/autoload/audio/sfx_map/sfx_map.tscn" id="3_4nox4"] | ||
[ext_resource type="PackedScene" uid="uid://cragwwd3j3bqt" path="res://global/autoload/audio/audio_player/audio_player.tscn" id="4_gols3"] | ||
[ext_resource type="Resource" uid="uid://cuwcu7njbtvqn" path="res://resources/songs/forest_song.tres" id="5_ftjyd"] | ||
[ext_resource type="Resource" uid="uid://d1yl7vrmvavit" path="res://resources/songs/axes_to_a_bed.tres" id="5_gma2y"] | ||
[ext_resource type="AudioStream" uid="uid://cjg7wv1kxy1ye" path="res://assets/audio/freesound_org/heartbeat_80bpm/loudernoises_heartbeat_80bpm.wav" id="5_s525l"] | ||
|
||
[node name="Audio" type="Node"] | ||
script = ExtResource("1_7i6rq") | ||
|
||
[node name="Music" parent="." instance=ExtResource("2_dmcs5")] | ||
|
||
[node name="SfxQueue" parent="." instance=ExtResource("2_bb4fe")] | ||
unique_name_in_owner = true | ||
stream_player_count = 16 | ||
|
||
[node name="SfxMap" parent="SfxQueue" instance=ExtResource("3_4nox4")] | ||
unique_name_in_owner = true | ||
|
||
[node name="Music" type="Node" parent="."] | ||
unique_name_in_owner = true | ||
|
||
[node name="ForestSong" parent="Music" instance=ExtResource("4_gols3")] | ||
default_audio = ExtResource("5_ftjyd") | ||
|
||
[node name="20AxesToABed" parent="Music" instance=ExtResource("4_gols3")] | ||
default_audio = ExtResource("5_gma2y") | ||
|
||
[node name="Ambience" type="Node" parent="."] | ||
|
||
[node name="Heartbeat" parent="Ambience" instance=ExtResource("4_gols3")] | ||
unique_name_in_owner = true | ||
default_audio = ExtResource("5_s525l") | ||
is_looping = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
class_name AudioPlayer extends Node | ||
|
||
signal finished | ||
signal master_volume_changed(volume: float) | ||
|
||
const TWEEN_FADE_AUDIO_DURATION: float = 2.0 | ||
|
||
@export_enum(&"Master", &"Music", &"SFX") var default_bus: String = &"Music" | ||
@export var default_audio: Resource # AudioStream | Song, can't union type in gdscript :( | ||
@export var autoplay: bool = false | ||
@export var max_volume: float = 1.0 | ||
@export var is_looping: bool = false | ||
|
||
var master_volume: float = max_volume | ||
var _current_audio: Resource | ||
var _fade_tween: Tween | ||
var _idle_playback_position: float = 0.0 | ||
var _combat_playback_position: float = 0.0 | ||
|
||
@onready var states: StateMachine = %States | ||
@onready var tracks: Node = %Tracks | ||
@onready var idle_track: AudioStreamPlayer = %Idle | ||
@onready var combat_track: AudioStreamPlayer = %Combat | ||
|
||
############### | ||
## overrides ## | ||
############### | ||
|
||
|
||
func _ready() -> void: | ||
assert(default_audio is AudioStream || default_audio is Song) | ||
|
||
set_bus(default_bus) | ||
set_audio(default_audio) | ||
_connect_signals() | ||
|
||
|
||
############# | ||
## methods ## | ||
############# | ||
|
||
|
||
func set_bus(bus: StringName) -> void: | ||
idle_track.bus = bus | ||
combat_track.bus = bus | ||
|
||
|
||
func set_audio(audio: Resource) -> void: | ||
_current_audio = audio | ||
if audio is AudioStream: | ||
idle_track.stream = audio | ||
if audio is Song: | ||
idle_track.stream = audio.idle_audio_stream | ||
combat_track.stream = audio.combat_audio_stream | ||
|
||
|
||
func is_playing() -> bool: | ||
return idle_track.playing || combat_track.playing | ||
|
||
|
||
func play(restart: bool = true) -> void: | ||
if restart: | ||
_idle_playback_position = 0.0 | ||
_combat_playback_position = 0.0 | ||
idle_track.play(_idle_playback_position) | ||
combat_track.play(_combat_playback_position) | ||
|
||
|
||
func stop() -> void: | ||
idle_track.stop() | ||
combat_track.stop() | ||
|
||
|
||
func set_master_volume(volume_db: float) -> void: | ||
master_volume = volume_db | ||
master_volume_changed.emit(master_volume) | ||
|
||
|
||
func transition_master_volume(from_volume: float, to_volume: float) -> void: | ||
if _fade_tween != null: | ||
_fade_tween.kill() | ||
_fade_tween = create_tween() | ||
|
||
_fade_tween.tween_method(set_master_volume, from_volume, to_volume, TWEEN_FADE_AUDIO_DURATION) | ||
|
||
|
||
func _fade_out_callback(pause: bool) -> void: | ||
if pause: | ||
_idle_playback_position = idle_track.get_playback_position() | ||
_combat_playback_position = combat_track.get_playback_position() | ||
stop() | ||
|
||
|
||
func fade_out(pause: bool = true) -> void: | ||
var from_volume: float = max_volume | ||
var to_volume: float = 0.0 | ||
transition_master_volume(from_volume, to_volume) | ||
_fade_tween.tween_callback(_fade_out_callback.bind(pause)) | ||
|
||
|
||
func fade_in(restart: bool = false) -> void: | ||
var from_volume: float = 0.0 | ||
var to_volume: float = max_volume | ||
transition_master_volume(from_volume, to_volume) | ||
play(restart) | ||
|
||
|
||
############### | ||
## signals ## | ||
############### | ||
|
||
|
||
func _connect_signals() -> void: | ||
SignalBus.tab_changed.connect(_on_tab_changed) | ||
idle_track.finished.connect(_on_track_finished) | ||
combat_track.finished.connect(_on_track_finished) | ||
|
||
|
||
func _on_track_finished() -> void: | ||
if is_looping: | ||
play(0.0) | ||
else: | ||
stop() | ||
finished.emit() | ||
|
||
|
||
func _on_tab_changed(tab_data: TabData) -> void: | ||
if _current_audio is Song: | ||
if tab_data.id == DarknessScreen.TAB_DATA_ID: | ||
states.transition_to("Combat") | ||
elif states.current.name == "Combat" && !tab_data.id == StarwayScreen.TAB_DATA_ID: | ||
states.transition_to("Idle") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[gd_scene load_steps=4 format=3 uid="uid://cragwwd3j3bqt"] | ||
|
||
[ext_resource type="Script" path="res://global/autoload/audio/audio_player/audio_player.gd" id="1_3cuwr"] | ||
[ext_resource type="Script" path="res://scenes/component/state_machine/state_machine.gd" id="2_pwrmv"] | ||
[ext_resource type="Script" path="res://global/autoload/audio/audio_player/track_state.gd" id="3_tvnd0"] | ||
|
||
[node name="AudioPlayer" type="Node"] | ||
script = ExtResource("1_3cuwr") | ||
|
||
[node name="Tracks" type="Node" parent="."] | ||
unique_name_in_owner = true | ||
|
||
[node name="Idle" type="AudioStreamPlayer" parent="Tracks"] | ||
unique_name_in_owner = true | ||
bus = &"Music" | ||
|
||
[node name="Combat" type="AudioStreamPlayer" parent="Tracks"] | ||
unique_name_in_owner = true | ||
bus = &"Music" | ||
|
||
[node name="States" type="Node" parent="." node_paths=PackedStringArray("default_state")] | ||
unique_name_in_owner = true | ||
script = ExtResource("2_pwrmv") | ||
default_state = NodePath("Idle") | ||
|
||
[node name="Idle" type="Node" parent="States"] | ||
script = ExtResource("3_tvnd0") | ||
|
||
[node name="Combat" type="Node" parent="States"] | ||
script = ExtResource("3_tvnd0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
extends StateMachineState | ||
|
||
@onready var audio_player: AudioPlayer = owner | ||
|
||
const TWEEN_TRANSITION_TIME: float = 1.0 | ||
|
||
var track: AudioStreamPlayer | ||
var _transition_tween: Tween | ||
var _is_active: bool = false | ||
|
||
############### | ||
## overrides ## | ||
############### | ||
|
||
|
||
func _on_ready() -> void: | ||
await audio_player.ready | ||
track = audio_player.tracks.get_node(NodePath(self.name)) | ||
track.volume_db = linear_to_db(0.0) | ||
audio_player.master_volume_changed.connect(_on_master_volume_changed) | ||
|
||
|
||
func _on_state_enter() -> void: | ||
print("entered state: %s" % self.name) | ||
_is_active = true | ||
var from_volume: float = 0.0 | ||
var to_volume: float = audio_player.master_volume | ||
transition_volume(from_volume, to_volume) | ||
|
||
|
||
func _on_state_exit() -> void: | ||
print("exited state: %s" % self.name) | ||
_is_active = false | ||
var from_volume: float = audio_player.master_volume | ||
var to_volume: float = 0.0 | ||
transition_volume(from_volume, to_volume) | ||
|
||
|
||
############# | ||
## methods ## | ||
############# | ||
|
||
|
||
func set_volume(volume: float) -> void: | ||
track.volume_db = linear_to_db(volume) | ||
|
||
|
||
func transition_volume(from_volume: float, to_volume: float) -> void: | ||
if _transition_tween != null: | ||
_transition_tween.kill() | ||
_transition_tween = create_tween() | ||
|
||
_transition_tween.tween_method(set_volume, from_volume, to_volume, TWEEN_TRANSITION_TIME) | ||
|
||
|
||
############### | ||
## signals ## | ||
############### | ||
|
||
|
||
func _on_master_volume_changed(volume: float) -> void: | ||
if linear_to_db(volume) < track.volume_db || _is_active: | ||
set_volume(volume) |
Oops, something went wrong.