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

@@ -1,11 +1,11 @@
extends Node
class UnlockGoalRequirement extends RefCounted:
var currency: GameState.CurrencyType
var currency_id: StringName
var amount: BigNumber
func _init(req_currency: GameState.CurrencyType, req_amount: BigNumber) -> void:
currency = req_currency
func _init(req_currency_id: StringName, req_amount: BigNumber) -> void:
currency_id = req_currency_id
amount = req_amount
class UnlockGoal extends RefCounted:
@@ -31,7 +31,7 @@ func _ready() -> void:
GameState.currency_changed.connect(_on_currency_changed)
_evaluate_all_goals()
func _on_currency_changed(_changed_currency: GameState.CurrencyType, _new_amount: BigNumber) -> void:
func _on_currency_changed(_changed_currency_id: StringName, _new_amount: BigNumber) -> void:
_evaluate_all_goals()
func _evaluate_all_goals() -> void:
@@ -48,7 +48,7 @@ func _evaluate_goal(goal: UnlockGoal) -> bool:
return false
for requirement in goal.requirements:
var total_amount: BigNumber = _get_total_currency_amount(requirement.currency)
var total_amount: BigNumber = _get_total_currency_amount(requirement.currency_id)
if total_amount.is_less_than(requirement.amount):
return false
@@ -159,8 +159,8 @@ func _parse_requirement(raw_requirement: Variant, goal_id: String) -> UnlockGoal
return null
var requirement_dict: Dictionary = raw_requirement
var currency_value: int = _parse_currency(requirement_dict.get("currency", ""))
if currency_value < 0:
var currency_id: StringName = _parse_currency(requirement_dict.get("currency", ""))
if currency_id == &"" or not GameState.is_known_currency_id(currency_id):
push_warning("Skipping requirement in goal '%s': unknown currency '%s'" % [goal_id, String(requirement_dict.get("currency", ""))])
return null
@@ -174,26 +174,13 @@ func _parse_requirement(raw_requirement: Variant, goal_id: String) -> UnlockGoal
push_warning("Skipping requirement in goal '%s': amount must be non-negative" % goal_id)
return null
return UnlockGoalRequirement.new(currency_value, amount)
return UnlockGoalRequirement.new(currency_id, amount)
func _parse_currency(raw_currency: Variant) -> int:
if raw_currency is int:
var currency_int: int = raw_currency
if currency_int == GameState.CurrencyType.gold or currency_int == GameState.CurrencyType.gems:
return currency_int
return -1
func _parse_currency(raw_currency: Variant) -> StringName:
return GameState.parse_currency_id(raw_currency)
var currency_text: String = String(raw_currency).to_lower().strip_edges()
match currency_text:
"gold":
return GameState.CurrencyType.gold
"gems", "gem":
return GameState.CurrencyType.gems
_:
return -1
func _get_total_currency_amount(currency: GameState.CurrencyType) -> BigNumber:
return GameState.get_total_currency_acquired(currency)
func _get_total_currency_amount(currency_id: StringName) -> BigNumber:
return GameState.get_total_currency_acquired_by_id(currency_id)
func _collect_known_generator_ids() -> void:
_known_generator_ids.clear()