70 lines
1.7 KiB
GDScript
70 lines
1.7 KiB
GDScript
|
|
class_name OwniverseMusicPlayer
|
||
|
|
extends AudioStreamPlayer
|
||
|
|
|
||
|
|
const EXCLUDED_TRACK := "apocalypse_the_end"
|
||
|
|
const MIN_PAUSE_SECONDS := 120.0
|
||
|
|
const MAX_PAUSE_SECONDS := 300.0
|
||
|
|
|
||
|
|
var enabled: bool = true
|
||
|
|
var last_track: String = ""
|
||
|
|
var _pause_timer: Timer
|
||
|
|
var _rng := RandomNumberGenerator.new()
|
||
|
|
var _music: Dictionary = OwniverseAudioLoader.get_music()
|
||
|
|
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
_rng.randomize()
|
||
|
|
_pause_timer = Timer.new()
|
||
|
|
_pause_timer.one_shot = true
|
||
|
|
_pause_timer.timeout.connect(_play_next)
|
||
|
|
add_child(_pause_timer)
|
||
|
|
finished.connect(_schedule_pause)
|
||
|
|
if enabled:
|
||
|
|
_schedule_pause()
|
||
|
|
|
||
|
|
|
||
|
|
func set_enabled(value: bool) -> void:
|
||
|
|
enabled = value
|
||
|
|
if not is_node_ready():
|
||
|
|
return
|
||
|
|
|
||
|
|
stream_paused = not enabled
|
||
|
|
_pause_timer.paused = not enabled
|
||
|
|
if enabled and not playing and _pause_timer.is_stopped():
|
||
|
|
_play_next()
|
||
|
|
|
||
|
|
|
||
|
|
func play_apocalypse() -> void:
|
||
|
|
if not enabled:
|
||
|
|
return
|
||
|
|
if not _music.has(EXCLUDED_TRACK):
|
||
|
|
push_warning("Apocalypse music is unavailable.")
|
||
|
|
return
|
||
|
|
|
||
|
|
_pause_timer.stop()
|
||
|
|
stream = _music[EXCLUDED_TRACK]
|
||
|
|
play()
|
||
|
|
|
||
|
|
|
||
|
|
func _play_next() -> void:
|
||
|
|
if not enabled:
|
||
|
|
return
|
||
|
|
|
||
|
|
var candidates: Array = _music.keys()
|
||
|
|
candidates.erase(EXCLUDED_TRACK)
|
||
|
|
candidates.erase(last_track)
|
||
|
|
if candidates.is_empty():
|
||
|
|
push_warning("No music tracks available for playback.")
|
||
|
|
return
|
||
|
|
|
||
|
|
var track_id: String = str(candidates[_rng.randi_range(0, candidates.size() - 1)])
|
||
|
|
last_track = track_id
|
||
|
|
stream = _music[track_id]
|
||
|
|
play()
|
||
|
|
|
||
|
|
|
||
|
|
func _schedule_pause() -> void:
|
||
|
|
if not enabled:
|
||
|
|
return
|
||
|
|
_pause_timer.start(_rng.randf_range(MIN_PAUSE_SECONDS, MAX_PAUSE_SECONDS))
|