48 lines
1.5 KiB
GDScript
48 lines
1.5 KiB
GDScript
class_name CurrencyTile
|
|
extends HBoxContainer
|
|
|
|
@export var currency: Currency
|
|
@export var game_state: LevelGameState
|
|
|
|
@onready var _icon: TextureRect = $CurrencyIcon
|
|
@onready var _label: Label = $Label
|
|
@onready var _currency_label: Label = $CurrencyValueLabel
|
|
@onready var _debug_button: Button = $DebugIncomeButton
|
|
|
|
var _currency_id: StringName = &""
|
|
|
|
func _ready() -> void:
|
|
if currency == null:
|
|
push_warning("CurrencyTile '%s' has no currency configured." % String(name))
|
|
return
|
|
|
|
if game_state == null:
|
|
push_error("CurrencyTile '%s' missing game_state reference" % String(name))
|
|
return
|
|
|
|
game_state.ready.connect(_on_level_game_state_ready)
|
|
|
|
func _on_level_game_state_ready() -> void:
|
|
_currency_id = game_state.get_currency_id(currency)
|
|
|
|
var label_string = game_state.get_currency_name(_currency_id)
|
|
_label.text = "%s:" % label_string
|
|
_icon.texture = game_state.get_currency_icon(_currency_id)
|
|
_debug_button.text = "+100 %s" % label_string
|
|
|
|
game_state.currency_changed.connect(_on_currency_changed)
|
|
_on_currency_changed(_currency_id, game_state.get_currency_amount_by_id(_currency_id))
|
|
|
|
func _on_currency_changed(changed_currency_id: StringName, amount: BigNumber) -> void:
|
|
if changed_currency_id != _currency_id:
|
|
return
|
|
_currency_label.text = amount.to_string_suffix(2)
|
|
visible = currency.always_visible or not amount.is_equal_to(BigNumber.from_float(0.0))
|
|
|
|
func _on_debug_income_button_pressed() -> void:
|
|
if currency == null:
|
|
return
|
|
if game_state == null:
|
|
return
|
|
game_state.add_currency(currency, BigNumber.from_float(100))
|