56 lines
1.3 KiB
GDScript
56 lines
1.3 KiB
GDScript
class_name Chest
|
|
extends ItemInteractable
|
|
|
|
# Action texts
|
|
const ACTION_COLLECT: StringName = &"collect gold"
|
|
# Animation names
|
|
const ANIM_TAKE_GOLD: StringName = &"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)
|
|
_:
|
|
pass # Other states: destroyed, hidden, idle
|
|
|
|
func update_action_name() -> void:
|
|
match state:
|
|
States.closed, States.looted:
|
|
interaction_area.action_name = ACTION_OPEN
|
|
States.opened:
|
|
interaction_area.action_name = ACTION_COLLECT if has_gold else ACTION_CLOSE
|
|
_:
|
|
pass # Other states
|
|
|
|
func collect_gold() -> void:
|
|
GameManager.player.gold += gold_amount
|
|
has_gold = false
|