spaceship/tests/test_ship_facing.gd

104 lines
3.8 KiB
GDScript3
Raw Normal View History

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_ship_facing.gd
# Regression test for ships freezing at a fixed heading.
#
# Ships holding formation sit INSIDE arrive_radius of their slot (the fleet
# anchor advances less than that per frame at cruise speed), so they snap
# to it with zero velocity. Facing used to be derived purely from a ship's
# own velocity behind a "> 8.0" gate, which meant a station-keeping ship
# had nothing to face along and stayed pointing whichever way it last
# happened to travel - it would not turn when the fleet changed direction.
var scene: Node
var fleet: Node
var stage: int = 0
var elapsed: float = 0.0
var heading_east: float = 0.0
func _initialize() -> void:
root.get_node("GameState").reset_formation()
#84: move 215 root files into a folder hierarchy Everything lived in one flat directory, so a diff gave no clue what area it touched and finding the file you wanted meant knowing its exact name. Twelve folders now, grouped by what a thing IS rather than what type it is - a vessel's definition, its component scenes and its upgrade resources sit together, because that is the set you read when you change one of them. buildings core controllers enemies logistics planets resources shaders surface ui vessels vfx world 332 files moved and every res:// reference rewritten - 306 files touched, including the autoload and main-scene paths in project.godot. Nothing renamed: this is purely where files sit, so the diff is a pile of pure moves and no behaviour is buried in it. ui_theme.tres, icon.svg and web_shell.html stay at the root, where Godot and the export preset expect them. Verified three ways, because a suite alone would not have caught all of it: the full 117 pass, the web export builds clean, and the game renders. The export is why that matters. The FIRST export after the move failed with "Attempt to open script 'res://GameState.gd'" - the old path - while project.godot on disk plainly said res://core/GameState.gd. That is this project's documented Android FUSE mtime trap: Godot served a cached copy of a file whose content had changed. A `touch` fixed it. Nothing in the test suite would have found that, because the suite runs from a copy on a native filesystem - the one place the bug cannot happen. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 19:44:14 +02:00
scene = load("res://world/Main.tscn").instantiate()
root.add_child(scene)
fleet = scene.get_node("Fleet")
func _angles_match(a: float, b: float, tolerance: float) -> bool:
return absf(angle_difference(a, b)) <= tolerance
func _process(delta: float) -> bool:
elapsed += delta
match stage:
0:
scene.get_node("AsteroidField").set_process(false)
scene.get_node("EnemySpawner").set_process(false)
fleet.move_in_direction(Vector2.RIGHT)
elapsed = 0.0
stage = 1
return false
1:
if elapsed < 1.5:
return false
# Structural check, because the behavioural ones below cannot
# reproduce the actual failure: on a real device at 60fps the
# anchor advances ~4.3 units per frame, LESS than arrive_radius,
# so a station-keeping ship snaps to its slot with zero velocity
# and has no heading of its own to face along. Headless runs
# _process uncapped against 60Hz physics, so ships here always
# pick up real velocity and look fine either way. Asserting the
# imposed heading is wired is what actually guards the fix.
var expected_fleet_velocity: Vector2 = Vector2.RIGHT * fleet.cruise_speed
for ship in fleet.ships:
if not ship.fleet_velocity.is_equal_approx(expected_fleet_velocity):
print("fleet_velocity=%s, expected %s" % [ship.fleet_velocity, expected_fleet_velocity])
print("TEST_SHIP_FACING: FAIL (fleet motion not supplied to ships)")
quit(1)
return true
# Flying east: every ship should point east (sprites are drawn
# nose-up, hence the +PI/2 convention in Ship._physics_process).
heading_east = Vector2.RIGHT.angle() + PI / 2.0
for ship in fleet.ships:
if not _angles_match(ship.rotation, heading_east, 0.25):
print("ship facing %.3f, expected ~%.3f while flying east" % [
ship.rotation, heading_east
])
print("TEST_SHIP_FACING: FAIL (not facing travel direction)")
quit(1)
return true
print("flying east: all ships facing %.3f" % fleet.ships[0].rotation)
# Turn by a SMALL angle. This is the discriminating case: a big
# turn shoves each ship sideways hard enough that its own
# velocity happens to point roughly the new way, so facing looks
# right even when broken. A gentle turn moves each ship only a
# few units, its velocity never clears the "> 8.0" gate, and a
# ship given no fleet motion simply stays pointing the old way.
fleet.move_in_direction(Vector2.RIGHT.rotated(-0.6))
elapsed = 0.0
stage = 2
return false
2:
if elapsed < 1.5:
return false
var heading_north: float = Vector2.RIGHT.rotated(-0.6).angle() + PI / 2.0
for ship in fleet.ships:
if not _angles_match(ship.rotation, heading_north, 0.15):
print("ship stuck at %.3f, expected ~%.3f after the gentle turn" % [
ship.rotation, heading_north
])
print("TEST_SHIP_FACING: FAIL (did not turn with the fleet)")
quit(1)
return true
print("gentle turn: all ships facing %.3f (was %.3f)" % [
fleet.ships[0].rotation, heading_east
])
print("TEST_SHIP_FACING: PASS")
quit(0)
return true
return false