Replace CurrencyType with Currency resource

This commit is contained in:
2026-03-14 12:39:21 +01:00
parent 020d287b03
commit 6da962a53d
13 changed files with 379 additions and 158 deletions

View File

@@ -18,7 +18,7 @@ signal production_tick(amount: BigNumber, cycle_count: int, total_owned: int)
const HUGE_COST_EXPONENT: int = 1000000
## Currency target updated by this generator.
@export var currency: GameState.CurrencyType = GameState.CurrencyType.gold
@export var currency: Resource = GameState.GOLD_CURRENCY
## Data resource containing balancing values and defaults (required).
@export var data: CurrencyGeneratorData
## Enables per-cycle automatic production when true.
@@ -85,6 +85,9 @@ func _ready() -> void:
_generator_id = _resolve_generator_id()
_ensure_registered()
cycle_progress_seconds = 0.0
if currency == null:
push_warning("CurrencyGenerator '%s' has no currency set; defaulting to Gold." % String(name))
currency = GameState.GOLD_CURRENCY
if data == null:
push_error("CurrencyGenerator '%s' is missing CurrencyGeneratorData." % String(name))
@@ -247,40 +250,35 @@ func get_generator_id() -> StringName:
_ensure_registered()
return _generator_id
## Returns the configured currency id.
func get_currency_id() -> StringName:
return GameState.get_currency_id(currency)
## True when the generator is both unlocked and available.
func is_available_to_player() -> bool:
return is_unlocked and is_available
## Routes positive/negative currency deltas to the configured currency bucket.
func _add_currency(amount: BigNumber) -> void:
match currency:
GameState.CurrencyType.gold:
GameState.add_gold(amount)
GameState.CurrencyType.gems:
GameState.add_gems(amount)
_:
push_error("Unknown currency type in CurrencyGenerator._add_currency")
if currency == null:
push_error("CurrencyGenerator '%s' cannot add currency: currency resource is null." % String(name))
return
GameState.add_currency(currency, amount)
## Attempts to spend target currency; returns false when insufficient.
func _spend_currency(cost: BigNumber) -> bool:
match currency:
GameState.CurrencyType.gold:
return GameState.spend_gold(cost)
GameState.CurrencyType.gems:
return GameState.spend_gems(cost)
_:
push_error("Unknown currency type in CurrencyGenerator._spend_currency")
return false
if currency == null:
push_error("CurrencyGenerator '%s' cannot spend currency: currency resource is null." % String(name))
return false
return GameState.spend_currency(currency, cost)
## Returns the current amount for the configured currency type.
func _get_currency_amount() -> BigNumber:
match currency:
GameState.CurrencyType.gold:
return GameState.gold
GameState.CurrencyType.gems:
return GameState.gems
_:
return BigNumber.from_float(0.0)
if currency == null:
return BigNumber.from_float(0.0)
return GameState.get_currency_amount(currency)
## Safely converts finite float values into BigNumber costs.
func _float_to_big_number(value: float) -> BigNumber: