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.
This commit is contained in:
parent
17d0cf7e66
commit
facbd83dd2
6 changed files with 236 additions and 6 deletions
|
|
@ -18,11 +18,16 @@ const TARGET_COLOR := Color(0.6, 0.95, 1.0)
|
|||
const BEAM_HALF_WIDTH := 22.0
|
||||
const DRAG_DEADZONE := 22.0
|
||||
const FLASH_DURATION := 0.22
|
||||
# #61: ammunition from the fleet hold, mirroring #60's gas bomber exactly.
|
||||
const ENERGY_PER_SHOT: int = 8
|
||||
|
||||
@export var minimum_zoom: float = 0.4
|
||||
@onready var fleet: Node2D = get_node("../Fleet")
|
||||
@onready var camera: Camera2D = get_node("../Camera2D")
|
||||
@onready var button: IconButton = get_node("../UI/LaserButton")
|
||||
@onready var status: Label = get_node("../UI/LaserStatus")
|
||||
|
||||
var status_message_until: int = 0
|
||||
|
||||
var aiming := false
|
||||
var aim_touch := -1
|
||||
|
|
@ -37,15 +42,27 @@ var last_hit_count := 0
|
|||
|
||||
func _ready() -> void:
|
||||
button.visible = false
|
||||
status.text = ""
|
||||
queue_redraw()
|
||||
|
||||
|
||||
func _can_afford_shot() -> bool:
|
||||
return GameState.fleet_resource(GameState.RESOURCE_ENERGY) >= ENERGY_PER_SHOT
|
||||
|
||||
|
||||
func _say(message: String) -> void:
|
||||
status.text = message
|
||||
status_message_until = Time.get_ticks_msec() + 3000
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
var had_visuals := aiming or not beams.is_empty()
|
||||
button.visible = fleet.has_capability("laser")
|
||||
button.enabled = _next_ready_laser_ship() != null
|
||||
button.enabled = _next_ready_laser_ship() != null and _can_afford_shot()
|
||||
if not button.visible and aiming:
|
||||
_cancel_aim()
|
||||
if status.text != "" and Time.get_ticks_msec() >= status_message_until:
|
||||
status.text = ""
|
||||
|
||||
if restoring_zoom and not aiming:
|
||||
var z := move_toward(camera.zoom.x, saved_zoom, maxf(saved_zoom, 0.1) * delta * 5.0)
|
||||
|
|
@ -63,14 +80,22 @@ func _process(delta: float) -> void:
|
|||
|
||||
|
||||
func can_begin_aim() -> bool:
|
||||
return button.visible and button.enabled and not button._blocked_by_modal() \
|
||||
and not aiming
|
||||
# Deliberately NOT button.enabled: an unaffordable shot still needs the
|
||||
# press to reach here so it can explain itself (#60's identical
|
||||
# reasoning on the gas bomber). A wholly unready fleet (no laser ship at
|
||||
# all) has nothing more useful to say than "greyed out".
|
||||
return button.visible and not button._blocked_by_modal() \
|
||||
and not aiming and _next_ready_laser_ship() != null
|
||||
|
||||
|
||||
func handle_touch(event: InputEventScreenTouch) -> bool:
|
||||
if event.pressed:
|
||||
if not can_begin_aim() or not Rect2(button.global_position, button.size).has_point(event.position):
|
||||
return false
|
||||
if not _can_afford_shot():
|
||||
_say("Need %d energy in the fleet hold to fire (have %d)" \
|
||||
% [ENERGY_PER_SHOT, GameState.fleet_resource(GameState.RESOURCE_ENERGY)])
|
||||
return true
|
||||
aiming = true
|
||||
aim_touch = event.index
|
||||
aim_start_screen = event.position
|
||||
|
|
@ -149,16 +174,22 @@ func _fire() -> void:
|
|||
if shooter == null:
|
||||
_cancel_aim()
|
||||
return
|
||||
if not _can_afford_shot():
|
||||
_say("Need %d energy in the fleet hold to fire (have %d)" \
|
||||
% [ENERGY_PER_SHOT, GameState.fleet_resource(GameState.RESOURCE_ENERGY)])
|
||||
_cancel_aim()
|
||||
return
|
||||
var component: Node = shooter.get_component("laser")
|
||||
if not component.consume_shot():
|
||||
_cancel_aim()
|
||||
return
|
||||
GameState.withdraw_fleet_resource(GameState.RESOURCE_ENERGY, ENERGY_PER_SHOT)
|
||||
var direction: Vector2 = (aim_target - _aim_origin())
|
||||
direction = direction.normalized() if direction.length() > 0.001 else Vector2.UP
|
||||
var beam_to: Vector2 = shooter.global_position + direction * component.beam_range
|
||||
_strike(shooter.global_position, beam_to, direction, int(component.damage))
|
||||
_finish_aim()
|
||||
button.enabled = _next_ready_laser_ship() != null
|
||||
button.enabled = _next_ready_laser_ship() != null and _can_afford_shot()
|
||||
|
||||
|
||||
func _strike(from: Vector2, to: Vector2, direction: Vector2, damage: int) -> void:
|
||||
|
|
|
|||
Loading…
Reference in a new issue