Revamp goals as resources

This commit is contained in:
2026-03-19 23:46:24 +01:00
parent 3c8709c834
commit bcf228447d
25 changed files with 561 additions and 343 deletions

View File

@@ -7,6 +7,7 @@ extends Node
signal currency_changed(currency_id: StringName, new_amount: BigNumber)
signal generator_state_changed(generator_id: StringName, state: Dictionary)
signal generator_buff_level_changed(generator_id: StringName, buff_id: StringName, new_level: int)
signal generator_buff_unlocked_changed(generator_id: StringName, buff_id: StringName, unlocked: bool)
# ==========================================
# STATE VARIABLES
@@ -16,6 +17,7 @@ var _total_currency_acquired: Dictionary = {}
var generator_states: Dictionary = {}
var generator_buff_levels: Dictionary = {}
var generator_buff_unlocked: Dictionary = {}
var last_save_time: int = 0 # Unix timestamp
@@ -29,6 +31,7 @@ const GENERATOR_UNLOCKED_KEY: String = "unlocked"
const GENERATOR_AVAILABLE_KEY: String = "available"
const GENERATOR_STATES_KEY: String = "generator_states"
const GENERATOR_BUFF_LEVELS_KEY: String = "generator_buff_levels"
const GENERATOR_BUFF_UNLOCKED_KEY: String = "generator_buff_unlocked"
const CURRENCIES_KEY: String = "currencies"
const CURRENT_KEY: String = "current"
const TOTAL_KEY: String = "total"
@@ -217,6 +220,26 @@ func register_generator_buff(generator_id: StringName, buff_id: StringName, init
generator_buff_levels[generator_key] = levels
generator_buff_level_changed.emit(StringName(generator_key), StringName(buff_key), sanitized_level)
func register_generator_buff_unlocked(generator_id: StringName, buff_id: StringName, initially_unlocked: bool = false) -> void:
var generator_key: String = String(generator_id)
var buff_key: String = String(buff_id)
if generator_key.is_empty() or buff_key.is_empty():
return
var unlocked_map: Dictionary = _get_generator_buff_unlocked_ref(generator_id)
if unlocked_map.has(buff_key):
var existing_unlocked: bool = _to_bool(unlocked_map.get(buff_key, initially_unlocked), initially_unlocked)
if bool(unlocked_map.get(buff_key, initially_unlocked)) != existing_unlocked:
unlocked_map[buff_key] = existing_unlocked
generator_buff_unlocked[generator_key] = unlocked_map
generator_buff_unlocked_changed.emit(StringName(generator_key), StringName(buff_key), existing_unlocked)
return
var sanitized_unlocked: bool = _to_bool(initially_unlocked, false)
unlocked_map[buff_key] = sanitized_unlocked
generator_buff_unlocked[generator_key] = unlocked_map
generator_buff_unlocked_changed.emit(StringName(generator_key), StringName(buff_key), sanitized_unlocked)
func get_generator_buff_level(generator_id: StringName, buff_id: StringName) -> int:
var buff_key: String = String(buff_id)
if buff_key.is_empty():
@@ -240,6 +263,32 @@ func set_generator_buff_level(generator_id: StringName, buff_id: StringName, val
generator_buff_levels[generator_key] = levels
generator_buff_level_changed.emit(StringName(generator_key), StringName(buff_key), next_level)
func is_generator_buff_unlocked(generator_id: StringName, buff_id: StringName) -> bool:
var buff_key: String = String(buff_id)
if buff_key.is_empty():
return false
var unlocked_map: Dictionary = _get_generator_buff_unlocked_ref(generator_id)
return _to_bool(unlocked_map.get(buff_key, false), false)
func set_generator_buff_unlocked(generator_id: StringName, buff_id: StringName, value: bool) -> void:
var generator_key: String = String(generator_id)
var buff_key: String = String(buff_id)
if generator_key.is_empty() or buff_key.is_empty():
return
var unlocked_map: Dictionary = _get_generator_buff_unlocked_ref(generator_id)
var next_unlocked: bool = _to_bool(value, false)
if _to_bool(unlocked_map.get(buff_key, false), false) == next_unlocked:
return
unlocked_map[buff_key] = next_unlocked
generator_buff_unlocked[generator_key] = unlocked_map
generator_buff_unlocked_changed.emit(StringName(generator_key), StringName(buff_key), next_unlocked)
func get_generator_buff_unlocked(generator_id: StringName) -> Dictionary:
return _get_generator_buff_unlocked_ref(generator_id).duplicate(true)
func get_generator_buff_levels(generator_id: StringName) -> Dictionary:
return _get_generator_buff_levels_ref(generator_id).duplicate(true)
@@ -251,6 +300,7 @@ func save_game() -> void:
CURRENCIES_KEY: _serialize_currency_balances(),
GENERATOR_STATES_KEY: _serialize_generator_states(),
GENERATOR_BUFF_LEVELS_KEY: _serialize_generator_buff_levels(),
GENERATOR_BUFF_UNLOCKED_KEY: _serialize_generator_buff_unlocked(),
LAST_SAVE_TIME_KEY: Time.get_unix_time_from_system()
}
@@ -272,6 +322,7 @@ func load_game() -> void:
generator_states = _deserialize_generator_states(parsed_data.get(GENERATOR_STATES_KEY, {}))
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())
func _load_currency_balances(raw_currencies: Variant) -> void:
@@ -446,6 +497,21 @@ func _get_generator_buff_levels_ref(generator_id: StringName) -> Dictionary:
generator_buff_levels[key] = fallback
return fallback
func _get_generator_buff_unlocked_ref(generator_id: StringName) -> Dictionary:
var key: String = String(generator_id)
if key.is_empty():
return {}
var existing_raw: Variant = generator_buff_unlocked.get(key, {})
if existing_raw is Dictionary:
var sanitized: Dictionary = _sanitize_generator_buff_unlocked(existing_raw)
generator_buff_unlocked[key] = sanitized
return sanitized
var fallback: Dictionary = {}
generator_buff_unlocked[key] = fallback
return fallback
func _sanitize_generator_buff_levels(raw_levels: Dictionary) -> Dictionary:
var sanitized_levels: Dictionary = {}
for buff_key_variant in raw_levels.keys():
@@ -457,6 +523,22 @@ func _sanitize_generator_buff_levels(raw_levels: Dictionary) -> Dictionary:
return sanitized_levels
func _sanitize_generator_buff_unlocked(raw_unlocked: Dictionary) -> Dictionary:
var sanitized_unlocked: Dictionary = {}
for buff_key_variant in raw_unlocked.keys():
var buff_key: String = String(buff_key_variant)
if buff_key.is_empty():
continue
# Save format only stores true values, but keep parser tolerant.
var is_unlocked: bool = _to_bool(raw_unlocked.get(buff_key_variant, false), false)
if not is_unlocked:
continue
sanitized_unlocked[buff_key] = true
return sanitized_unlocked
func _serialize_generator_states() -> Dictionary:
var result: Dictionary = {}
for key_variant in generator_states.keys():
@@ -513,6 +595,47 @@ func _deserialize_generator_buff_levels(raw_value: Variant) -> Dictionary:
return result
func _serialize_generator_buff_unlocked() -> Dictionary:
var result: Dictionary = {}
for generator_key_variant in generator_buff_unlocked.keys():
var generator_key: String = String(generator_key_variant)
if generator_key.is_empty():
continue
var raw_unlocked: Variant = generator_buff_unlocked.get(generator_key_variant)
if not (raw_unlocked is Dictionary):
continue
var sanitized_unlocked: Dictionary = _sanitize_generator_buff_unlocked(raw_unlocked)
if sanitized_unlocked.is_empty():
continue
result[generator_key] = sanitized_unlocked
return result
func _deserialize_generator_buff_unlocked(raw_value: Variant) -> Dictionary:
var result: Dictionary = {}
if not (raw_value is Dictionary):
return result
for generator_key_variant in raw_value.keys():
var generator_key: String = String(generator_key_variant)
if generator_key.is_empty():
continue
var raw_unlocked: Variant = raw_value.get(generator_key_variant)
if not (raw_unlocked is Dictionary):
continue
var sanitized_unlocked: Dictionary = _sanitize_generator_buff_unlocked(raw_unlocked)
if sanitized_unlocked.is_empty():
continue
result[generator_key] = sanitized_unlocked
return result
func _serialize_generator_state_entry(raw_state: Dictionary) -> Dictionary:
var owned_value: int = _to_non_negative_int(raw_state.get(GENERATOR_OWNED_KEY, 0), 0)
var purchased_value: int = _to_non_negative_int(raw_state.get(GENERATOR_PURCHASED_COUNT_KEY, owned_value), owned_value)