Files
windroost/p2_godot_sample/scripts/vfx_view.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

141 lines
3.9 KiB
GDScript

class_name VfxView
extends Node2D
## One-shot pixel VFX player.
##
## Reads a fixed-grid sheet produced by `tools/create_pixel_vfx.py`. Frames are
## advanced by index, never interpolated, and the sprite uses nearest filtering
## so the clusters stay hard on every viewport size.
##
## Low-power mode skips intermediate frames but keeps the first, the middle and
## the last, so the state is still readable while the animation costs less.
const VFX_DIR := "res://assets/vfx/"
const MANIFEST_PATH := VFX_DIR + "vfx_manifest.json"
# The manifest is static for the whole session. Parsing it on every effect
# spawn meant a disk read + full JSON parse per hit (up to ~16/sec under held
# taps). Cache it once; a null cache means "not loaded yet", an empty dict means
# "tried and missing/invalid" so we do not retry every spawn.
static var _manifest_cache: Dictionary = {}
static var _manifest_loaded := false
var effect_id := ""
var low_power := false
var reduced_motion := false
var finished := false
var _sprite: Sprite2D
var _texture: Texture2D
var _cell := 16
var _frames := 1
var _fps := 12.0
var _anchor := Vector2(8, 15)
var _step := 1
var _frame := 0
var _elapsed := 0.0
func _ready() -> void:
_ensure_sprite()
_apply_frame()
func _ensure_sprite() -> void:
# configure() may call _ready() before add_child(); Godot then calls _ready()
# again on tree entry. Without this guard two Sprite2Ds are created per VFX,
# double-drawing every effect. Match UnitView's validity guard.
if _sprite != null and is_instance_valid(_sprite):
return
_sprite = Sprite2D.new()
_sprite.name = "Sheet"
_sprite.centered = false
_sprite.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
add_child(_sprite)
## Configure before or after entering the tree.
func configure(id: String, is_low_power: bool = false, no_motion: bool = false) -> bool:
effect_id = id
low_power = is_low_power
reduced_motion = no_motion
if not _manifest_loaded:
_manifest_loaded = true
var file := FileAccess.open(MANIFEST_PATH, FileAccess.READ)
if file == null:
push_warning("VFX manifest missing: %s" % MANIFEST_PATH)
return false
var parsed = JSON.parse_string(file.get_as_text())
file.close()
if parsed is Dictionary:
_manifest_cache = parsed
var spec: Dictionary = _manifest_cache.get("effects", {}).get(id, {})
if spec.is_empty():
return false
_texture = load(VFX_DIR + str(spec.get("file", ""))) as Texture2D
if _texture == null:
return false
var size: Array = spec.get("cell_size", [16, 16])
_cell = int(size[0])
_frames = int(spec.get("frame_count", 1))
_fps = float(spec.get("fps", 12))
var anchor: Array = spec.get("anchor", [_cell / 2, _cell - 1])
_anchor = Vector2(float(anchor[0]), float(anchor[1]))
var low_step := int(spec.get("low_power_frame_step", 1))
_step = maxi(1, low_step) if low_power else 1
finished = false
_frame = 0
_elapsed = 0.0
if reduced_motion:
# Show the final state immediately: the feedback stays readable but
# nothing animates. No flashes, no motion.
_frame = maxi(0, _frames - 1)
finished = true
if _sprite == null:
_ready()
else:
_apply_frame()
return true
## Advance with a caller-supplied delta so tests can drive it deterministically.
func step(delta: float) -> bool:
if finished:
return true
_elapsed += delta
var frame_duration := 1.0 / maxf(0.1, _fps)
while _elapsed >= frame_duration and not finished:
_elapsed -= frame_duration
_frame += _step
if _frame >= _frames - 1:
_frame = _frames - 1
finished = true
_apply_frame()
return finished
func _process(delta: float) -> void:
if step(delta):
queue_free()
func current_frame() -> int:
return _frame
func frame_count() -> int:
return _frames
func frame_step() -> int:
return _step
func _apply_frame() -> void:
if _sprite == null or _texture == null:
return
_sprite.texture = _texture
_sprite.region_enabled = true
_sprite.region_rect = Rect2(float(_frame * _cell), 0.0, float(_cell), float(_cell))
_sprite.position = -_anchor