Files
code_of_conquest_dnd/client/scripts/ui/considering_phrases.gd

42 lines
1.3 KiB
GDScript

class_name ConsideringPhrases
extends RefCounted
## Authored "the GM is thinking" lines (charter §13 — even loading is content).
## Pure: cycles a phrase pool in order on a running index that is instance state
## (spec decision 4 — the indicator reuses one instance so the first phrase varies
## turn to turn without randomness). A missing/malformed file degrades to one
## hardcoded constant, so the indicator itself cannot fail.
const PATH := "res://content/considering.json"
const LAST_RESORT := "The Master considers your words."
var _pool: Array = []
var _index: int = 0
func _init(path := PATH) -> void:
_pool = _load(path)
func next() -> String:
if _pool.is_empty():
return LAST_RESORT
var phrase := str(_pool[_index % _pool.size()])
_index += 1
return phrase
static func _load(path: String) -> Array:
if not FileAccess.file_exists(path):
return []
var text := FileAccess.get_file_as_string(path)
# Instance parse() (not JSON.parse_string) so malformed input returns an
# Error code instead of the engine error the test config promotes to a fail.
var json := JSON.new()
if json.parse(text) != OK:
return []
var data = json.get_data()
if typeof(data) != TYPE_DICTIONARY:
return []
var lines = data.get("considering", [])
return lines if typeof(lines) == TYPE_ARRAY else []