SchildDerStaerke/scripts/chest.gd

74 lines
1.5 KiB
GDScript

class_name Chest
extends ItemInteractable
# Aktionstexte
const ACTION_OPEN := "open chest"
const ACTION_CLOSE := "close chest"
const ACTION_COLLECT := "collect gold"
# Animationsnamen
const ANIM_OPEN := "open"
const ANIM_CLOSE := "close"
const ANIM_TAKE_GOLD := "take_gold"
@export var gold_amount: int = 10
var has_gold: bool = false
func _ready() -> void:
state_changed.connect(_on_state_changed)
interaction_area.interact = _on_interact
has_gold = gold_amount > 0
state = States.closed
update_action_name()
func _on_interact() -> void:
match state:
States.closed:
state = States.opened
States.opened:
if has_gold:
collect_gold()
state = States.looted
else:
state = States.closed
States.looted:
state = States.closed
update_action_name()
func _on_state_changed(new_state: States) -> void:
match new_state:
States.opened:
animation_player.play(ANIM_OPEN)
States.closed:
animation_player.play(ANIM_CLOSE)
States.looted:
animation_player.play(ANIM_TAKE_GOLD)
States.destroyed:
pass
States.hidden:
pass
States.idle:
pass
func update_action_name() -> void:
match state:
States.closed:
interaction_area.action_name = ACTION_OPEN
States.opened:
interaction_area.action_name = ACTION_COLLECT if has_gold else ACTION_CLOSE
States.looted:
interaction_area.action_name = ACTION_OPEN
States.destroyed:
pass
States.hidden:
pass
States.idle:
pass
func collect_gold() -> void:
GameManager.player.gold += gold_amount
has_gold = false