Revamp goals as resources
This commit is contained in:
@@ -1,22 +1,16 @@
|
||||
extends PanelContainer
|
||||
|
||||
class GoalRequirement extends RefCounted:
|
||||
var currency_id: StringName
|
||||
var amount: BigNumber
|
||||
|
||||
func _init(req_currency_id: StringName, req_amount: BigNumber) -> void:
|
||||
currency_id = req_currency_id
|
||||
amount = req_amount
|
||||
|
||||
class GoalDefinition extends RefCounted:
|
||||
var id: StringName
|
||||
var target_generator_id: StringName
|
||||
var requirements: Array[GoalRequirement]
|
||||
var goal: Resource
|
||||
|
||||
func _init(goal_id: StringName, target_id: StringName, goal_requirements: Array[GoalRequirement]) -> void:
|
||||
id = goal_id
|
||||
func _init(target_id: StringName, goal_data: Resource) -> void:
|
||||
target_generator_id = target_id
|
||||
requirements = goal_requirements
|
||||
goal = goal_data
|
||||
|
||||
const GOAL_DATA_SCRIPT: Script = preload("res://core/goals/goal_data.gd")
|
||||
const GOAL_REQUIREMENT_SCRIPT: Script = preload("res://core/goals/goal_requirement_data.gd")
|
||||
const GENERATOR_UNLOCK_GOAL_DATA_SCRIPT: Script = preload("res://core/generator-unlock-goals/generator_unlock_goal_data.gd")
|
||||
|
||||
class GoalRow extends RefCounted:
|
||||
var goal: GoalDefinition
|
||||
@@ -30,12 +24,12 @@ class GoalRow extends RefCounted:
|
||||
requirements_label = requirements
|
||||
button = unlock_button
|
||||
|
||||
const GOALS_FILE_PATH: String = "res://idles/generator_unlock_goals.json"
|
||||
@export var goals: Array[GeneratorUnlockGoalData] = []
|
||||
|
||||
@onready var _summary_label: Label = $MarginContainer/VBoxContainer/SummaryLabel
|
||||
@onready var _goals_list: VBoxContainer = $MarginContainer/VBoxContainer/ScrollContainer/GoalsList
|
||||
|
||||
var _goals: Array[GoalDefinition] = []
|
||||
var _loaded_goals: Array[GoalDefinition] = []
|
||||
var _rows_by_goal_id: Dictionary = {}
|
||||
var _known_generator_ids: Dictionary = {}
|
||||
var _warned_unknown_target_ids: Dictionary = {}
|
||||
@@ -59,101 +53,52 @@ func _on_generator_state_changed(generator_id: StringName, _state: Dictionary) -
|
||||
_refresh_ui()
|
||||
|
||||
func _load_goals() -> void:
|
||||
_goals.clear()
|
||||
|
||||
if not FileAccess.file_exists(GOALS_FILE_PATH):
|
||||
push_warning("Goals debug UI: goals file not found at %s" % GOALS_FILE_PATH)
|
||||
return
|
||||
|
||||
var file: FileAccess = FileAccess.open(GOALS_FILE_PATH, FileAccess.READ)
|
||||
if file == null:
|
||||
push_warning("Goals debug UI: could not open goals file at %s" % GOALS_FILE_PATH)
|
||||
return
|
||||
|
||||
var parsed: Variant = JSON.parse_string(file.get_as_text())
|
||||
if not (parsed is Dictionary):
|
||||
push_warning("Goals debug UI: goals root must be a Dictionary")
|
||||
return
|
||||
|
||||
var goals_raw: Variant = parsed.get("goals", [])
|
||||
if not (goals_raw is Array):
|
||||
push_warning("Goals debug UI: goals must be an Array")
|
||||
return
|
||||
_loaded_goals.clear()
|
||||
|
||||
var seen_goal_ids: Dictionary = {}
|
||||
for raw_goal in goals_raw:
|
||||
var goal: GoalDefinition = _parse_goal(raw_goal)
|
||||
if goal == null:
|
||||
for goal_resource in goals:
|
||||
if goal_resource == null:
|
||||
continue
|
||||
if goal_resource.get_script() != GENERATOR_UNLOCK_GOAL_DATA_SCRIPT:
|
||||
continue
|
||||
var goal_data: Resource = goal_resource
|
||||
if goal_data == null:
|
||||
continue
|
||||
var target_generator_id: String = String(goal_data.get("target_generator_id")).strip_edges()
|
||||
if target_generator_id.is_empty():
|
||||
continue
|
||||
var resolved_goal: Resource = goal_data.call("get_goal_data")
|
||||
if resolved_goal == null:
|
||||
continue
|
||||
if not bool(resolved_goal.call("is_valid")):
|
||||
continue
|
||||
|
||||
var goal_key: String = String(goal.id)
|
||||
var goal_key: String = String(resolved_goal.get("id"))
|
||||
if seen_goal_ids.has(goal_key):
|
||||
push_warning("Goals debug UI: duplicate goal id '%s' skipped" % goal_key)
|
||||
continue
|
||||
|
||||
seen_goal_ids[goal_key] = true
|
||||
_goals.append(goal)
|
||||
|
||||
func _parse_goal(raw_goal: Variant) -> GoalDefinition:
|
||||
if not (raw_goal is Dictionary):
|
||||
return null
|
||||
|
||||
var goal_dict: Dictionary = raw_goal
|
||||
var goal_id: String = String(goal_dict.get("id", "")).strip_edges()
|
||||
var target_id: String = String(goal_dict.get("target_generator_id", "")).strip_edges()
|
||||
if goal_id.is_empty() or target_id.is_empty():
|
||||
return null
|
||||
|
||||
var raw_requirements: Variant = goal_dict.get("requirements", [])
|
||||
if not (raw_requirements is Array):
|
||||
return null
|
||||
|
||||
var requirements: Array[GoalRequirement] = []
|
||||
for raw_requirement in raw_requirements:
|
||||
var requirement: GoalRequirement = _parse_requirement(raw_requirement)
|
||||
if requirement != null:
|
||||
requirements.append(requirement)
|
||||
|
||||
if requirements.is_empty():
|
||||
return null
|
||||
|
||||
return GoalDefinition.new(StringName(goal_id), StringName(target_id), requirements)
|
||||
|
||||
func _parse_requirement(raw_requirement: Variant) -> GoalRequirement:
|
||||
if not (raw_requirement is Dictionary):
|
||||
return null
|
||||
|
||||
var requirement_dict: Dictionary = raw_requirement
|
||||
var currency_id: StringName = _parse_currency(requirement_dict.get("currency", ""))
|
||||
if currency_id == &"" or not GameState.is_known_currency_id(currency_id):
|
||||
return null
|
||||
|
||||
var raw_amount: Variant = requirement_dict.get("amount", {})
|
||||
if not (raw_amount is Dictionary):
|
||||
return null
|
||||
|
||||
var amount: BigNumber = BigNumber.deserialize(raw_amount)
|
||||
if amount.mantissa < 0.0:
|
||||
return null
|
||||
|
||||
return GoalRequirement.new(currency_id, amount)
|
||||
|
||||
func _parse_currency(raw_currency: Variant) -> StringName:
|
||||
return GameState.parse_currency_id(raw_currency)
|
||||
_loaded_goals.append(GoalDefinition.new(StringName(target_generator_id), resolved_goal))
|
||||
|
||||
func _build_goal_rows() -> void:
|
||||
_rows_by_goal_id.clear()
|
||||
for child in _goals_list.get_children():
|
||||
child.queue_free()
|
||||
|
||||
for goal in _goals:
|
||||
for goal in _loaded_goals:
|
||||
if goal.goal == null:
|
||||
continue
|
||||
if goal.goal.get_script() != GOAL_DATA_SCRIPT:
|
||||
continue
|
||||
|
||||
var row_container: HBoxContainer = HBoxContainer.new()
|
||||
row_container.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||||
row_container.add_theme_constant_override("separation", 12)
|
||||
|
||||
var title_label: Label = Label.new()
|
||||
title_label.custom_minimum_size = Vector2(220.0, 0.0)
|
||||
title_label.text = "%s -> %s" % [String(goal.id), String(goal.target_generator_id)]
|
||||
title_label.text = "%s -> %s" % [String(goal.goal.get("id")), String(goal.target_generator_id)]
|
||||
|
||||
var status_label: Label = Label.new()
|
||||
status_label.custom_minimum_size = Vector2(95.0, 0.0)
|
||||
@@ -165,7 +110,7 @@ func _build_goal_rows() -> void:
|
||||
unlock_button.custom_minimum_size = Vector2(90.0, 0.0)
|
||||
unlock_button.text = "Unlock"
|
||||
unlock_button.disabled = true
|
||||
unlock_button.pressed.connect(_on_unlock_pressed.bind(goal.id))
|
||||
unlock_button.pressed.connect(_on_unlock_pressed.bind(goal.goal.get("id")))
|
||||
|
||||
row_container.add_child(title_label)
|
||||
row_container.add_child(status_label)
|
||||
@@ -173,17 +118,22 @@ func _build_goal_rows() -> void:
|
||||
row_container.add_child(unlock_button)
|
||||
_goals_list.add_child(row_container)
|
||||
|
||||
_rows_by_goal_id[String(goal.id)] = GoalRow.new(goal, status_label, requirements_label, unlock_button)
|
||||
_rows_by_goal_id[String(goal.goal.get("id"))] = GoalRow.new(goal, status_label, requirements_label, unlock_button)
|
||||
|
||||
func _refresh_ui() -> void:
|
||||
if _goals.is_empty():
|
||||
if _loaded_goals.is_empty():
|
||||
_summary_label.text = "No valid goals loaded"
|
||||
return
|
||||
|
||||
var ready_to_unlock: int = 0
|
||||
var unlocked: int = 0
|
||||
for goal in _goals:
|
||||
var row: GoalRow = _rows_by_goal_id.get(String(goal.id))
|
||||
for goal in _loaded_goals:
|
||||
if goal.goal == null:
|
||||
continue
|
||||
if goal.goal.get_script() != GOAL_DATA_SCRIPT:
|
||||
continue
|
||||
|
||||
var row: GoalRow = _rows_by_goal_id.get(String(goal.goal.get("id")))
|
||||
if row == null:
|
||||
continue
|
||||
_refresh_goal_row(row)
|
||||
@@ -193,7 +143,7 @@ func _refresh_ui() -> void:
|
||||
elif _is_goal_met(goal):
|
||||
ready_to_unlock += 1
|
||||
|
||||
_summary_label.text = "Goals: %d | Ready: %d | Unlocked: %d" % [_goals.size(), ready_to_unlock, unlocked]
|
||||
_summary_label.text = "Goals: %d | Ready: %d | Unlocked: %d" % [_loaded_goals.size(), ready_to_unlock, unlocked]
|
||||
|
||||
func _refresh_goal_row(row: GoalRow) -> void:
|
||||
var goal: GoalDefinition = row.goal
|
||||
@@ -221,11 +171,14 @@ func _refresh_goal_row(row: GoalRow) -> void:
|
||||
row.status_label.text = "Locked"
|
||||
|
||||
func _is_goal_met(goal: GoalDefinition) -> bool:
|
||||
for requirement in goal.requirements:
|
||||
var total_amount: BigNumber = GameState.get_total_currency_acquired_by_id(requirement.currency_id)
|
||||
if total_amount.is_less_than(requirement.amount):
|
||||
return false
|
||||
return true
|
||||
if goal == null:
|
||||
return false
|
||||
if goal.goal == null:
|
||||
return false
|
||||
if goal.goal.get_script() != GOAL_DATA_SCRIPT:
|
||||
return false
|
||||
|
||||
return bool(goal.goal.call("is_met"))
|
||||
|
||||
func _is_goal_completed(goal: GoalDefinition) -> bool:
|
||||
return (
|
||||
@@ -248,7 +201,7 @@ func _on_unlock_pressed(goal_id: StringName) -> void:
|
||||
|
||||
GameState.set_generator_unlocked(goal.target_generator_id, true)
|
||||
GameState.set_generator_available(goal.target_generator_id, true)
|
||||
print("[GoalsDebugUI] Unlocked goal '%s' -> %s" % [String(goal.id), String(goal.target_generator_id)])
|
||||
print("[GoalsDebugUI] Unlocked goal '%s' -> %s" % [String(goal.goal.get("id")), String(goal.target_generator_id)])
|
||||
_refresh_ui()
|
||||
|
||||
func _can_resolve_target(target_generator_id: StringName) -> bool:
|
||||
@@ -282,14 +235,28 @@ func _collect_known_generator_ids() -> void:
|
||||
_known_generator_ids[generator_key] = true
|
||||
|
||||
func _get_requirements_text(goal: GoalDefinition) -> String:
|
||||
if goal == null:
|
||||
return ""
|
||||
if goal.goal == null:
|
||||
return ""
|
||||
if goal.goal.get_script() != GOAL_DATA_SCRIPT:
|
||||
return ""
|
||||
|
||||
var parts: Array[String] = []
|
||||
for requirement in goal.requirements:
|
||||
var total_amount: BigNumber = GameState.get_total_currency_acquired_by_id(requirement.currency_id)
|
||||
for requirement_resource in goal.goal.call("get_valid_requirements"):
|
||||
if requirement_resource == null:
|
||||
continue
|
||||
if requirement_resource.get_script() != GOAL_REQUIREMENT_SCRIPT:
|
||||
continue
|
||||
|
||||
var requirement_currency_id: StringName = requirement_resource.call("get_currency_id")
|
||||
var total_amount: BigNumber = GameState.get_total_currency_acquired_by_id(requirement_currency_id)
|
||||
var required_amount: BigNumber = requirement_resource.call("get_amount")
|
||||
parts.append(
|
||||
"%s %s / %s" % [
|
||||
_currency_label(requirement.currency_id),
|
||||
_currency_label(requirement_currency_id),
|
||||
total_amount.to_string_suffix(2),
|
||||
requirement.amount.to_string_suffix(2)
|
||||
required_amount.to_string_suffix(2)
|
||||
]
|
||||
)
|
||||
return " | ".join(parts)
|
||||
|
||||
Reference in New Issue
Block a user