Replace generic Resource usage with type-safe resources
This commit is contained in:
@@ -4,16 +4,13 @@ signal goal_achieved(goal_id: StringName, target_generator_id: StringName)
|
||||
|
||||
class GeneratorUnlockGoal extends RefCounted:
|
||||
var target_generator_id: StringName
|
||||
var goal: Resource
|
||||
var goal: GoalData
|
||||
|
||||
func _init(target_id: StringName, goal_data: Resource) -> void:
|
||||
func _init(target_id: StringName, goal_data: GoalData) -> void:
|
||||
target_generator_id = target_id
|
||||
goal = goal_data
|
||||
|
||||
const GOAL_DATA_SCRIPT: Script = preload("res://core/goals/goal_data.gd")
|
||||
const GENERATOR_UNLOCK_GOAL_DATA_SCRIPT: Script = preload("res://core/generator-unlock-goals/generator_unlock_goal_data.gd")
|
||||
|
||||
@export var goals: Array[Resource] = []
|
||||
@export var goals: Array[GeneratorUnlockGoalData] = []
|
||||
|
||||
var _runtime_goals: Array[GeneratorUnlockGoal] = []
|
||||
var _known_generator_ids: Dictionary = {}
|
||||
@@ -60,13 +57,11 @@ func _evaluate_goal(runtime_goal: GeneratorUnlockGoal) -> bool:
|
||||
return false
|
||||
if runtime_goal.goal == null:
|
||||
return false
|
||||
if runtime_goal.goal.get_script() != GOAL_DATA_SCRIPT:
|
||||
return false
|
||||
if _is_goal_completed(runtime_goal):
|
||||
return false
|
||||
if not _can_resolve_target(runtime_goal.target_generator_id):
|
||||
return false
|
||||
if not bool(runtime_goal.goal.call("is_met")):
|
||||
if not bool(runtime_goal.goal.is_met()):
|
||||
return false
|
||||
|
||||
var goal_id: StringName = _extract_goal_id(runtime_goal.goal)
|
||||
@@ -76,11 +71,11 @@ func _evaluate_goal(runtime_goal: GeneratorUnlockGoal) -> bool:
|
||||
print("[UnlockSystem] Goal completed: %s -> %s" % [String(goal_id), String(runtime_goal.target_generator_id)])
|
||||
return true
|
||||
|
||||
func _extract_goal_id(goal: Resource) -> StringName:
|
||||
func _extract_goal_id(goal: GoalData) -> StringName:
|
||||
if goal == null:
|
||||
return &""
|
||||
|
||||
var goal_id_text: String = String(goal.get("id")).strip_edges()
|
||||
var goal_id_text: String = String(goal.id).strip_edges()
|
||||
if goal_id_text.is_empty():
|
||||
return &""
|
||||
|
||||
@@ -109,27 +104,25 @@ func _load_goals() -> void:
|
||||
_runtime_goals.clear()
|
||||
|
||||
var seen_goal_ids: Dictionary = {}
|
||||
for goal_resource in goals:
|
||||
if goal_resource == null:
|
||||
for generator_unlock_goal_data in goals:
|
||||
if generator_unlock_goal_data == null:
|
||||
continue
|
||||
if goal_resource.get_script() != GENERATOR_UNLOCK_GOAL_DATA_SCRIPT:
|
||||
continue
|
||||
var goal_data: Resource = goal_resource
|
||||
var goal_data: GeneratorUnlockGoalData = generator_unlock_goal_data
|
||||
if goal_data == null:
|
||||
continue
|
||||
var target_generator_id: String = String(goal_data.get("target_generator_id")).strip_edges()
|
||||
var target_generator_id: String = String(goal_data.target_generator_id).strip_edges()
|
||||
if target_generator_id.is_empty():
|
||||
push_warning("Skipping unlock goal resource: missing target_generator_id")
|
||||
continue
|
||||
var resolved_goal: Resource = goal_data.call("get_goal_data")
|
||||
var resolved_goal: GoalData = generator_unlock_goal_data.get_goal_data()
|
||||
if resolved_goal == null:
|
||||
push_warning("Skipping unlock goal for target '%s': goal is null" % target_generator_id)
|
||||
continue
|
||||
if not bool(resolved_goal.call("is_valid")):
|
||||
if not bool(resolved_goal.is_valid()):
|
||||
push_warning("Skipping unlock goal for target '%s': goal is invalid" % target_generator_id)
|
||||
continue
|
||||
|
||||
var goal_key: String = String(resolved_goal.get("id"))
|
||||
var goal_key: String = String(resolved_goal.id)
|
||||
if seen_goal_ids.has(goal_key):
|
||||
push_warning("Duplicate unlock goal id skipped: '%s'" % goal_key)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user