Files
idle/currency_label.gd
2026-04-23 22:09:57 +02:00

47 lines
1.4 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)
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))