Files
windroost/p2_godot_sample/scripts/economy_loop_test.gd
T
wushenghua a0c590deec feat: add animated units, real touch input and the runtime economy loop
Continues the MVP work from the UI and numbers baseline.

Animation. The frame runs were already in the repository but had never been
packed for runtime use. pack_animation_strip.py now derives one shared scale
and one shared foot line from the union of every frame in a state, so the
character does not jitter between frames; H01 idle/attack, EG01 idle/hit and
B01 idle/hit are packed as transparent fixed-grid atlases. UnitView plays
them with the node origin on the foot line, loops idle, plays hit once and
falls back, and flashes on damage. Content-to-art bindings live in the
content fixture, so no character id is hardcoded in the HUD.

Touch. The previous HUD used gui_input callbacks, which is not proof of a
mobile input path. run_touch_test.sh now feeds InputEventScreenTouch through
Input.parse_input_event and checks that taps on the tap toggle, an enemy card
and the storm button produce the right events on the right tick. It also
caught a real bug: Godot emulates mouse presses from touches, so one tap was
firing both handlers; enemy taps are now debounced.

Economy. The paper model had the reward and upgrade curves but the runtime
only subtracted gold from a balance that was always zero. The scene now owns
permanent progress: victory grants the reward from the shared rules, upgrades
cost a growing amount, hero DPS rises per level, and the next battle is
created with the upgraded roster DPS. run_economy_loop_test.sh proves four
N01 clears pay for the first upgrade and that duplicate grants and losses
behave correctly. The economy pacing was left at four wins rather than
weakening it to satisfy the test.

tools/run_p2_checks.sh now runs 11 gates; all pass. PRODUCTION/38 records
P2-IMP-02/03 and P2-BAT-02 as PASS. Remaining P2 holds: profiler/visual
limit, shield-break and ultimate VFX, real-device safe area and performance.
2026-11-16 23:30:49 +08:00

117 lines
4.4 KiB
GDScript

extends SceneTree
## Runtime economy loop test.
##
## Proves that the player-facing loop works in the running game rather than
## only on paper: win -> reward -> gold -> upgrade -> higher roster DPS ->
## the next battle actually uses the upgraded DPS. Also checks duplicate
## reward suppression and that failing a battle grants no gold.
const OUTPUT_PATH := "res://artifacts/economy_loop_test.json"
var _problems: Array = []
var _steps: Array = []
func _init() -> void:
_run.call_deferred()
func _run() -> void:
var scene = load("res://scripts/battle_scene.gd").new()
get_root().add_child(scene)
var battle := scene as BattleScene
battle.set_process(false)
battle.reset_progress()
var base_dps := battle.base_roster_dps()
_check(absf(base_dps - 65.0) < 0.001, "base roster DPS should be 65, got %s" % str(base_dps))
_check(battle.gold == 0, "a fresh profile should start with 0 gold")
_check(battle.crystal == 0, "a fresh profile should start with 0 crystal")
_record("initial", {"gold": battle.gold, "dps": battle.effective_roster_dps(), "level": battle.level_of("H01")})
# N01 pays 30 gold and the first upgrade costs 120: four clears are the
# intended early pacing, so replay rather than weakening the economy.
for run_index in range(4):
battle.setup("zh_CN", "P3A-N01-AUTO", 10001)
battle.advance_ticks(200)
_check(str(battle.state.get("result", "")) == "win", "N01 auto should win on run %d" % (run_index + 1))
_check(battle.gold == 120, "four N01 wins should grant 120 gold, got %d" % battle.gold)
_check(battle.crystal == 0, "N01 grants no crystal")
_record("victory", {"gold": battle.gold, "crystal": battle.crystal, "round": battle.round_index})
# Re-rendering the settled frame must not pay the reward twice.
battle.advance_ticks(1)
_check(battle.gold == 120, "duplicate reward grant must not add gold")
_record("duplicate_reward_guard", {"gold": battle.gold})
# Upgrade the first visible hero.
var cost := battle.upgrade_cost("H01")
var dps_before := battle.effective_roster_dps()
var level_before := battle.level_of("H01")
var bought := battle.purchase_upgrade("H01")
_check(bought, "the upgrade should be affordable with 30 gold")
_check(battle.gold == 120 - cost, "the upgrade should cost %d gold" % cost)
_check(battle.level_of("H01") == level_before + 1, "the hero level should increase")
_check(
battle.effective_roster_dps() > dps_before,
"upgrading should raise roster DPS from %s" % str(dps_before)
)
_record("upgrade", {
"gold": battle.gold, "cost": cost, "level": battle.level_of("H01"),
"dps_before": dps_before, "dps_after": battle.effective_roster_dps()
})
# The next battle must actually use the upgraded DPS.
battle.setup("zh_CN", "P3A-N01-AUTO", 10001)
var sim_dps := float(battle.state.get("roster_dps", 0.0))
_check(
absf(sim_dps - battle.effective_roster_dps()) < 0.001,
"the new battle should use the upgraded roster DPS: sim=%s profile=%s"
% [str(sim_dps), str(battle.effective_roster_dps())]
)
_check(sim_dps > 65.0, "the new battle should be stronger than the base 65 DPS")
_record("next_battle", {"sim_dps": sim_dps, "gold": battle.gold})
# A loss grants nothing.
battle.setup("zh_CN", "P3A-N03-SEMI", 10001)
battle.advance_ticks(300)
var gold_before_loss := battle.gold
_check(str(battle.state.get("result", "")) == "timeout", "N03 seed 10001 should time out")
_check(battle.gold == gold_before_loss, "a loss must not grant gold")
_record("loss", {"gold": battle.gold, "result": str(battle.state.get("result", ""))})
var passed := _problems.is_empty()
var report := {
"schema": 1,
"kind": "windroost_economy_loop_test",
"overall": "PASS" if passed else "FAIL",
"problems": _problems,
"steps": _steps,
}
var artifact_dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(artifact_dir)
var file := FileAccess.open(artifact_dir.path_join("economy_loop_test.json"), FileAccess.WRITE)
if file != null:
file.store_string(JSON.stringify(report, "\t", true))
file.close()
for entry in _steps:
print("ECONOMY_STEP %s" % JSON.stringify(entry))
if passed:
print("ECONOMY_LOOP_PASS %d stages verified" % _steps.size())
else:
print("ECONOMY_LOOP_FAIL %s" % "; ".join(_problems))
quit(0 if passed else 1)
func _check(condition: bool, message: String) -> void:
if not condition:
_problems.append(message)
func _record(name: String, data: Dictionary) -> void:
var entry := data.duplicate()
entry["step"] = name
_steps.append(entry)