Decouple everything from globals
This commit is contained in:
@@ -25,6 +25,7 @@ class GoalRow extends RefCounted:
|
||||
|
||||
@onready var _summary_label: Label = $MarginContainer/VBoxContainer/SummaryLabel
|
||||
@onready var _goals_list: VBoxContainer = $MarginContainer/VBoxContainer/ScrollContainer/GoalsList
|
||||
@onready var game_state: LevelGameState = find_parent("LevelGameState")
|
||||
|
||||
var _loaded_goals: Array[GoalDefinition] = []
|
||||
var _rows_by_goal_id: Dictionary = {}
|
||||
@@ -36,9 +37,10 @@ func _ready() -> void:
|
||||
_collect_known_generator_ids()
|
||||
_load_goals()
|
||||
_build_goal_rows()
|
||||
GameState.currency_changed.connect(_on_currency_changed)
|
||||
GameState.generator_state_changed.connect(_on_generator_state_changed)
|
||||
GameState.goal_completed.connect(_on_goal_completed)
|
||||
if game_state:
|
||||
game_state.currency_changed.connect(_on_currency_changed)
|
||||
game_state.generator_state_changed.connect(_on_generator_state_changed)
|
||||
game_state.goal_completed.connect(_on_goal_completed)
|
||||
_refresh_ui()
|
||||
_refresh_ui.call_deferred()
|
||||
|
||||
@@ -74,7 +76,9 @@ func _load_goals() -> void:
|
||||
_generators_by_id[String(generator_id)] = generator
|
||||
|
||||
var seen_goal_ids: Dictionary = {}
|
||||
for goal in GameState.get_all_goals():
|
||||
var all_goals_raw: Array = game_state.get_all_goals() if game_state else []
|
||||
for goal_raw in all_goals_raw:
|
||||
var goal: GoalData = goal_raw as GoalData
|
||||
if goal == null or not goal.has_id():
|
||||
continue
|
||||
|
||||
@@ -182,13 +186,17 @@ func _is_goal_met(goal: GoalDefinition) -> bool:
|
||||
return false
|
||||
if goal.goal == null:
|
||||
return false
|
||||
if game_state == null:
|
||||
return false
|
||||
|
||||
return bool(goal.goal.is_met())
|
||||
return bool(game_state.is_goal_met(goal.goal))
|
||||
|
||||
func _is_goal_completed(goal: GoalDefinition) -> bool:
|
||||
if goal.goal == null or goal.goal.id.is_empty():
|
||||
return false
|
||||
return GameState.is_goal_completed(goal.goal.id)
|
||||
if game_state == null:
|
||||
return false
|
||||
return game_state.is_goal_completed(goal.goal.id)
|
||||
|
||||
func _on_unlock_pressed(goal_id: StringName) -> void:
|
||||
var row: GoalRow = _rows_by_goal_id.get(String(goal_id))
|
||||
@@ -203,14 +211,17 @@ func _on_unlock_pressed(goal_id: StringName) -> void:
|
||||
if not _is_goal_met(goal):
|
||||
return
|
||||
|
||||
if game_state == null:
|
||||
return
|
||||
|
||||
var generator: CurrencyGenerator = _get_generator_for_goal(goal)
|
||||
if generator == null:
|
||||
return
|
||||
|
||||
GameState.set_generator_unlocked(goal.target_generator_id, true)
|
||||
GameState.set_generator_available(goal.target_generator_id, true)
|
||||
game_state.set_generator_unlocked(goal.target_generator_id, true)
|
||||
game_state.set_generator_available(goal.target_generator_id, true)
|
||||
if goal.goal.unlock_behavior == GoalData.UnlockBehavior.MANUAL:
|
||||
GameState._complete_goal_manually(goal.goal.id)
|
||||
game_state._complete_goal_manually(goal.goal.id)
|
||||
|
||||
print("[GoalsDebugUI] Unlocked goal '%s' -> %s" % [String(goal.goal.id), String(goal.target_generator_id)])
|
||||
_refresh_ui()
|
||||
@@ -220,7 +231,7 @@ func _can_resolve_target(target_generator_id: StringName) -> bool:
|
||||
if target_key.is_empty():
|
||||
return false
|
||||
|
||||
if _known_generator_ids.has(target_key) or GameState.generator_states.has(target_key):
|
||||
if _known_generator_ids.has(target_key) or (game_state and game_state.generator_states.has(target_key)):
|
||||
return true
|
||||
|
||||
if not _warned_unknown_target_ids.has(target_key):
|
||||
@@ -292,16 +303,22 @@ func _get_requirements_text(goal: GoalDefinition) -> String:
|
||||
return ""
|
||||
if goal.goal.get_script() != GOAL_DATA_SCRIPT:
|
||||
return ""
|
||||
if game_state == null:
|
||||
return ""
|
||||
|
||||
var parts: Array[String] = []
|
||||
for requirement_resource in goal.goal.get_valid_requirements():
|
||||
for requirement_resource in goal.goal.get_requirements():
|
||||
if requirement_resource == null:
|
||||
continue
|
||||
if requirement_resource.get_script() != GOAL_REQUIREMENT_SCRIPT:
|
||||
continue
|
||||
|
||||
var requirement_currency_id: StringName = requirement_resource.get_currency_id()
|
||||
var total_amount: BigNumber = GameState.get_total_currency_acquired_by_id(requirement_currency_id)
|
||||
var currency: Currency = requirement_resource.get_currency()
|
||||
if currency == null:
|
||||
continue
|
||||
|
||||
var requirement_currency_id: StringName = game_state.get_currency_id(currency)
|
||||
var total_amount: BigNumber = game_state.get_total_currency_acquired_by_id(requirement_currency_id)
|
||||
var required_amount: BigNumber = requirement_resource.get_amount()
|
||||
parts.append(
|
||||
"%s %s / %s" % [
|
||||
@@ -316,4 +333,6 @@ func _get_requirements_text(goal: GoalDefinition) -> String:
|
||||
return " | ".join(parts)
|
||||
|
||||
func _currency_label(currency_id: StringName) -> String:
|
||||
return GameState.get_currency_name(currency_id)
|
||||
if game_state == null:
|
||||
return "Unknown"
|
||||
return game_state.get_currency_name(currency_id)
|
||||
|
||||
Reference in New Issue
Block a user