init
This commit is contained in:
58
'
Normal file
58
'
Normal file
@@ -0,0 +1,58 @@
|
||||
@tool
|
||||
class_name TreasureChest extends Node2D
|
||||
|
||||
@export var item_data: ItemData :
|
||||
set = set_item_data
|
||||
@export var quantity: int = 1 :
|
||||
set = set_quantity
|
||||
|
||||
@onready var item_sprite: Sprite2D = $ItemSprite
|
||||
@onready var label: Label = $ItemSprite/Label
|
||||
@onready var animation_player: AnimationPlayer = $AnimationPlayer
|
||||
@onready var area: Area2D = $Area2D
|
||||
|
||||
var is_open := false
|
||||
|
||||
func _ready() -> void:
|
||||
update_texture()
|
||||
update_label()
|
||||
if Engine.is_editor_hint():
|
||||
return
|
||||
|
||||
area.area_entered.connect(on_area_entered)
|
||||
area.area_exited.connect(on_area_exited)
|
||||
|
||||
func set_item_data(new_item_data: ItemData) -> void:
|
||||
item_data = new_item_data
|
||||
update_texture()
|
||||
|
||||
func set_quantity(new_quantity: int) -> void:
|
||||
quantity = new_quantity
|
||||
update_label()
|
||||
|
||||
func on_area_entered(_body: Area2D) -> void:
|
||||
PlayerManager.interact_pressed.connect(on_player_interaction)
|
||||
|
||||
func on_area_exited(_body: Area2D) -> void:
|
||||
PlayerManager.interact_pressed.disconnect(on_player_interaction)
|
||||
|
||||
func on_player_interaction() -> void:
|
||||
if is_open:
|
||||
return
|
||||
is_open = true
|
||||
animation_player.play("open_chest")
|
||||
if item_data and quantity > 0:
|
||||
PlayerManager.INVENTORY_DATA.add_item(item_data, quantity)
|
||||
else:
|
||||
printerr("Chest has not items")
|
||||
push_error("Chest has not items")
|
||||
|
||||
func update_texture() -> void:
|
||||
if !item_data or !sprite:
|
||||
return
|
||||
sprite.texture = item_data.texture
|
||||
|
||||
func update_label() -> void:
|
||||
if !label:
|
||||
return
|
||||
label.text = "x%d" % quantity if quantity > 0 else ""
|
||||
Reference in New Issue
Block a user