spaceship/tests/test_laser_ammo.gd
Loïc Wisniewski facbd83dd2 Laser shooters consume energy from the fleet hold when firing (#61)
Mirrors #60's gas bomber ammunition exactly: ENERGY_PER_SHOT (8) drawn
from GameState.fleet_inventory before consuming the firing ship's
cooldown, the button's enabled rule requires affordability too, and an
unaffordable-but-otherwise-ready press still explains itself through a
new LaserStatus label rather than silently doing nothing.

Added two small generic GameState helpers this needed:
deposit_fleet_resource()/deposit_bank_resource(), counterparts to
#60's withdraw_fleet_resource() for resources (energy) with no
dedicated fleet_cargo/fleet_gas_cargo-style property. Along the way,
confirmed deposit_resource_to_building() on a "bank" building deposits
into that building's own per-record inventory, NOT bank_inventory
itself - only building_resource()/building_inventory() special-case
the "bank" capability to redirect there.

test_laser_vessel.gd (#58) updated to stock fleet energy first, since
shots are no longer free - same fix #60 required for the gas bomber's
own pre-existing tests. Also fixed test_laser_vessel.gd's own instance
of the bare-ClassName.CONST script-corruption trap (documented in
#60's commit): var skill: LaserController and a bare
LaserController.BEAM_HALF_WIDTH reference, mirrored into a local const
instead.
2026-08-16 19:11:31 +02:00

141 lines
5.5 KiB
GDScript

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_laser_ammo.gd
# Forgejo #61: laser shots draw ammunition from the FLEET HOLD
# (GameState.fleet_inventory), the same way gas bomb shots do (#60) - the
# fleet has to be carrying its own energy.
#
# Covers:
# A. banked energy does not enable firing - only the fleet hold counts
# B. with insufficient fleet energy the button is disabled, pressing it
# explains why, and no beam is fired / no cooldown consumed
# C. firing drains exactly ENERGY_PER_SHOT from the fleet hold and
# nothing from the bank
var game_state: Node
var main: Node
var fleet: Node
var skill: Node
var laser_component: Node
var stage: int = 0
var elapsed: float = 0.0
# Mirrors LaserController.ENERGY_PER_SHOT's value rather than referencing
# the class constant directly - a bare ClassName.CONST reference elsewhere
# in a test script has been observed to corrupt method resolution for the
# WHOLE script (an unrelated skill._process() call earlier in the same
# file then fails with "Nonexistent function '_process' in base 'Nil'"),
# root cause unconfirmed. See tests/test_gas_bomber_cooldowns.gd's
# identical fix (#60).
const ENERGY_PER_SHOT: int = 8
func _initialize() -> void:
game_state = root.get_node("GameState")
game_state.buildings.clear()
game_state.extractors.clear()
game_state.transporters.clear()
game_state.stored_vessels.clear()
game_state.reset_progression()
game_state.vessel_levels["unlock_laser"] = 1
game_state.rocks = 0
game_state.gas = 0
game_state.fleet_gas_cargo = 0
main = load("res://Main.tscn").instantiate()
root.add_child(main)
fleet = main.get_node("Fleet")
skill = main.get_node("LaserController")
func _process(delta: float) -> bool:
elapsed += delta
match stage:
0:
if elapsed < 0.2:
return false
main.get_node("EnemySpawner").set_process(false)
main.get_node("AsteroidField").set_process(false)
# An interceptor rides along purely for its "cargo" capability
# (#56): a fleet made only of a laser ship has zero cargo
# capacity, which would silently clamp every fleet energy
# deposit below to 0.
fleet.spawn_ships(2, Vector2.ZERO, PackedStringArray([
GameState.VESSEL_INTERCEPTOR, GameState.VESSEL_LASER]))
laser_component = fleet.ships[1].get_component("laser")
skill._process(0.0)
# --- A. bank energy does not count ----------------------------
game_state.deposit_bank_resource(GameState.RESOURCE_ENERGY, 500)
skill._process(0.0)
if skill.button.enabled:
return _fail_now("500 banked energy (0 in the fleet hold) left the control enabled")
print("500 banked energy, 0 fleet energy: control correctly disabled")
# --- B. insufficient fleet energy -------------------------------
game_state.deposit_fleet_resource(GameState.RESOURCE_ENERGY, ENERGY_PER_SHOT - 1)
skill._process(0.0)
if skill.button.enabled:
return _fail_now("fleet energy one short of ENERGY_PER_SHOT left the control enabled")
var press := InputEventScreenTouch.new()
press.index = 3
press.position = skill.button.global_position + skill.button.size * 0.5
press.pressed = true
main._handle_touch(press)
if skill.aiming:
return _fail_now("aiming began despite insufficient fleet energy")
if skill.status.text == "":
return _fail_now("an unaffordable press did not explain itself")
print("insufficient fleet energy: disabled, press explains itself, no aiming began")
if not laser_component.is_ready():
return _fail_now("a refused press consumed the laser ship's cooldown")
if game_state.fleet_resource(GameState.RESOURCE_ENERGY) != ENERGY_PER_SHOT - 1:
return _fail_now("a refused press spent fleet energy anyway")
print("no cooldown consumed, no energy spent on a refused press")
# --- C. exact ammo cost on a real shot -----------------------------
game_state.deposit_fleet_resource(GameState.RESOURCE_ENERGY, 8)
skill._process(0.0)
if not skill.button.enabled:
return _fail_now("enough fleet energy still left the control disabled")
var fleet_energy_before: int = game_state.fleet_resource(GameState.RESOURCE_ENERGY)
var press2 := InputEventScreenTouch.new()
press2.index = 4
press2.position = skill.button.global_position + skill.button.size * 0.5
press2.pressed = true
main._handle_touch(press2)
if not skill.aiming:
return _fail_now("an affordable press did not begin aiming")
var drag := InputEventScreenDrag.new()
drag.index = 4
drag.position = press2.position + Vector2(-300.0, 0.0)
main._handle_drag(drag)
var release := InputEventScreenTouch.new()
release.index = 4
release.position = drag.position
release.pressed = false
main._handle_touch(release)
if laser_component.is_ready():
return _fail_now("firing did not consume the laser ship's cooldown")
var fleet_energy_after: int = game_state.fleet_resource(GameState.RESOURCE_ENERGY)
if fleet_energy_before - fleet_energy_after != ENERGY_PER_SHOT:
return _fail_now("firing spent %d fleet energy, expected exactly %d" \
% [fleet_energy_before - fleet_energy_after, ENERGY_PER_SHOT])
if game_state.banked_resource(GameState.RESOURCE_ENERGY) != 500:
return _fail_now("firing touched the bank (%d), expected it untouched at 500" \
% game_state.banked_resource(GameState.RESOURCE_ENERGY))
print("firing drained exactly %d from the fleet hold, bank untouched" % ENERGY_PER_SHOT)
print("TEST_LASER_AMMO: PASS")
quit(0)
return true
return false
func _fail_now(message: String) -> bool:
print("TEST_LASER_AMMO: FAIL (%s)" % message)
quit(1)
return true