RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
unlock_manager.gd
Go to the documentation of this file.
1extends RefCounted
2
3
7
8const ConditionEvaluator = preload("res://scripts/systems/conditionEvaluator/condition_evaluator.gd")
9const DataCacheManager = preload("res://scripts/systems/dataCacheManager/data_cache_manager.gd")
10
11var rules := []
12var unlocked := {}
13var condition_evaluator = ConditionEvaluator.new()
14var data_cache_manager = DataCacheManager.new()
15
16
17
18func load_rules(file_path:String) -> void:
19 rules.clear()
20 var data = data_cache_manager.load_json(file_path)
21 if data is Dictionary:
22 configure(data.get("unlocks", []))
23
24
25
26func get_cache_stats() -> Dictionary:
27 return data_cache_manager.get_cache_stats()
28
29
30
31func configure(next_rules:Array, state:Dictionary = {}) -> void:
32 rules.clear()
33 unlocked = state.get("unlocked", {}).duplicate(true) if state.get("unlocked", {}) is Dictionary else unlocked
34 for rule in next_rules:
35 if not rule is Dictionary:
36 continue
37 var entry = rule.duplicate(true)
38 entry["id"] = str(entry.get("id", ""))
39 entry["target"] = str(entry.get("target", entry.get("id", "")))
40 entry["category"] = str(entry.get("category", "general"))
41 entry["conditions"] = entry.get("conditions", {}) if entry.get("conditions", {}) is Dictionary else {}
42 if str(entry.get("id", "")) != "" and str(entry.get("target", "")) != "":
43 rules.append(entry)
44
45
46
47func evaluate(context:Dictionary = {}) -> Array:
48 var newly_unlocked := []
49 for rule in rules:
50 var target = str(rule.get("target", ""))
51 if target == "" or is_unlocked(target):
52 continue
53 if condition_evaluator.matches(rule.get("conditions", {}), context):
54 unlocked[target] = true
55 newly_unlocked.append(rule.duplicate(true))
56 return newly_unlocked
57
58
59
60func is_unlocked(target_id:String) -> bool:
61 return bool(unlocked.get(target_id, false))
62
63
64
65func set_unlocked(target_id:String, value:bool = true) -> void:
66 unlocked[target_id] = value
67
68
69
70func get_unlocks(filters:Dictionary = {}) -> Array:
71 var rows := []
72 for rule in rules:
73 var row = rule.duplicate(true)
74 var target = str(row.get("target", ""))
75 row["unlocked"] = is_unlocked(target)
76 if filters.has("category") and str(filters.get("category", "")) != str(row.get("category", "")):
77 continue
78 if filters.has("unlocked") and bool(filters.get("unlocked", false)) != bool(row.get("unlocked", false)):
79 continue
80 rows.append(row)
81 return rows
82
83
84
85func get_state() -> Dictionary:
86 return {"unlocked": unlocked.duplicate(true)}
87
88
89
90func apply_state(state:Dictionary) -> void:
91 unlocked = state.get("unlocked", {}).duplicate(true) if state.get("unlocked", {}) is Dictionary else {}
bool
Return whether a target is unlocked.