TinyAdventure/objects/chest.gd

52 lines
1.1 KiB
GDScript

@tool
class_name Chest
extends StaticBody3D
signal chest_opened
@export var item:ItemResource = null
@export var is_open:bool = false:
get:
return is_open
set(value):
if value != is_open:
is_open = value
_update_geometry()
@onready var item_spawner: ItemSpawner = %ItemSpawner
@onready var animation_player: AnimationPlayer = %AnimationPlayer
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
item_spawner.item = item
_update_geometry()
func _update_geometry() -> void:
if animation_player == null:
return
if is_open:
animation_player.current_animation = "ChestAnimations/Open"
animation_player.active = true
animation_player.seek(animation_player.current_animation_length)
else:
animation_player.current_animation = "ChestAnimations/Open"
animation_player.active = true
animation_player.seek(0)
animation_player.active = false
func _on_opened(_ignore) -> void:
item_spawner.spawn()
chest_opened.emit(self)
func open() -> void:
if is_open:
return
is_open = true
animation_player.active = true
animation_player.seek(0)
animation_player.animation_finished.connect(_on_opened)