conductor/scripts/musician.gd

131 lines
3.2 KiB
GDScript3
Raw Normal View History

2026-03-13 21:52:21 +00:00
#@tool
#@icon(icon_path: String)
class_name Musician
extends AnimatedSprite2D
## Documentation comments
2026-03-15 03:57:31 +00:00
#signal
#enum
#const
2026-03-16 04:25:37 +00:00
#@export_group("Input Textures")
#@export var solid_texture: Texture2D
#@export var outline_texture: Texture2D
2026-03-15 20:49:01 +00:00
@export var song_tempo: int = 120
2026-03-16 04:25:37 +00:00
@export_enum("whole:4", "half:2", "quarter:1") var boost_note_type: int
@export_enum("sample", "viol", "cello", "bass", "execute") var input_action: String
2026-03-15 20:49:01 +00:00
2026-03-16 04:25:37 +00:00
var beat_count: int = 0
2026-03-15 20:49:01 +00:00
var decay_length: float
var decay_rate: float
2026-03-16 04:25:37 +00:00
var is_playing_music: bool = false
2026-03-15 20:49:01 +00:00
var is_boosted: bool
var is_over_boosted: bool = false
var seconds_per_beat: float
var useconds_per_beat: int
2026-03-13 21:52:21 +00:00
@onready var audio: AudioStreamPlayer2D = %Audio
@onready var light: PointLight2D = %Light
@onready var occluder: LightOccluder2D = %Occluder
@onready var input_prompt: Sprite2D = %InputPrompt
2026-03-15 20:49:01 +00:00
@onready var volume_bar: ProgressBar = %VolumeBar
2026-03-16 04:25:37 +00:00
@onready var beat_timer: Timer = %BeatTimer
2026-03-15 20:49:01 +00:00
@onready var boost_timer: Timer = %BoostTimer
2026-03-16 04:25:37 +00:00
@onready var beat_particles: CPUParticles2D = $BeatParticles
2026-03-13 21:52:21 +00:00
## OVERRIDES
func _ready() -> void:
light.visible = false
2026-03-15 20:49:01 +00:00
_connect_signals()
2026-03-16 04:25:37 +00:00
#input_prompt.texture = outline_texture
2026-03-15 20:49:01 +00:00
seconds_per_beat = 60.0/float(song_tempo)
2026-03-16 04:25:37 +00:00
beat_timer.wait_time = seconds_per_beat
decay_length = boost_note_type * seconds_per_beat
2026-03-15 20:49:01 +00:00
print("this musician's boosts will last %s seconds" % decay_length)
boost_timer.wait_time = decay_length
decay_rate = 80.0 / decay_length
print("so the volume will drop by %s db per second" % decay_rate)
2026-03-13 21:52:21 +00:00
2026-03-15 20:49:01 +00:00
func _process(delta: float) -> void:
if is_over_boosted:
2026-03-16 04:25:37 +00:00
distort_pitch()
if is_playing_music and not is_boosted:
2026-03-15 20:49:01 +00:00
drop_volume(decay_rate * delta)
volume_bar.value = audio.volume_db
2026-03-13 21:52:21 +00:00
2026-03-15 20:49:01 +00:00
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("play_pause"):
play_pause()
if event.is_action_pressed(input_action):
boost_volume()
2026-03-13 21:52:21 +00:00
## CORE
2026-03-15 20:49:01 +00:00
func boost_volume() -> void:
if is_boosted:
is_over_boosted = true
audio.volume_db = 0.0
is_boosted = true
boost_timer.start()
2026-03-16 04:25:37 +00:00
func distort_pitch() -> void:
audio.pitch_scale += randf_range(-0.001, 0.0001)
2026-03-15 20:49:01 +00:00
func drop_volume(d_r: float) -> void:
audio.volume_db -= d_r
func play_pause() -> void:
2026-03-16 04:25:37 +00:00
if not is_playing_music:
_play()
2026-03-13 21:52:21 +00:00
else:
2026-03-16 04:25:37 +00:00
_pause()
2026-03-13 21:52:21 +00:00
## PRIVATE/HELPER
2026-03-15 20:49:01 +00:00
func _connect_signals() -> void:
boost_timer.connect("timeout", on_boost_timer_timeout)
audio.connect("finished", play_pause)
2026-03-16 04:25:37 +00:00
beat_timer.connect("timeout", on_beat_timer_timeout)
func _play() -> void:
play("default")
audio.play()
beat_timer.start()
if not is_boosted:
is_boosted = true
boost_timer.start()
light.visible = true
is_playing_music = true
func _pause() -> void:
play("idle")
audio.stop()
beat_timer.stop()
light.visible = false
is_playing_music = false
2026-03-15 20:49:01 +00:00
2026-03-13 21:52:21 +00:00
## RECEIVERS
2026-03-16 04:25:37 +00:00
func on_beat_timer_timeout() -> void:
beat_count += 1
if beat_count % 4 == 0:
beat_particles.color = Color.RED
beat_particles.amount = 24
beat_particles.lifetime = 0.2
else:
beat_particles.color = Color.WHITE
beat_particles.amount = 8
beat_particles.lifetime = 0.15
beat_particles.restart()
2026-03-15 20:49:01 +00:00
func on_boost_timer_timeout() -> void:
print("boost over")
boost_timer.stop()
is_boosted = false
is_over_boosted = false
audio.pitch_scale = 1.0
2026-03-13 21:52:21 +00:00
## SETTERS/GETTERS