Files
windroost/p2_godot_sample/scripts/ui_text.gd
T
wushenghua e7b55e1c17 feat: align runtime with paper numbers and build the battle HUD carrier
Numbers. The paper runner (tools/mvp_validation_sim.py) and the Godot
runtime adapter were two independent rewrites of the same rules and had
drifted: seed derivation, damage order, the weakness rule and the shield
rebuild semantics all differed, so the runtime never actually reproduced
the paper numbers and P2-BAT-01 could never pass.

- add data/mvp_battle_rules.json as the single source of balance data and
  of the per-run seed derivation, read by both engines
- derive the efficiency factor from SHA-256 (48-bit prefix) so Python and
  GDScript land on the same float64
- port the paper semantics into battle_sim.gd and expose the simulation as
  a stepwise state machine so a live scene can drive it at 1x/2x/3x
- add tools/mvp_parity_gate.py: diffs 300 results field by field, event
  log by event log, and exits non-zero on any difference
- freeze data/mvp_validation_seed_reference.json so unapproved numeric
  drift fails the gate
- note that GDScript's roundf() is half-away-from-zero while Python's
  round() is banker's rounding; the runtime uses String.num instead

UI. The sample had no real UI carrier, no localization and no touch path,
so the NinePatch, dynamic text, safe area and input cases had nothing to
be proven against.

- add the battle HUD, laid out from the contract table rather than container
  stretch ratios, reproducing the 390x844 rectangles exactly and adapting
  360x800 / 430x932 with the stage kept as priority space
- add runtime text tables for zh_CN and en_US and a key validator that
  proves both locales carry the same keys and every content id has a string
- add NinePatch frames with a 6px border band and 6px cut corners
- add the live battle scene, the capture runner and the 1x/2x/3x
  equivalence test

tools/run_p2_checks.sh runs the nine gates in one command; all pass.
2026-11-16 22:22:55 +08:00

98 lines
2.9 KiB
GDScript

class_name UiText
extends RefCounted
## Runtime string table for the Windroost UI.
##
## Every visible string comes from a key in `data/ui_text.<locale>.json`.
## Seven-locale schema is fixed on day one (ADR-009); only zh_CN and en_US ship
## in the vertical slice (ADR-016). A missing key in the active locale falls
## back to zh_CN and then to the key itself, so a missing translation degrades
## to a visible marker instead of a crash.
const DATA_DIR := "res://../data/"
const FALLBACK_LOCALE := "zh_CN"
const SUPPORTED_LOCALES := ["zh_CN", "en_US", "es", "pt-BR", "ja", "fr", "de"]
static var _tables: Dictionary = {}
static var _missing: Dictionary = {}
static func available_locales() -> Array[String]:
var locales: Array[String] = []
for path in _locale_paths():
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
continue
var parsed = JSON.parse_string(file.get_as_text())
file.close()
if parsed is Dictionary:
locales.append(str(parsed.get("locale", "")))
return locales
static func _locale_paths() -> Array[String]:
var paths: Array[String] = []
for locale in SUPPORTED_LOCALES:
paths.append(DATA_DIR + "ui_text.%s.json" % locale)
return paths
static func _load_table(locale: String) -> Dictionary:
if _tables.has(locale):
return _tables[locale]
var path := DATA_DIR + "ui_text.%s.json" % locale
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
push_warning("UI text table missing: %s" % path)
_tables[locale] = {}
return {}
var parsed = JSON.parse_string(file.get_as_text())
file.close()
var table: Dictionary = {}
if parsed is Dictionary:
table = parsed.get("strings", {})
_tables[locale] = table
return table
static func t(key: String, locale: String = FALLBACK_LOCALE) -> String:
var table := _load_table(locale)
if table.has(key):
return str(table[key])
if locale != FALLBACK_LOCALE:
var fallback := _load_table(FALLBACK_LOCALE)
if fallback.has(key):
return str(fallback[key])
_missing[key] = true
return "[%s]" % key
static func tf(key: String, args: Dictionary, locale: String = FALLBACK_LOCALE) -> String:
var template := t(key, locale)
var result := template
for arg_key in args.keys():
result = result.replace("{%s}" % str(arg_key), str(args[arg_key]))
return result
## Return the first key that exists. Used for optional variants such as a
## short display name falling back to the full name.
static func t_any(keys: Array, locale: String = FALLBACK_LOCALE) -> String:
for key in keys:
var candidate := str(key)
if has_key(candidate, locale):
return t(candidate, locale)
return t(str(keys[0]) if not keys.is_empty() else "", locale)
static func missing_keys() -> Array[String]:
var keys: Array[String] = []
for key in _missing.keys():
keys.append(str(key))
keys.sort()
return keys
static func has_key(key: String, locale: String = FALLBACK_LOCALE) -> bool:
return _load_table(locale).has(key)