grunk/src/props/wall_switch/wall_switch.gd

99 lines
2.0 KiB
GDScript3
Raw Normal View History

2025-03-05 14:46:13 -07:00
extends Node3D
2025-03-05 15:35:49 -07:00
2025-03-20 17:46:01 -06:00
signal cleaned
2025-03-05 15:35:49 -07:00
signal activated
2025-03-20 17:46:01 -06:00
const CLEAN_THRESHOLD := 1400
2025-03-05 15:35:49 -07:00
2025-03-20 17:46:01 -06:00
@export var clean := false
2025-03-05 15:35:49 -07:00
@export var enabled := false
2025-04-19 09:49:07 -06:00
@export var label: String
2025-03-05 15:35:49 -07:00
@onready var animation_player: AnimationPlayer = %AnimationPlayer
@onready var light_animation: AnimationPlayer = %LightAnimation
@onready var gunk_body: GunkBody = %GunkBody
@onready var interactive: Interactive = %Interactive
2025-04-02 21:03:36 -06:00
@onready var action_sfx: AudioStreamPlayer3D = %ActionSFX
@onready var action_delay: Timer = %ActionDelay
2025-03-05 15:35:49 -07:00
2025-03-20 17:46:01 -06:00
# XXX I must have been smoking crack when I wrote this logic. Why so complicated?
2025-03-05 15:35:49 -07:00
func _ready() -> void:
2025-04-19 09:49:07 -06:00
interactive.label = label
2025-03-20 17:46:01 -06:00
if clean:
gunk_body.clear_all()
_on_clean()
2025-03-05 15:35:49 -07:00
if enabled:
2025-03-20 17:46:01 -06:00
_on_enable()
2025-03-05 15:35:49 -07:00
2025-03-10 17:00:30 -06:00
## Called once all gunk is cleared. Enables interaction & plays animations
2025-03-05 15:35:49 -07:00
func enable() -> void:
enabled = true
2025-03-20 17:46:01 -06:00
_on_enable()
2025-03-05 15:35:49 -07:00
2025-03-20 17:46:01 -06:00
func _on_enable() -> void:
if clean:
light_animation.play("success")
if not animation_player.is_playing():
interactive.enabled = true
2025-03-05 15:35:49 -07:00
2025-03-10 17:00:30 -06:00
## Called once the switch is no longer usable. Disables interaction & plays animations
func disable() -> void:
2025-03-20 17:46:01 -06:00
_on_disable()
2025-03-10 17:00:30 -06:00
enabled = false
2025-03-20 17:46:01 -06:00
func _on_disable() -> void:
if enabled and clean:
light_animation.play("disable")
else:
light_animation.play("RESET")
2025-03-10 17:00:30 -06:00
interactive.enabled = false
2025-03-20 17:46:01 -06:00
func _on_clean() -> void:
clean = true
if enabled:
_on_enable()
else:
_on_disable()
cleaned.emit()
2025-03-05 15:35:49 -07:00
func _activate() -> void:
animation_player.play("activate")
2025-04-02 21:03:36 -06:00
action_sfx.play()
action_delay.start()
2025-03-05 15:35:49 -07:00
# Disable while animation is playing
interactive.enabled = false
2025-03-10 17:00:30 -06:00
func _animation_finished(anim_name: StringName) -> void:
if anim_name == "activate":
interactive.enabled = enabled
func _on_gunk_body_clear_total_updated(clear_total: float) -> void:
2025-03-20 17:46:01 -06:00
if not clean and clear_total >= CLEAN_THRESHOLD:
_on_clean()
2025-04-02 21:03:36 -06:00
func _on_action_delay_timeout() -> void:
activated.emit()
2025-04-22 12:08:09 -06:00
func serialize() -> Dictionary:
return {"enabled": enabled}
func deserialize(state: Dictionary) -> void:
if state["enabled"]:
enable()
else:
disable()