Replace CurrencyType with Currency resource
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
extends PanelContainer
|
||||
|
||||
class GoalRequirement 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 GoalDefinition extends RefCounted:
|
||||
@@ -47,7 +47,7 @@ func _ready() -> void:
|
||||
_refresh_ui()
|
||||
_refresh_ui.call_deferred()
|
||||
|
||||
func _on_currency_changed(_currency_type: GameState.CurrencyType, _new_amount: BigNumber) -> void:
|
||||
func _on_currency_changed(_currency_id: StringName, _new_amount: BigNumber) -> void:
|
||||
_refresh_ui()
|
||||
|
||||
func _on_generator_state_changed(_generator_id: StringName, _state: Dictionary) -> void:
|
||||
@@ -119,8 +119,8 @@ func _parse_requirement(raw_requirement: Variant) -> GoalRequirement:
|
||||
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):
|
||||
return null
|
||||
|
||||
var raw_amount: Variant = requirement_dict.get("amount", {})
|
||||
@@ -131,23 +131,10 @@ func _parse_requirement(raw_requirement: Variant) -> GoalRequirement:
|
||||
if amount.mantissa < 0.0:
|
||||
return null
|
||||
|
||||
return GoalRequirement.new(currency_value, amount)
|
||||
return GoalRequirement.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
|
||||
|
||||
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 _parse_currency(raw_currency: Variant) -> StringName:
|
||||
return GameState.parse_currency_id(raw_currency)
|
||||
|
||||
func _build_goal_rows() -> void:
|
||||
_rows_by_goal_id.clear()
|
||||
@@ -225,7 +212,7 @@ func _refresh_goal_row(row: GoalRow) -> void:
|
||||
|
||||
func _is_goal_met(goal: GoalDefinition) -> bool:
|
||||
for requirement in goal.requirements:
|
||||
var total_amount: BigNumber = GameState.get_total_currency_acquired(requirement.currency)
|
||||
var total_amount: BigNumber = GameState.get_total_currency_acquired_by_id(requirement.currency_id)
|
||||
if total_amount.is_less_than(requirement.amount):
|
||||
return false
|
||||
return true
|
||||
@@ -253,21 +240,15 @@ func _on_unlock_pressed(goal_id: StringName) -> void:
|
||||
func _get_requirements_text(goal: GoalDefinition) -> String:
|
||||
var parts: Array[String] = []
|
||||
for requirement in goal.requirements:
|
||||
var total_amount: BigNumber = GameState.get_total_currency_acquired(requirement.currency)
|
||||
var total_amount: BigNumber = GameState.get_total_currency_acquired_by_id(requirement.currency_id)
|
||||
parts.append(
|
||||
"%s %s / %s" % [
|
||||
_currency_label(requirement.currency),
|
||||
_currency_label(requirement.currency_id),
|
||||
total_amount.to_string_suffix(2),
|
||||
requirement.amount.to_string_suffix(2)
|
||||
]
|
||||
)
|
||||
return " | ".join(parts)
|
||||
|
||||
func _currency_label(currency: GameState.CurrencyType) -> String:
|
||||
match currency:
|
||||
GameState.CurrencyType.gold:
|
||||
return "Gold"
|
||||
GameState.CurrencyType.gems:
|
||||
return "Gems"
|
||||
_:
|
||||
return "Unknown"
|
||||
func _currency_label(currency_id: StringName) -> String:
|
||||
return GameState.get_currency_name(currency_id)
|
||||
|
||||
Reference in New Issue
Block a user