spaceship/tests/test_fleet_formation.gd

77 lines
2.4 KiB
GDScript3
Raw Normal View History

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_fleet_formation.gd
# Verifies that issuing a fleet move order makes every ship converge on its
# formation slot (offset preserved) around the ordered point AND actually
# stops there - a regression guard for the arrival-overshoot bug where ships
# oscillated back and forth around the target forever instead of settling.
const CHECKPOINT_TIME: float = 4.0
const TEST_DURATION: float = 7.0
const ARRIVE_TOLERANCE: float = 2.0
const STABLE_TOLERANCE: float = 1.0
const VELOCITY_TOLERANCE: float = 1.0
var main_scene: Node
var fleet: Node
var target: Vector2 = Vector2(600.0, 300.0)
var elapsed: float = 0.0
var started: bool = false
var checkpoint_taken: bool = false
var checkpoint_positions: Array = []
func _initialize() -> void:
var packed: PackedScene = load("res://Main.tscn")
main_scene = packed.instantiate()
root.add_child(main_scene)
fleet = main_scene.get_node("Fleet")
func _physics_process(delta: float) -> bool:
if not started:
# Wait one physics tick so Fleet/Ship _ready() has run and
# formation_offsets are computed before we issue the order.
fleet.move_to(target)
started = true
return false
elapsed += delta
if not checkpoint_taken and elapsed >= CHECKPOINT_TIME:
checkpoint_taken = true
for ship in fleet.ships:
checkpoint_positions.append(ship.position)
if elapsed >= TEST_DURATION:
_finish()
return true
return false
func _finish() -> void:
var all_ok: bool = true
for i in range(fleet.ships.size()):
var ship: Node = fleet.ships[i]
var expected: Vector2 = target + fleet.formation_offsets[i]
var dist: float = ship.position.distance_to(expected)
var drift_since_checkpoint: float = ship.position.distance_to(checkpoint_positions[i])
var speed: float = ship.velocity.length()
var arrived: bool = dist <= ARRIVE_TOLERANCE
var stable: bool = drift_since_checkpoint <= STABLE_TOLERANCE
var stopped: bool = speed <= VELOCITY_TOLERANCE
var ok: bool = arrived and stable and stopped
all_ok = all_ok and ok
print("Ship %d expected=%s actual=%s dist=%.3f drift_since_t%.0fs=%.3f speed=%.3f %s" % [
i, expected, ship.position, dist, CHECKPOINT_TIME, drift_since_checkpoint, speed, "OK" if ok else "FAIL"
])
if all_ok:
print("TEST_FLEET_FORMATION: PASS (ships arrived and settled, no oscillation)")
quit(0)
else:
print("TEST_FLEET_FORMATION: FAIL")
quit(1)