Replace CurrencyType with Currency resource
This commit is contained in:
313
game_state.gd
313
game_state.gd
@@ -4,40 +4,50 @@ extends Node
|
||||
# ==========================================
|
||||
# SIGNALS
|
||||
# ==========================================
|
||||
signal currency_changed(currency_type: CurrencyType, new_amount: BigNumber)
|
||||
signal currency_changed(currency_id: StringName, new_amount: BigNumber)
|
||||
signal generator_state_changed(generator_id: StringName, state: Dictionary)
|
||||
|
||||
enum CurrencyType { gold, gems }
|
||||
const GOLD_CURRENCY: Resource = preload("res://currency/gold.tres")
|
||||
const GEMS_CURRENCY: Resource = preload("res://currency/gems.tres")
|
||||
const DEFAULT_CURRENCIES: Array[Resource] = [GOLD_CURRENCY, GEMS_CURRENCY]
|
||||
|
||||
const GOLD_CURRENCY_ID: StringName = &"gold"
|
||||
const GEMS_CURRENCY_ID: StringName = &"gems"
|
||||
const LEGACY_CURRENCY_INDEX_TO_ID: Dictionary = {
|
||||
0: GOLD_CURRENCY_ID,
|
||||
1: GEMS_CURRENCY_ID,
|
||||
}
|
||||
|
||||
# ==========================================
|
||||
# STATE VARIABLES
|
||||
# ==========================================
|
||||
var _current_currency: Dictionary = {}
|
||||
var _total_currency_acquired: Dictionary = {}
|
||||
var _currency_by_id: Dictionary = {}
|
||||
|
||||
var gold: BigNumber:
|
||||
get:
|
||||
return _get_current_currency_ref(CurrencyType.gold)
|
||||
return _get_current_currency_ref(GOLD_CURRENCY_ID)
|
||||
set(value):
|
||||
_set_current_currency(CurrencyType.gold, value)
|
||||
_set_current_currency(GOLD_CURRENCY_ID, value)
|
||||
|
||||
var gems: BigNumber:
|
||||
get:
|
||||
return _get_current_currency_ref(CurrencyType.gems)
|
||||
return _get_current_currency_ref(GEMS_CURRENCY_ID)
|
||||
set(value):
|
||||
_set_current_currency(CurrencyType.gems, value)
|
||||
_set_current_currency(GEMS_CURRENCY_ID, value)
|
||||
|
||||
var total_gold_acquired: BigNumber:
|
||||
get:
|
||||
return _get_total_currency_ref(CurrencyType.gold)
|
||||
return _get_total_currency_ref(GOLD_CURRENCY_ID)
|
||||
set(value):
|
||||
_set_total_currency(CurrencyType.gold, value)
|
||||
_set_total_currency(GOLD_CURRENCY_ID, value)
|
||||
|
||||
var total_gems_acquired: BigNumber:
|
||||
get:
|
||||
return _get_total_currency_ref(CurrencyType.gems)
|
||||
return _get_total_currency_ref(GEMS_CURRENCY_ID)
|
||||
set(value):
|
||||
_set_total_currency(CurrencyType.gems, value)
|
||||
_set_total_currency(GEMS_CURRENCY_ID, value)
|
||||
|
||||
var generator_states: Dictionary = {}
|
||||
|
||||
@@ -49,8 +59,12 @@ const GENERATOR_OWNED_KEY: String = "owned"
|
||||
const GENERATOR_PURCHASED_COUNT_KEY: String = "purchased_count"
|
||||
const GENERATOR_UNLOCKED_KEY: String = "unlocked"
|
||||
const GENERATOR_AVAILABLE_KEY: String = "available"
|
||||
const CURRENCIES_KEY: String = "currencies"
|
||||
const CURRENT_KEY: String = "current"
|
||||
const TOTAL_KEY: String = "total"
|
||||
|
||||
func _ready() -> void:
|
||||
_initialize_currency_catalog()
|
||||
_initialize_currency_maps()
|
||||
load_game()
|
||||
|
||||
@@ -58,39 +72,131 @@ func _ready() -> void:
|
||||
# STATE MODIFIERS
|
||||
# ==========================================
|
||||
func add_gold(amount: BigNumber) -> void:
|
||||
add_currency(CurrencyType.gold, amount)
|
||||
add_currency_by_id(GOLD_CURRENCY_ID, amount)
|
||||
|
||||
func spend_gold(cost: BigNumber) -> bool:
|
||||
return spend_currency(CurrencyType.gold, cost)
|
||||
return spend_currency_by_id(GOLD_CURRENCY_ID, cost)
|
||||
|
||||
func add_gems(amount: BigNumber) -> void:
|
||||
add_currency(CurrencyType.gems, amount)
|
||||
add_currency_by_id(GEMS_CURRENCY_ID, amount)
|
||||
|
||||
func spend_gems(cost: BigNumber) -> bool:
|
||||
return spend_currency(CurrencyType.gems, cost)
|
||||
return spend_currency_by_id(GEMS_CURRENCY_ID, cost)
|
||||
|
||||
func get_known_currencies() -> Array[Resource]:
|
||||
var result: Array[Resource] = []
|
||||
for raw_currency in _currency_by_id.values():
|
||||
var currency: Resource = raw_currency as Resource
|
||||
if currency != null:
|
||||
result.append(currency)
|
||||
return result
|
||||
|
||||
func get_currency_id(currency: Resource) -> StringName:
|
||||
if currency == null:
|
||||
return &""
|
||||
|
||||
var raw_id: Variant = currency.get("id")
|
||||
if raw_id is StringName:
|
||||
return _normalize_currency_id(raw_id)
|
||||
return _normalize_currency_id(StringName(String(raw_id)))
|
||||
|
||||
func parse_currency_id(raw_currency: Variant) -> StringName:
|
||||
if raw_currency is int:
|
||||
return currency_id_from_legacy_index(raw_currency)
|
||||
|
||||
var currency_text: String = String(raw_currency).to_lower().strip_edges()
|
||||
if currency_text == "gem":
|
||||
currency_text = "gems"
|
||||
return _normalize_currency_id(StringName(currency_text))
|
||||
|
||||
func currency_id_from_legacy_index(currency_index: int) -> StringName:
|
||||
return LEGACY_CURRENCY_INDEX_TO_ID.get(currency_index, &"")
|
||||
|
||||
func is_known_currency_id(currency_id: StringName) -> bool:
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
|
||||
if normalized_currency_id == &"":
|
||||
return false
|
||||
return _currency_by_id.has(normalized_currency_id)
|
||||
|
||||
func get_currency_resource(currency_id: StringName) -> Resource:
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
|
||||
if normalized_currency_id == &"":
|
||||
return null
|
||||
|
||||
var raw_currency: Variant = _currency_by_id.get(normalized_currency_id)
|
||||
if raw_currency is Resource:
|
||||
return raw_currency
|
||||
return null
|
||||
|
||||
func get_currency_name(currency_id: StringName) -> String:
|
||||
var currency: Resource = get_currency_resource(currency_id)
|
||||
if currency != null:
|
||||
var display_name: String = String(currency.get("display_name")).strip_edges()
|
||||
if not display_name.is_empty():
|
||||
return display_name
|
||||
return _humanize_currency_id(currency_id)
|
||||
|
||||
func get_currency_icon(currency_id: StringName) -> Texture2D:
|
||||
var currency: Resource = get_currency_resource(currency_id)
|
||||
if currency == null:
|
||||
return null
|
||||
var raw_icon: Variant = currency.get("icon")
|
||||
if raw_icon is Texture2D:
|
||||
return raw_icon
|
||||
return null
|
||||
|
||||
func add_currency(currency: Resource, amount: BigNumber) -> void:
|
||||
var currency_id: StringName = get_currency_id(currency)
|
||||
if currency_id == &"":
|
||||
push_warning("Attempted to add currency with an invalid Currency resource.")
|
||||
return
|
||||
|
||||
add_currency_by_id(currency_id, amount)
|
||||
|
||||
func spend_currency(currency: Resource, cost: BigNumber) -> bool:
|
||||
var currency_id: StringName = get_currency_id(currency)
|
||||
if currency_id == &"":
|
||||
push_warning("Attempted to spend currency with an invalid Currency resource.")
|
||||
return false
|
||||
|
||||
return spend_currency_by_id(currency_id, cost)
|
||||
|
||||
func add_currency_by_id(currency_id: StringName, amount: BigNumber) -> void:
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
|
||||
if normalized_currency_id == &"":
|
||||
return
|
||||
|
||||
func add_currency(currency: CurrencyType, amount: BigNumber) -> void:
|
||||
if amount.mantissa > 0.0:
|
||||
_get_total_currency_ref(currency).add_in_place(amount)
|
||||
_get_total_currency_ref(normalized_currency_id).add_in_place(amount)
|
||||
|
||||
var current: BigNumber = _get_current_currency_ref(currency)
|
||||
var current: BigNumber = _get_current_currency_ref(normalized_currency_id)
|
||||
current.add_in_place(amount)
|
||||
currency_changed.emit(currency, current)
|
||||
currency_changed.emit(normalized_currency_id, current)
|
||||
|
||||
func spend_currency(currency: CurrencyType, cost: BigNumber) -> bool:
|
||||
var current: BigNumber = _get_current_currency_ref(currency)
|
||||
func spend_currency_by_id(currency_id: StringName, cost: BigNumber) -> bool:
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
|
||||
if normalized_currency_id == &"":
|
||||
return false
|
||||
|
||||
var current: BigNumber = _get_current_currency_ref(normalized_currency_id)
|
||||
if current.is_greater_than(cost) or current.is_equal_to(cost):
|
||||
var negative_cost: BigNumber = BigNumber.new(-cost.mantissa, cost.exponent)
|
||||
current.add_in_place(negative_cost)
|
||||
currency_changed.emit(currency, current)
|
||||
currency_changed.emit(normalized_currency_id, current)
|
||||
return true
|
||||
return false
|
||||
|
||||
func get_currency_amount(currency: CurrencyType) -> BigNumber:
|
||||
return _get_current_currency_ref(currency)
|
||||
func get_currency_amount(currency: Resource) -> BigNumber:
|
||||
return get_currency_amount_by_id(get_currency_id(currency))
|
||||
|
||||
func get_total_currency_acquired(currency: CurrencyType) -> BigNumber:
|
||||
return _get_total_currency_ref(currency)
|
||||
func get_currency_amount_by_id(currency_id: StringName) -> BigNumber:
|
||||
return _get_current_currency_ref(currency_id)
|
||||
|
||||
func get_total_currency_acquired(currency: Resource) -> BigNumber:
|
||||
return get_total_currency_acquired_by_id(get_currency_id(currency))
|
||||
|
||||
func get_total_currency_acquired_by_id(currency_id: StringName) -> BigNumber:
|
||||
return _get_total_currency_ref(currency_id)
|
||||
|
||||
func register_generator(generator_id: StringName, unlocked: bool = false, available: bool = false) -> void:
|
||||
var key: String = String(generator_id)
|
||||
@@ -167,6 +273,8 @@ func set_generator_available(generator_id: StringName, value: bool) -> void:
|
||||
# ==========================================
|
||||
func save_game() -> void:
|
||||
var save_data = {
|
||||
CURRENCIES_KEY: _serialize_currency_balances(),
|
||||
# Keep legacy fields for compatibility with older builds.
|
||||
"gold": gold.serialize(),
|
||||
"gems": gems.serialize(),
|
||||
"total_gold_acquired": total_gold_acquired.serialize(),
|
||||
@@ -174,25 +282,56 @@ func save_game() -> void:
|
||||
"generator_states": _serialize_generator_states(),
|
||||
"last_save_time": Time.get_unix_time_from_system()
|
||||
}
|
||||
|
||||
var file = FileAccess.open(file_name, FileAccess.WRITE)
|
||||
|
||||
var file: FileAccess = FileAccess.open(file_name, FileAccess.WRITE)
|
||||
if file:
|
||||
file.store_string(JSON.stringify(save_data))
|
||||
|
||||
func load_game() -> void:
|
||||
if not FileAccess.file_exists(file_name):
|
||||
return
|
||||
|
||||
var file = FileAccess.open(file_name, FileAccess.READ)
|
||||
|
||||
var file: FileAccess = FileAccess.open(file_name, FileAccess.READ)
|
||||
if file:
|
||||
var parsed = JSON.parse_string(file.get_as_text())
|
||||
var parsed: Variant = JSON.parse_string(file.get_as_text())
|
||||
if parsed is Dictionary:
|
||||
gold = BigNumber.deserialize(parsed.get("gold", {}))
|
||||
gems = BigNumber.deserialize(parsed.get("gems", {}))
|
||||
total_gold_acquired = _deserialize_total_currency(parsed.get("total_gold_acquired", null), gold)
|
||||
total_gems_acquired = _deserialize_total_currency(parsed.get("total_gems_acquired", null), gems)
|
||||
generator_states = _deserialize_generator_states(parsed.get("generator_states", {}))
|
||||
last_save_time = parsed.get("last_save_time", Time.get_unix_time_from_system())
|
||||
var parsed_data: Dictionary = parsed
|
||||
if parsed_data.has(CURRENCIES_KEY):
|
||||
_load_currency_balances(parsed_data.get(CURRENCIES_KEY, {}))
|
||||
else:
|
||||
_load_legacy_currency_balances(parsed_data)
|
||||
|
||||
generator_states = _deserialize_generator_states(parsed_data.get("generator_states", {}))
|
||||
last_save_time = parsed_data.get("last_save_time", Time.get_unix_time_from_system())
|
||||
|
||||
func _load_legacy_currency_balances(parsed_data: Dictionary) -> void:
|
||||
var loaded_gold: BigNumber = BigNumber.deserialize(parsed_data.get("gold", {}))
|
||||
var loaded_gems: BigNumber = BigNumber.deserialize(parsed_data.get("gems", {}))
|
||||
|
||||
_set_current_currency(GOLD_CURRENCY_ID, loaded_gold)
|
||||
_set_current_currency(GEMS_CURRENCY_ID, loaded_gems)
|
||||
_set_total_currency(GOLD_CURRENCY_ID, _deserialize_total_currency(parsed_data.get("total_gold_acquired", null), loaded_gold))
|
||||
_set_total_currency(GEMS_CURRENCY_ID, _deserialize_total_currency(parsed_data.get("total_gems_acquired", null), loaded_gems))
|
||||
|
||||
func _load_currency_balances(raw_currencies: Variant) -> void:
|
||||
if not (raw_currencies is Dictionary):
|
||||
return
|
||||
|
||||
var currencies: Dictionary = raw_currencies
|
||||
for raw_currency_id in currencies.keys():
|
||||
var currency_id: StringName = _normalize_currency_id(StringName(String(raw_currency_id)))
|
||||
if currency_id == &"":
|
||||
continue
|
||||
|
||||
var raw_entry: Variant = currencies.get(raw_currency_id)
|
||||
if not (raw_entry is Dictionary):
|
||||
continue
|
||||
|
||||
var entry: Dictionary = raw_entry
|
||||
var current_amount: BigNumber = BigNumber.deserialize(entry.get(CURRENT_KEY, {}))
|
||||
var total_amount: BigNumber = _deserialize_total_currency(entry.get(TOTAL_KEY, null), current_amount)
|
||||
_set_current_currency(currency_id, current_amount)
|
||||
_set_total_currency(currency_id, total_amount)
|
||||
|
||||
func _deserialize_total_currency(raw_total: Variant, current_amount: BigNumber) -> BigNumber:
|
||||
var minimum_total: BigNumber = _copy_big_number(current_amount)
|
||||
@@ -212,35 +351,109 @@ func _deserialize_total_currency(raw_total: Variant, current_amount: BigNumber)
|
||||
func _copy_big_number(value: BigNumber) -> BigNumber:
|
||||
return BigNumber.new(value.mantissa, value.exponent)
|
||||
|
||||
func _serialize_currency_balances() -> Dictionary:
|
||||
var result: Dictionary = {}
|
||||
|
||||
for currency_id in _collect_currency_ids_for_save():
|
||||
var key: String = String(currency_id)
|
||||
if key.is_empty():
|
||||
continue
|
||||
|
||||
result[key] = {
|
||||
CURRENT_KEY: _get_current_currency_ref(currency_id).serialize(),
|
||||
TOTAL_KEY: _get_total_currency_ref(currency_id).serialize(),
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
func _collect_currency_ids_for_save() -> Array[StringName]:
|
||||
var ids: Array[StringName] = []
|
||||
|
||||
for raw_id in _current_currency.keys():
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(StringName(String(raw_id)))
|
||||
if normalized_currency_id == &"" or ids.has(normalized_currency_id):
|
||||
continue
|
||||
ids.append(normalized_currency_id)
|
||||
|
||||
for raw_id in _currency_by_id.keys():
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(StringName(String(raw_id)))
|
||||
if normalized_currency_id == &"" or ids.has(normalized_currency_id):
|
||||
continue
|
||||
ids.append(normalized_currency_id)
|
||||
|
||||
return ids
|
||||
|
||||
func _initialize_currency_catalog() -> void:
|
||||
_currency_by_id.clear()
|
||||
|
||||
for currency in DEFAULT_CURRENCIES:
|
||||
if currency == null:
|
||||
continue
|
||||
|
||||
var currency_id: StringName = get_currency_id(currency)
|
||||
if currency_id == &"":
|
||||
push_warning("Skipping currency with missing id in DEFAULT_CURRENCIES.")
|
||||
continue
|
||||
|
||||
_currency_by_id[currency_id] = currency
|
||||
|
||||
func _initialize_currency_maps() -> void:
|
||||
_set_current_currency(CurrencyType.gold, BigNumber.from_float(0.0))
|
||||
_set_current_currency(CurrencyType.gems, BigNumber.from_float(0.0))
|
||||
_set_total_currency(CurrencyType.gold, BigNumber.from_float(0.0))
|
||||
_set_total_currency(CurrencyType.gems, BigNumber.from_float(0.0))
|
||||
for currency in DEFAULT_CURRENCIES:
|
||||
var currency_id: StringName = get_currency_id(currency)
|
||||
if currency_id == &"":
|
||||
continue
|
||||
_set_current_currency(currency_id, BigNumber.from_float(0.0))
|
||||
_set_total_currency(currency_id, BigNumber.from_float(0.0))
|
||||
|
||||
func _get_current_currency_ref(currency: CurrencyType) -> BigNumber:
|
||||
var value: Variant = _current_currency.get(currency)
|
||||
func _normalize_currency_id(currency_id: StringName) -> StringName:
|
||||
var normalized: String = String(currency_id).to_lower().strip_edges()
|
||||
if normalized.is_empty():
|
||||
return &""
|
||||
return StringName(normalized)
|
||||
|
||||
func _humanize_currency_id(currency_id: StringName) -> String:
|
||||
var normalized: String = String(_normalize_currency_id(currency_id))
|
||||
if normalized.is_empty():
|
||||
return "Unknown"
|
||||
return normalized.replace("_", " ").capitalize()
|
||||
|
||||
func _get_current_currency_ref(currency_id: StringName) -> BigNumber:
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
|
||||
if normalized_currency_id == &"":
|
||||
return BigNumber.from_float(0.0)
|
||||
|
||||
var value: Variant = _current_currency.get(normalized_currency_id)
|
||||
if value is BigNumber:
|
||||
return value
|
||||
|
||||
var fallback: BigNumber = BigNumber.from_float(0.0)
|
||||
_current_currency[currency] = fallback
|
||||
_current_currency[normalized_currency_id] = fallback
|
||||
return fallback
|
||||
|
||||
func _get_total_currency_ref(currency: CurrencyType) -> BigNumber:
|
||||
var value: Variant = _total_currency_acquired.get(currency)
|
||||
func _get_total_currency_ref(currency_id: StringName) -> BigNumber:
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
|
||||
if normalized_currency_id == &"":
|
||||
return BigNumber.from_float(0.0)
|
||||
|
||||
var value: Variant = _total_currency_acquired.get(normalized_currency_id)
|
||||
if value is BigNumber:
|
||||
return value
|
||||
|
||||
var fallback: BigNumber = BigNumber.from_float(0.0)
|
||||
_total_currency_acquired[currency] = fallback
|
||||
_total_currency_acquired[normalized_currency_id] = fallback
|
||||
return fallback
|
||||
|
||||
func _set_current_currency(currency: CurrencyType, value: BigNumber) -> void:
|
||||
_current_currency[currency] = _copy_big_number(value)
|
||||
func _set_current_currency(currency_id: StringName, value: BigNumber) -> void:
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
|
||||
if normalized_currency_id == &"":
|
||||
return
|
||||
_current_currency[normalized_currency_id] = _copy_big_number(value)
|
||||
|
||||
func _set_total_currency(currency: CurrencyType, value: BigNumber) -> void:
|
||||
_total_currency_acquired[currency] = _copy_big_number(value)
|
||||
func _set_total_currency(currency_id: StringName, value: BigNumber) -> void:
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
|
||||
if normalized_currency_id == &"":
|
||||
return
|
||||
_total_currency_acquired[normalized_currency_id] = _copy_big_number(value)
|
||||
|
||||
func _default_generator_state(unlocked: bool, available: bool) -> Dictionary:
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user