extends Node # ModifierCatalog.gd # Holds the master list of every modifier in the game and picks random sets # for each round. Register this as an AUTOLOAD (Project > Project Settings > # Autoload) with the name "ModifierCatalog" so any script can reach it globally. # # Two ways to fill the catalog: # A) Drop .tres Modifier files into res://Modifiers/ and they auto-load (below). # B) Build them in code in _register_builtins() for quick prototyping. var modifiers: Array[Modifier] = [] var _by_id: Dictionary = {} # id -> Modifier, for fast lookup func _ready() -> void: _load_from_folder("res://Modifiers/") if modifiers.is_empty(): # Fallback so the prototype works before you author .tres files. _register_builtins() _reindex() print("ModifierCatalog loaded ", modifiers.size(), " modifiers.") # Load every .tres Modifier resource from a folder. func _load_from_folder(path: String) -> void: var dir := DirAccess.open(path) if dir == null: return dir.list_dir_begin() var fname := dir.get_next() while fname != "": if not dir.current_is_dir() and fname.ends_with(".tres"): var res := load(path + fname) if res is Modifier: modifiers.append(res) fname = dir.get_next() dir.list_dir_end() # Quick code-defined modifiers for prototyping. Replace texture paths with yours. func _register_builtins() -> void: modifiers.append(_make("stars_x5", "Stars", "Multiplies this segment's value by 5.", "res://Models/Dartboard/Dartboard-Proto_Red.png", 5.0, 0)) modifiers.append(_make("double", "Double", "Doubles this segment's value.", "res://Models/Dartboard/Dartboard-Proto_Green.png", 2.0, 0)) modifiers.append(_make("plus10", "Bonus +10", "Adds 10 points to this segment.", "res://Models/Dartboard/Dartboard-Proto_Cork.png", 1.0, 10)) func _make(id: String, title: String, desc: String, tex_path: String, mult: float, add: int) -> Modifier: var m := Modifier.new() m.id = id m.title = title m.description = desc m.texture = load(tex_path) m.score_mult = mult m.score_add = add return m func _reindex() -> void: _by_id.clear() for m in modifiers: _by_id[m.id] = m func get_by_id(id: String) -> Modifier: return _by_id.get(id) # Pick `count` distinct random modifiers, respecting weight. Returns an Array. func pick_random(count: int) -> Array[Modifier]: var pool := modifiers.duplicate() var chosen: Array[Modifier] = [] count = min(count, pool.size()) for _i in count: var total := 0.0 for m in pool: total += max(m.weight, 0.0001) var r := randf() * total var acc := 0.0 var picked_index := 0 for j in pool.size(): acc += max(pool[j].weight, 0.0001) if r <= acc: picked_index = j break chosen.append(pool[picked_index]) pool.remove_at(picked_index) # distinct: don't pick the same one twice return chosen