Files
windroost/p2_godot_sample/scripts/touch_input_test.gd
T
wushenghua 2408bda84d fix: audit-driven battle, perf and save correctness batch
Three parallel read-only reviews (save/boundary, perf/lifecycle, battle/economy)
produced verified findings; each was confirmed with a probe before fixing.

Battle logic:
- storm_cooldown was set to 10s on use but never decremented, disabling the
  button for the rest of every battle. Now ticks with speed-scaled delta.
  touch_input_test verifies 9.8s -> 0 after 12s.
- set_p3a_scenario ignored the per-wave seed passed to setup(), so every wave
  of a multi-wave node reused seed 10001. Now accepts an explicit seed from
  GameRoot.

Performance:
- set_roster rebuilt all hero cards every frame (~35 nodes + styleboxes/frame,
  ~2100 nodes/sec at 60fps). Now diffs by hero id and updates labels/energy
  in place; visible_node_test asserts card instance ids are unchanged across
  an identical set_roster call.
- VfxView.configure() called _ready() before add_child() triggered Godot's
  _ready() again, creating two Sprite2Ds per effect (double overdraw). Added
  an _ensure_sprite validity guard like UnitView.
- VFX manifest was reopened + JSON-parsed per effect spawn (~16/sec under held
  taps); now cached in a static.
- SfxPlayer._played (test-only telemetry) grew unbounded; capped at 64.

Save/profile:
- muted and low_power were written by settings but absent from DEFAULT_PROFILE,
  so _normalize() dropped them on every save -> settings never persisted.
  Added to defaults + migration; save_corruption_test verifies.
- An unclaimed offline report was overwritten by the next launch after any
  intervening save, silently losing earned gold. build_offline_report now
  preserves a pending report; p3b test models claim/clear between scenarios.
- hero_levels values are clamped to 1..10000 (negative/huge values gave free
  or overflowing upgrade costs) and unlocked_heroes is deduped (repeat ids
  triple-counted roster DPS).

Robustness:
- hit-stop restore coroutine now uses a generation token so a stale coroutine
  cannot end a newer hit-stop after clear_all.

Full gate P2_CHECKS_PASS. All logs still 0 script/parse errors and 0 lambda-
capture errors. prototype/ untracked.
2026-11-18 04:48:58 +08:00

145 lines
5.3 KiB
GDScript

