Add currency database, cleanup legacy system
This commit is contained in:
161
game_state.gd
161
game_state.gd
@@ -7,54 +7,20 @@ extends Node
|
||||
signal currency_changed(currency_id: StringName, new_amount: BigNumber)
|
||||
signal generator_state_changed(generator_id: StringName, state: Dictionary)
|
||||
|
||||
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(GOLD_CURRENCY_ID)
|
||||
set(value):
|
||||
_set_current_currency(GOLD_CURRENCY_ID, value)
|
||||
|
||||
var gems: BigNumber:
|
||||
get:
|
||||
return _get_current_currency_ref(GEMS_CURRENCY_ID)
|
||||
set(value):
|
||||
_set_current_currency(GEMS_CURRENCY_ID, value)
|
||||
|
||||
var total_gold_acquired: BigNumber:
|
||||
get:
|
||||
return _get_total_currency_ref(GOLD_CURRENCY_ID)
|
||||
set(value):
|
||||
_set_total_currency(GOLD_CURRENCY_ID, value)
|
||||
|
||||
var total_gems_acquired: BigNumber:
|
||||
get:
|
||||
return _get_total_currency_ref(GEMS_CURRENCY_ID)
|
||||
set(value):
|
||||
_set_total_currency(GEMS_CURRENCY_ID, value)
|
||||
|
||||
var generator_states: Dictionary = {}
|
||||
|
||||
var last_save_time: int = 0 # Unix timestamp
|
||||
|
||||
# ==========================================
|
||||
# Save / Load
|
||||
# ==========================================
|
||||
static var file_name: String = "user://idle_save.json"
|
||||
|
||||
const GENERATOR_OWNED_KEY: String = "owned"
|
||||
const GENERATOR_PURCHASED_COUNT_KEY: String = "purchased_count"
|
||||
const GENERATOR_UNLOCKED_KEY: String = "unlocked"
|
||||
@@ -64,86 +30,44 @@ const CURRENT_KEY: String = "current"
|
||||
const TOTAL_KEY: String = "total"
|
||||
|
||||
func _ready() -> void:
|
||||
_initialize_currency_catalog()
|
||||
_initialize_currency_maps()
|
||||
load_game()
|
||||
|
||||
# ==========================================
|
||||
# STATE MODIFIERS
|
||||
# ==========================================
|
||||
func add_gold(amount: BigNumber) -> void:
|
||||
add_currency_by_id(GOLD_CURRENCY_ID, amount)
|
||||
|
||||
func spend_gold(cost: BigNumber) -> bool:
|
||||
return spend_currency_by_id(GOLD_CURRENCY_ID, cost)
|
||||
|
||||
func add_gems(amount: BigNumber) -> void:
|
||||
add_currency_by_id(GEMS_CURRENCY_ID, amount)
|
||||
|
||||
func spend_gems(cost: BigNumber) -> bool:
|
||||
return spend_currency_by_id(GEMS_CURRENCY_ID, cost)
|
||||
#func add_gold(amount: BigNumber) -> void:
|
||||
#add_currency_by_id(GOLD_CURRENCY_ID, amount)
|
||||
#
|
||||
#func spend_gold(cost: BigNumber) -> bool:
|
||||
#return spend_currency_by_id(GOLD_CURRENCY_ID, cost)
|
||||
#
|
||||
#func add_gems(amount: BigNumber) -> void:
|
||||
#add_currency_by_id(GEMS_CURRENCY_ID, amount)
|
||||
#
|
||||
#func spend_gems(cost: BigNumber) -> bool:
|
||||
#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
|
||||
return CurrencyDatabase.get_known_currencies()
|
||||
|
||||
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)))
|
||||
return CurrencyDatabase.get_currency_id(currency)
|
||||
|
||||
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, &"")
|
||||
return CurrencyDatabase.parse_currency_id(raw_currency)
|
||||
|
||||
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)
|
||||
return CurrencyDatabase.is_known_currency_id(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
|
||||
return CurrencyDatabase.get_currency_resource(currency_id)
|
||||
|
||||
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)
|
||||
return CurrencyDatabase.get_currency_name(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
|
||||
return CurrencyDatabase.get_currency_icon(currency_id)
|
||||
|
||||
func add_currency(currency: Resource, amount: BigNumber) -> void:
|
||||
var currency_id: StringName = get_currency_id(currency)
|
||||
@@ -274,11 +198,6 @@ 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(),
|
||||
"total_gems_acquired": total_gems_acquired.serialize(),
|
||||
"generator_states": _serialize_generator_states(),
|
||||
"last_save_time": Time.get_unix_time_from_system()
|
||||
}
|
||||
@@ -298,21 +217,10 @@ func load_game() -> void:
|
||||
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
|
||||
@@ -375,31 +283,16 @@ func _collect_currency_ids_for_save() -> Array[StringName]:
|
||||
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)))
|
||||
for known_currency_id in CurrencyDatabase.get_known_currency_ids():
|
||||
var normalized_currency_id: StringName = _normalize_currency_id(known_currency_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:
|
||||
for currency in DEFAULT_CURRENCIES:
|
||||
var currency_id: StringName = get_currency_id(currency)
|
||||
for currency_id in CurrencyDatabase.get_known_currency_ids():
|
||||
if currency_id == &"":
|
||||
continue
|
||||
_set_current_currency(currency_id, BigNumber.from_float(0.0))
|
||||
@@ -411,12 +304,6 @@ func _normalize_currency_id(currency_id: StringName) -> StringName:
|
||||
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 == &"":
|
||||
|
||||
Reference in New Issue
Block a user