Add prestige

This commit is contained in:
2026-03-22 10:50:37 +01:00
parent bb1a96b63c
commit c914b0c590
15 changed files with 1032 additions and 7 deletions

View File

@@ -14,10 +14,12 @@ signal generator_buff_unlocked_changed(generator_id: StringName, buff_id: String
# ==========================================
var _current_currency: Dictionary = {}
var _total_currency_acquired: Dictionary = {}
var _all_time_currency_acquired: Dictionary = {}
var generator_states: Dictionary = {}
var generator_buff_levels: Dictionary = {}
var generator_buff_unlocked: Dictionary = {}
var _external_save_sections: Dictionary = {}
var last_save_time: int = 0 # Unix timestamp
@@ -35,6 +37,7 @@ const GENERATOR_BUFF_UNLOCKED_KEY: String = "generator_buff_unlocked"
const CURRENCIES_KEY: String = "currencies"
const CURRENT_KEY: String = "current"
const TOTAL_KEY: String = "total"
const ALL_TIME_KEY: String = "all_time"
const LAST_SAVE_TIME_KEY: String = "last_save_time"
func _ready() -> void:
@@ -100,6 +103,7 @@ func add_currency_by_id(currency_id: StringName, amount: BigNumber) -> void:
if amount.mantissa > 0.0:
_get_total_currency_ref(normalized_currency_id).add_in_place(amount)
_get_all_time_currency_ref(normalized_currency_id).add_in_place(amount)
var current: BigNumber = _get_current_currency_ref(normalized_currency_id)
current.add_in_place(amount)
@@ -130,6 +134,12 @@ func get_total_currency_acquired(currency: Resource) -> BigNumber:
func get_total_currency_acquired_by_id(currency_id: StringName) -> BigNumber:
return _get_total_currency_ref(currency_id)
func get_all_time_currency_acquired(currency: Resource) -> BigNumber:
return get_all_time_currency_acquired_by_id(get_currency_id(currency))
func get_all_time_currency_acquired_by_id(currency_id: StringName) -> BigNumber:
return _get_all_time_currency_ref(currency_id)
func register_generator(generator_id: StringName, unlocked: bool = false, available: bool = false, owned: int = 0) -> void:
var key: String = String(generator_id)
if key.is_empty():
@@ -293,6 +303,35 @@ func get_generator_buff_unlocked(generator_id: StringName) -> Dictionary:
func get_generator_buff_levels(generator_id: StringName) -> Dictionary:
return _get_generator_buff_levels_ref(generator_id).duplicate(true)
func set_external_save_data(section_key: StringName, payload: Dictionary) -> void:
var key: String = String(section_key).strip_edges()
if key.is_empty():
return
_external_save_sections[key] = payload.duplicate(true)
func get_external_save_data(section_key: StringName) -> Dictionary:
var key: String = String(section_key).strip_edges()
if key.is_empty():
return {}
var raw_payload: Variant = _external_save_sections.get(key, {})
if raw_payload is Dictionary:
return raw_payload.duplicate(true)
return {}
func reset_for_prestige(reset_total_currency: bool = false) -> void:
for currency_id in _collect_currency_ids_for_save():
_set_current_currency(currency_id, BigNumber.from_float(0.0))
currency_changed.emit(currency_id, _get_current_currency_ref(currency_id))
if reset_total_currency:
_set_total_currency(currency_id, BigNumber.from_float(0.0))
generator_states.clear()
generator_buff_levels.clear()
generator_buff_unlocked.clear()
# ==========================================
# PERSISTENCE (Save/Load)
# ==========================================
@@ -305,6 +344,17 @@ func save_game() -> void:
LAST_SAVE_TIME_KEY: Time.get_unix_time_from_system()
}
for section_key in _external_save_sections.keys():
var key: String = String(section_key)
if key.is_empty():
continue
var payload: Variant = _external_save_sections.get(section_key)
if not (payload is Dictionary):
continue
save_data[key] = payload
var file: FileAccess = FileAccess.open(file_name, FileAccess.WRITE)
if file:
file.store_string(JSON.stringify(save_data))
@@ -318,6 +368,7 @@ func load_game() -> void:
var parsed: Variant = JSON.parse_string(file.get_as_text())
if parsed is Dictionary:
var parsed_data: Dictionary = parsed
_external_save_sections.clear()
if parsed_data.has(CURRENCIES_KEY):
_load_currency_balances(parsed_data.get(CURRENCIES_KEY, {}))
@@ -325,6 +376,16 @@ func load_game() -> void:
generator_buff_levels = _deserialize_generator_buff_levels(parsed_data.get(GENERATOR_BUFF_LEVELS_KEY, {}))
generator_buff_unlocked = _deserialize_generator_buff_unlocked(parsed_data.get(GENERATOR_BUFF_UNLOCKED_KEY, {}))
last_save_time = parsed_data.get(LAST_SAVE_TIME_KEY, Time.get_unix_time_from_system())
for save_key_variant in parsed_data.keys():
var save_key: String = String(save_key_variant)
if save_key.is_empty():
continue
if save_key in [CURRENCIES_KEY, GENERATOR_STATES_KEY, GENERATOR_BUFF_LEVELS_KEY, GENERATOR_BUFF_UNLOCKED_KEY, LAST_SAVE_TIME_KEY]:
continue
var section_payload: Variant = parsed_data.get(save_key_variant)
if section_payload is Dictionary:
_external_save_sections[save_key] = section_payload.duplicate(true)
func _load_currency_balances(raw_currencies: Variant) -> void:
if not (raw_currencies is Dictionary):
@@ -343,8 +404,10 @@ func _load_currency_balances(raw_currencies: Variant) -> void:
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)
var all_time_amount: BigNumber = _deserialize_all_time_currency(entry.get(ALL_TIME_KEY, null), total_amount)
_set_current_currency(currency_id, current_amount)
_set_total_currency(currency_id, total_amount)
_set_all_time_currency(currency_id, all_time_amount)
func _deserialize_total_currency(raw_total: Variant, current_amount: BigNumber) -> BigNumber:
var minimum_total: BigNumber = _copy_big_number(current_amount)
@@ -361,6 +424,21 @@ func _deserialize_total_currency(raw_total: Variant, current_amount: BigNumber)
return minimum_total
return parsed_total
func _deserialize_all_time_currency(raw_all_time: Variant, total_amount: BigNumber) -> BigNumber:
var minimum_all_time: BigNumber = _copy_big_number(total_amount)
if minimum_all_time.mantissa < 0.0:
minimum_all_time = BigNumber.from_float(0.0)
if not (raw_all_time is Dictionary):
return minimum_all_time
var parsed_all_time: BigNumber = BigNumber.deserialize(raw_all_time)
if parsed_all_time.mantissa < 0.0:
return minimum_all_time
if parsed_all_time.is_less_than(minimum_all_time):
return minimum_all_time
return parsed_all_time
func _copy_big_number(value: BigNumber) -> BigNumber:
return BigNumber.new(value.mantissa, value.exponent)
@@ -375,6 +453,7 @@ func _serialize_currency_balances() -> Dictionary:
result[key] = {
CURRENT_KEY: _get_current_currency_ref(currency_id).serialize(),
TOTAL_KEY: _get_total_currency_ref(currency_id).serialize(),
ALL_TIME_KEY: _get_all_time_currency_ref(currency_id).serialize(),
}
return result
@@ -388,6 +467,12 @@ func _collect_currency_ids_for_save() -> Array[StringName]:
continue
ids.append(normalized_currency_id)
for raw_id in _all_time_currency_acquired.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 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):
@@ -402,6 +487,7 @@ func _initialize_currency_maps() -> void:
continue
_set_current_currency(currency_id, BigNumber.from_float(0.0))
_set_total_currency(currency_id, BigNumber.from_float(0.0))
_set_all_time_currency(currency_id, BigNumber.from_float(0.0))
func _normalize_currency_id(currency_id: StringName) -> StringName:
var normalized: String = String(currency_id).to_lower().strip_edges()
@@ -435,6 +521,19 @@ func _get_total_currency_ref(currency_id: StringName) -> BigNumber:
_total_currency_acquired[normalized_currency_id] = fallback
return fallback
func _get_all_time_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 = _all_time_currency_acquired.get(normalized_currency_id)
if value is BigNumber:
return value
var fallback: BigNumber = BigNumber.from_float(0.0)
_all_time_currency_acquired[normalized_currency_id] = fallback
return fallback
func _set_current_currency(currency_id: StringName, value: BigNumber) -> void:
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
if normalized_currency_id == &"":
@@ -447,6 +546,12 @@ func _set_total_currency(currency_id: StringName, value: BigNumber) -> void:
return
_total_currency_acquired[normalized_currency_id] = _copy_big_number(value)
func _set_all_time_currency(currency_id: StringName, value: BigNumber) -> void:
var normalized_currency_id: StringName = _normalize_currency_id(currency_id)
if normalized_currency_id == &"":
return
_all_time_currency_acquired[normalized_currency_id] = _copy_big_number(value)
func _default_generator_state(unlocked: bool, available: bool, owned: int = 0) -> Dictionary:
var owned_value: int = _to_non_negative_int(owned, 0)
return {