extends SceneTree
## Real touch-input evidence for the P2/P3a HUD.
##
## This test does NOT call button callbacks or `gui_input` directly. It feeds
## `InputEventScreenTouch` through `Input.parse_input_event`, so the event goes
## through the engine's normal viewport/Control routing and the emulated mouse
## path that real mobile touches use.
##
## It verifies that a touch lands on the correct tick and produces the right
## simulation event, and writes a machine-readable log.
const OUTPUT_PATH := "res://artifacts/touch_input_test.json"
const SCENARIO := "P3A-N03-SEMI"
const SEED := 10001
var _log: Array = []
var _problems: Array = []
func _init() -> void:
_run.call_deferred()
func _run() -> void:
var root_window := get_root()
root_window.content_scale_size = Vector2i(390, 844)
root_window.size = Vector2i(390, 844)
await process_frame
await process_frame
var scene = load("res://scripts/battle_scene.gd").new()
root_window.add_child(scene)
var battle := scene as BattleScene
battle.set_process(false)
battle.setup("zh_CN", SCENARIO, SEED)
battle.advance_by_delta(2.0)
await process_frame
await process_frame
var hit_center := battle.hud.enemy_hit_center("E-R1-F3-V1#1")
var tap_center := battle.hud.named_button_center("TapButton")
var storm_center := battle.hud.named_button_center("StormButton")
_log.append({"step": "targets", "enemy": str(hit_center), "tap": str(tap_center), "storm": str(storm_center)})
if hit_center == Vector2.ZERO or tap_center == Vector2.ZERO or storm_center == Vector2.ZERO:
_problems.append("a required hit target was not found")
# 1. Start continuous tapping with a real touch.
await _tap(tap_center)
var tap_active := bool(battle.tap_active)
_log.append({"step": "tap_button", "tick": battle.state_fingerprint()["ticks"], "tap_active": tap_active})
if not tap_active:
_problems.append("ScreenTouch on TapButton did not start tapping")
# 2. Select an enemy with a real touch, then let a few ticks land.
var before := (battle.state.get("event_log", []) as Array).size()
await _tap(hit_center)
battle.advance_by_delta(0.4)
var after_log: Array = battle.state.get("event_log", [])
var click_events: Array = []
for event in after_log.slice(before):
if str(event.get("type", "")) == "click_enemy":
click_events.append(event)
_log.append({"step": "enemy_touch", "tick": battle.state_fingerprint()["ticks"], "click_events": click_events})
if click_events.is_empty():
_problems.append("ScreenTouch on the enemy did not insert a click_enemy event")
# One ScreenTouch must not be routed twice (ScreenTouch plus emulated mouse).
for event in click_events:
var same_time := 0
for other in click_events:
if other.get("time") == event.get("time"):
same_time += 1
if same_time > 2:
_problems.append(
"duplicate click_enemy events at time %s (%d); mouse emulation from touch must stay off"
% [str(event.get("time")), same_time]
)
break
# 3. Use the storm button with a real touch.
var before_storm := (battle.state.get("event_log", []) as Array).size()
await _tap(storm_center)
battle.advance_by_delta(0.2)
var storm_events: Array = []
for event in (battle.state.get("event_log", []) as Array).slice(before_storm):
if str(event.get("type", "")) == "offboard_burst":
storm_events.append(event)
_log.append({"step": "storm_touch", "tick": battle.state_fingerprint()["ticks"], "storm_events": storm_events})
if storm_events.is_empty():
_problems.append("ScreenTouch on StormButton did not insert an offboard_burst event")
# Storm cooldown must actually expire. Regression: it was set to 10s on use
# but never decremented, disabling the button for the rest of the battle.
if not battle.storm_cooldown > 0.0:
_problems.append("storm should be on cooldown right after use")
var cd_start := battle.storm_cooldown
for i in range(60):
battle.advance_by_delta(0.2) # 12 s of sim time
if not is_equal_approx(battle.storm_cooldown, 0.0):
_problems.append("storm cooldown should reach 0 after 10 s (start %.1f, now %.1f)" % [cd_start, battle.storm_cooldown])
_log.append({"step": "storm_cooldown", "start": cd_start, "end": battle.storm_cooldown})
var passed := _problems.is_empty()
var report := {
"schema": 1,
"kind": "windroost_touch_input_test",
"input_method": "Input.parse_input_event(InputEventScreenTouch)",
"scenario": SCENARIO,
"seed": SEED,
"overall": "PASS" if passed else "FAIL",
"problems": _problems,
"steps": _log,
"final_fingerprint": battle.state_fingerprint(),
}
var artifact_dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(artifact_dir)
var file := FileAccess.open(artifact_dir.path_join("touch_input_test.json"), FileAccess.WRITE)
if file != null:
file.store_string(JSON.stringify(report, "\t", true))
file.close()
for entry in _log:
print("TOUCH_STEP %s" % JSON.stringify(entry))
if passed:
print("TOUCH_INPUT_PASS %d real ScreenTouch events verified" % 3)
else:
print("TOUCH_INPUT_FAIL %s" % "; ".join(_problems))
quit(0 if passed else 1)
func _tap(position: Vector2) -> void:
var down := InputEventScreenTouch.new()
down.index = 1
down.position = position
down.pressed = true
Input.parse_input_event(down)
await process_frame
await process_frame
var up := InputEventScreenTouch.new()
up.index = 1
up.position = position
up.pressed = false
Input.parse_input_event(up)
await process_frame