spaceship/tests/test_shot_reach.gd

137 lines
4.8 KiB
GDScript3
Raw Normal View History

Implement #85, #86, #87 and #88 #85 - on a computer the sticks and the pivot pair are dead weight: WASD steers, the mouse aims, Q/E pivot. All four go together the first time a keyboard or a mouse is used, and only where there is no touchscreen. A touch device with a keyboard attached keeps everything, because a finger is still a way to play it. #86 - the default gun shoots faster (500 -> 850) and further (320 -> 620). The interesting half is that a bolt now STOPS where the gun's range ends. Projectile lifetime is derived from fire_range at launch rather than set independently, so the distance a shot covers and the distance the aim guide draws are one number. They were two, and they disagreed: the guide promised 320 while a bolt actually flew speed * lifetime = 750, so shots landed on things well past the line saying where they would stop. #87 - the ship that fires throws sparks, so a shot is visibly attached to the vessel it came from; with a whole fleet shooting there was nothing tying the two together, which started mattering once the player had to aim. Enemy deaths gained a shockwave ring and roughly double the debris - a kill used to read as a small red puff that was easy to miss in a fight. #88 - everything moves 1.5x faster: fleet cruise, every vessel's max_speed and acceleration, and every enemy's. Scaling the enemies too keeps it a change of TEMPO rather than a difficulty cut, and scaling acceleration alongside speed keeps the handling from #77's tuning intact. Three tests carried assertions that were really about one balance pass: test_enemy_mothership pinned a literal 35.0 and now compares against the chaser, which is what "never reaches ramming speed" actually means; test_planet_atmosphere_transition flew far enough at the new speed to reach the landed garrison, and now clears it, since that test is about the handoff and not about a fight it never meant to pick. Also repaired real data loss, caught only because test_gas_burner failed: Godot had silently re-serialised GasBurnerBuildingDefinition.tres and dropped collected_outputs, capabilities, map_radius and map_phase, and had recreated a stale copy of it at the pre-#84 root path. Without collected_outputs no transporter ever collects a burner's energy. Both files restored from git. It had also resurrected pre-#84 SNAPSHOTS of Main.tscn, Planet.tscn and lunar_surface.gdshader at their old root paths - the root Main.tscn still carried 27 references to the flat layout. They are dead weight the engine never loads, since project.godot and every preload point into the folders, but they are exactly the kind of thing that wastes an hour later when someone edits the wrong copy. Deleted, and the root is now down to ui_theme.tres, icon.svg, web_shell.html and the two config files. Suite 119/119. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-22 02:19:14 +02:00
extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_shot_reach.gd
# Forgejo #86: the default gun shoots faster and further. #87: the ship that
# fires throws sparks, so a shot is visibly attached to the vessel it came
# from.
#
# The interesting part is not the numbers, it is that a bolt now stops where
# the gun's range ends. It used to fly speed * lifetime - 750 units - while
# the aim guide drew fire_range, 320. The guide was a promise the shot did not
# keep, in the direction that matters: shots landed on things well past the
# line saying where they would stop.
#
# Covers:
# A. a bolt's travel distance IS the gun's range, whatever either is set to
# B. the aim guide draws that same range, so the line and the shot agree
# C. firing throws sparks from the firing ship, and they clear themselves up
const PICKUP_TOLERANCE: float = 60.0
var game_state: Node
var main: Node
var fleet: Node
var controller: Node
var weapon: Node
var shooter: Node2D
var stage: int = 0
var elapsed: float = 0.0
var bolt: Node2D
var launch_point: Vector2 = Vector2.ZERO
var furthest: float = 0.0
func _initialize() -> void:
game_state = root.get_node("GameState")
game_state.buildings.clear()
game_state.extractors.clear()
game_state.reset_progression()
main = load("res://world/Main.tscn").instantiate()
root.add_child(main)
current_scene = main
fleet = main.get_node("Fleet")
controller = main.get_node("FireStickController")
func _sparks() -> Array:
var found: Array = []
for node in root.get_children():
if node.get_script() != null and String(node.get_script().resource_path).ends_with("MuzzleSparkVFX.gd"):
found.append(node)
return found
func _process(delta: float) -> bool:
elapsed += delta
match stage:
0:
if elapsed < 0.3:
return false
main.get_node("EnemySpawner").set_process(false)
main.get_node("AsteroidField").set_process(false)
for hazard in get_nodes_in_group("asteroids") + get_nodes_in_group("enemies") \
+ get_nodes_in_group("player_projectiles"):
if is_instance_valid(hazard):
hazard.free()
fleet.spawn_ships(1, Vector2.ZERO, PackedStringArray([GameState.VESSEL_INTERCEPTOR]))
shooter = fleet.ships[0]
weapon = shooter.get_component("weapon")
if weapon == null:
return _fail("the default vessel has no weapon component")
# --- B. the guide draws the gun's own range --------------------
controller.aim(Vector2(1.0, 0.0))
if not is_equal_approx(controller.guide_range(), weapon.fire_range):
return _fail("the aim guide reaches %.0f but the gun's range is %.0f" \
% [controller.guide_range(), weapon.fire_range])
print("aim guide and gun range agree at %.0f" % weapon.fire_range)
launch_point = shooter.global_position
weapon.fire_timer = 0.0
weapon.tick(0.0)
var bolts: Array = get_nodes_in_group("player_projectiles")
if bolts.is_empty():
return _fail("the gun fired nothing")
bolt = bolts[0]
# --- C. sparks at the muzzle -----------------------------------
var sparks: Array = _sparks()
if sparks.is_empty():
return _fail("firing threw no sparks")
if sparks[0].global_position.distance_to(shooter.global_position) > PICKUP_TOLERANCE:
return _fail("the sparks appeared %.0f from the ship that fired" \
% sparks[0].global_position.distance_to(shooter.global_position))
print("firing threw sparks at the firing ship")
stage = 1
elapsed = 0.0
return false
1:
# --- A. the bolt stops where the range ends --------------------
if is_instance_valid(bolt) and not bolt.is_queued_for_deletion():
furthest = maxf(furthest, launch_point.distance_to(bolt.global_position))
return false
var reach: float = weapon.fire_range
# One physics step of slack either way: the bolt is freed on the
# tick its age passes the limit, so it lands just short of or just
# past the exact figure.
var slack: float = bolt_step()
if absf(furthest - reach) > slack:
return _fail("the bolt travelled %.0f but the gun's range is %.0f (slack %.0f)" \
% [furthest, reach, slack])
print("bolt stopped at %.0f against a range of %.0f" % [furthest, reach])
if not _sparks().is_empty():
return _fail("%d spark burst(s) still alive long after firing" % _sparks().size())
print("sparks cleared themselves up")
print("TEST_SHOT_REACH: PASS")
quit(0)
return true
return false
func bolt_step() -> float:
# How far a bolt moves in a single physics tick, which is the granularity
# its lifetime can be enforced at.
var speed: float = 850.0
var probe: Node2D = load("res://vessels/Projectile.tscn").instantiate()
speed = float(probe.speed)
probe.free()
return speed / float(Engine.physics_ticks_per_second) * 1.5
func _fail(message: String) -> bool:
print("TEST_SHOT_REACH: FAIL (%s)" % message)
quit(1)
return true