Decouple everything from globals

This commit is contained in:
2026-04-10 01:09:57 +02:00
parent b758803afe
commit f671ab3419
56 changed files with 993 additions and 1291 deletions

View File

@@ -1,3 +1,4 @@
class_name PrestigeManager
extends Node
signal prestige_state_changed(total_prestige: BigNumber, pending_gain: BigNumber)
@@ -23,6 +24,7 @@ const MULTIPLIER_MODE_ADDITIVE: int = 0
const MULTIPLIER_MODE_MULTIPLICATIVE_POWER: int = 1
@export var config: Resource
@export var game_state: LevelGameState
var total_prestige_earned: BigNumber = BigNumber.from_float(0.0)
var current_prestige_unspent: BigNumber = BigNumber.from_float(0.0)
@@ -33,13 +35,17 @@ var last_reset_time: int = 0
func _ready() -> void:
if config == null:
config = load("res://docs/gyms/prestige/primary_prestige.tres") as Resource
config = load("res://docs/gyms/tiny_sword/prestige/primary_prestige.tres") as Resource
if not _is_config_valid():
push_warning("PrestigeManager has invalid or missing config; prestige is disabled.")
return
var loaded_state: Dictionary = GameState.get_external_save_data(SAVE_KEY)
if game_state == null:
push_warning("PrestigeManager: game_state reference is not set.")
return
var loaded_state: Dictionary = game_state.get_external_save_data(SAVE_KEY)
if loaded_state.is_empty():
_initialize_run_tracking_from_current_state()
else:
@@ -47,7 +53,7 @@ func _ready() -> void:
_validate_loaded_run_tracking()
_save_state_to_game_state()
GameState.currency_changed.connect(_on_currency_changed)
game_state.currency_changed.connect(_on_currency_changed)
prestige_state_changed.emit(get_total_prestige(), calculate_pending_gain())
prestige_threshold_changed.emit(get_next_prestige_threshold())
@@ -134,13 +140,16 @@ func perform_prestige() -> bool:
prestige_resets_count += 1
last_reset_time = Time.get_unix_time_from_system()
GameState.reset_for_prestige(true, false)
if game_state:
game_state.reset_for_prestige(true, false)
_reset_all_buff_levels()
_reinitialize_generators_after_prestige_reset()
GameState.emit_currency_changed_for_all()
if game_state:
game_state.emit_currency_changed_for_all()
_initialize_run_tracking_from_current_state()
_save_state_to_game_state()
GameState.save_game()
if game_state:
game_state.save_game()
var total: BigNumber = get_total_prestige()
var next_threshold: BigNumber = get_next_prestige_threshold()
@@ -261,9 +270,12 @@ func _get_basis_value() -> BigNumber:
if source_currency_id == &"":
return BigNumber.from_float(0.0)
if game_state == null:
return BigNumber.from_float(0.0)
match _get_config_int("basis", BASIS_LIFETIME_TOTAL):
BASIS_RUN_TOTAL:
var lifetime_now: BigNumber = GameState.get_total_currency_acquired_by_id(source_currency_id)
var lifetime_now: BigNumber = game_state.get_total_currency_acquired_by_id(source_currency_id)
var run_total: BigNumber = lifetime_now.subtract(run_start_total_source_currency)
if run_total.mantissa < 0.0:
return BigNumber.from_float(0.0)
@@ -271,7 +283,7 @@ func _get_basis_value() -> BigNumber:
BASIS_RUN_MAX:
return _copy_big_number(run_peak_source_currency)
_:
return _copy_big_number(GameState.get_total_currency_acquired_by_id(source_currency_id))
return _copy_big_number(game_state.get_total_currency_acquired_by_id(source_currency_id))
func _initialize_run_tracking_from_current_state() -> void:
if config == null:
@@ -283,8 +295,13 @@ func _initialize_run_tracking_from_current_state() -> void:
run_peak_source_currency = BigNumber.from_float(0.0)
return
run_start_total_source_currency = _copy_big_number(GameState.get_total_currency_acquired_by_id(source_currency_id))
run_peak_source_currency = _copy_big_number(GameState.get_currency_amount_by_id(source_currency_id))
if game_state == null:
run_start_total_source_currency = BigNumber.from_float(0.0)
run_peak_source_currency = BigNumber.from_float(0.0)
return
run_start_total_source_currency = _copy_big_number(game_state.get_total_currency_acquired_by_id(source_currency_id))
run_peak_source_currency = _copy_big_number(game_state.get_currency_amount_by_id(source_currency_id))
_save_state_to_game_state()
func _validate_loaded_run_tracking() -> void:
@@ -297,11 +314,14 @@ func _validate_loaded_run_tracking() -> void:
run_peak_source_currency = BigNumber.from_float(0.0)
return
var current_currency: BigNumber = GameState.get_currency_amount_by_id(source_currency_id)
if game_state == null:
return
var current_currency: BigNumber = game_state.get_currency_amount_by_id(source_currency_id)
if current_currency.is_greater_than(run_peak_source_currency):
run_peak_source_currency = _copy_big_number(current_currency)
var lifetime_now: BigNumber = GameState.get_total_currency_acquired_by_id(source_currency_id)
var lifetime_now: BigNumber = game_state.get_total_currency_acquired_by_id(source_currency_id)
if run_start_total_source_currency.is_greater_than(lifetime_now):
run_start_total_source_currency = _copy_big_number(lifetime_now)
@@ -327,12 +347,16 @@ func _deserialize_state(raw_state: Dictionary) -> void:
current_prestige_unspent = _copy_big_number(total_prestige_earned)
func _save_state_to_game_state() -> void:
GameState.set_external_save_data(SAVE_KEY, _serialize_state())
if game_state:
game_state.set_external_save_data(SAVE_KEY, _serialize_state())
func _reset_all_buff_levels() -> void:
for buff_id in GameState._buff_levels.keys():
GameState.set_buff_level(buff_id, 0)
GameState.set_buff_unlocked(buff_id, false)
if game_state == null:
return
for buff_id in game_state._buff_levels.keys():
game_state.set_buff_level(buff_id, 0)
game_state.set_buff_unlocked(buff_id, false)
func _reinitialize_generators_after_prestige_reset() -> void:
var scene_root: Node = get_tree().current_scene