#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>
This commit is contained in:
Loïc Wisniewski 2026-08-21 19:44:14 +02:00
commit 2f161621c3
495 changed files with 571 additions and 567 deletions

View file

@ -0,0 +1,244 @@
class_name AimedFleetAbilityController
extends Node2D
# Shared press-drag-release input and camera behavior for manually aimed fleet
# abilities. Subclasses provide the component, ammunition, target calculation,
# shot effect, and effect visuals.
const AIM_DEADZONE: float = 22.0
@export var minimum_zoom: float = 0.4
@onready var fleet: Node2D = get_node("../Fleet")
@onready var camera: Camera2D = get_node("../Camera2D")
var button: IconButton
var status: Label
var status_message_until: int = 0
var aiming: bool = false
var aim_touch: int = -1
var aim_start_screen: Vector2 = Vector2.ZERO
var aim_screen: Vector2 = Vector2.ZERO
var aim_target: Vector2 = Vector2.ZERO
var saved_zoom: float = 1.0
var restoring_zoom: bool = false
func _ready() -> void:
button = get_node(_button_path())
status = get_node(_status_path())
button.visible = false
status.text = ""
queue_redraw()
func _process(delta: float) -> void:
var had_visuals: bool = aiming or _has_effect_visuals()
button.visible = fleet.has_capability(_component_id())
button.enabled = _next_ready_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 zoom: float = move_toward(camera.zoom.x, saved_zoom,
maxf(saved_zoom, 0.1) * delta * 5.0)
camera.zoom = Vector2(zoom, zoom)
if is_equal_approx(zoom, saved_zoom):
restoring_zoom = false
_advance_effects(delta)
if aiming or _has_effect_visuals() or had_visuals:
queue_redraw()
func can_begin_aim() -> bool:
# An unaffordable shot still claims the press so the status label can
# explain what is missing instead of silently passing the touch through.
return button.visible and not button._blocked_by_modal() \
and not aiming and _next_ready_ship() != null
# Fires immediately at a world point, with no press-drag-release. The mouse
# path (#80) has a crosshair already - it does not need to be dragged into
# existence - so it aims and fires in one call rather than pretending to be
# a touch.
func fire_at(world_target: Vector2) -> bool:
if not button.visible or button._blocked_by_modal() or _next_ready_ship() == null:
return false
if not _can_afford_shot():
_say(_unaffordable_message())
return false
# Recorded before firing because _fire() ends by restoring it, and a stale
# value from an earlier aim would snap the camera somewhere unrelated.
saved_zoom = camera.zoom.x
restoring_zoom = false
aim_target = world_target
_fire()
return true
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(_unaffordable_message())
return true
aiming = true
aim_touch = event.index
aim_start_screen = event.position
aim_screen = event.position
saved_zoom = camera.zoom.x
restoring_zoom = false
button.pressed_down = true
button.queue_redraw()
_update_aim()
return true
if event.index != aim_touch:
return false
if aim_start_screen.distance_to(aim_screen) >= AIM_DEADZONE:
_fire()
elif _aim_at_nearest_enemy():
# Tapping the button used to do nothing at all - the press was claimed,
# the aim began, and releasing without dragging simply threw it away.
# A tap now fires at the nearest enemy in range; dragging remains the
# way to place a shot precisely.
_fire()
else:
_say("Nothing in range - drag from the button to aim")
_cancel_aim()
return true
# Points aim_target at the closest enemy within reach, or reports that there
# is none. Both aimed abilities read only the aim_target when they fire - the
# laser takes its DIRECTION and always extends to full range, the gas bomb
# takes the point itself - so setting it here is all auto-aim has to do, and
# each ability keeps its own meaning for it.
func _aim_at_nearest_enemy() -> bool:
var origin: Vector2 = _aim_origin()
var reach: float = _aim_max_range()
var nearest: Node2D = null
var nearest_distance: float = INF
for enemy in get_tree().get_nodes_in_group("enemies"):
if not is_instance_valid(enemy) or not (enemy is Node2D):
continue
var distance: float = origin.distance_to((enemy as Node2D).global_position)
if distance > reach or distance >= nearest_distance:
continue
nearest_distance = distance
nearest = enemy
if nearest == null:
return false
aim_target = nearest.global_position
queue_redraw()
return true
func handle_drag(event: InputEventScreenDrag) -> bool:
if not aiming or event.index != aim_touch:
return false
aim_screen = event.position
_update_aim()
return true
func _update_aim() -> void:
var origin: Vector2 = _aim_origin()
aim_target = _calculate_aim_target(aim_screen - aim_start_screen, origin)
_update_aim_zoom(origin, aim_target)
queue_redraw()
func _update_aim_zoom(origin: Vector2, target: Vector2) -> void:
var displacement: Vector2 = target - origin
var half_screen: Vector2 = get_viewport_rect().size * 0.5 - Vector2(54.0, 54.0)
var padding: float = _aim_padding()
var fit_x: float = saved_zoom if absf(displacement.x) < 1.0 \
else half_screen.x / (absf(displacement.x) + padding)
var fit_y: float = saved_zoom if absf(displacement.y) < 1.0 \
else half_screen.y / (absf(displacement.y) + padding)
var desired: float = clampf(minf(saved_zoom, minf(fit_x, fit_y)),
minimum_zoom, saved_zoom)
camera.zoom = Vector2(desired, desired)
func _finish_aim() -> void:
aiming = false
aim_touch = -1
button.pressed_down = false
button.queue_redraw()
restoring_zoom = true
func _cancel_aim() -> void:
_finish_aim()
queue_redraw()
func _fire() -> void:
var ship: Node2D = _next_ready_ship()
if ship == null:
_cancel_aim()
return
if not _can_afford_shot():
_say(_unaffordable_message())
_cancel_aim()
return
var component: Node = ship.get_component(_component_id())
if component == null or not _consume_shot(component):
_cancel_aim()
return
GameState.withdraw_fleet_resource(_ammo_resource(), _ammo_cost())
_perform_shot(ship, component)
_finish_aim()
button.enabled = _next_ready_ship() != null and _can_afford_shot()
func _next_ready_ship() -> Node2D:
for ship in fleet.ships:
if not is_instance_valid(ship):
continue
var component: Node = ship.get_component(_component_id())
if component != null and component.is_ready():
return ship
return null
func _can_afford_shot() -> bool:
return GameState.fleet_resource(_ammo_resource()) >= _ammo_cost()
func _unaffordable_message() -> String:
var resource_type: String = _ammo_resource()
return "Need %d %s in the fleet hold to fire (have %d)" % [
_ammo_cost(), GameState.resource_name(resource_type).to_lower(),
GameState.fleet_resource(resource_type),
]
func _say(message: String) -> void:
status.text = message
status_message_until = Time.get_ticks_msec() + 3000
func _aim_origin() -> Vector2:
return fleet.get_centroid()
# Subclass hooks.
func _button_path() -> NodePath: return NodePath()
func _status_path() -> NodePath: return NodePath()
func _component_id() -> String: return ""
func _ammo_resource() -> String: return ""
func _ammo_cost() -> int: return 0
func _aim_padding() -> float: return 0.0
# How far this ability can reach, for auto-aim and for the aim guide.
func _aim_max_range() -> float: return 0.0
func _has_effect_visuals() -> bool: return false
func _advance_effects(_delta: float) -> void: pass
func _consume_shot(_component: Node) -> bool: return false
func _perform_shot(_ship: Node2D, _component: Node) -> void: pass
func _calculate_aim_target(_drag: Vector2, origin: Vector2) -> Vector2: return origin

View file

@ -0,0 +1 @@
uid://dbahjmvwru7bp

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1 @@
uid://csfvnrer8sb4a

View file

@ -0,0 +1,101 @@
extends Node2D
# Points every armed vessel's gun wherever the right-hand stick is pushed.
#
# The fleet's guns used to seek their own targets and fire on their own. That
# made combat something that happened TO the player rather than something they
# did, and left the right half of the screen with nothing on it. Aiming is now
# the second stick, and this is the only thing that tells a weapon to fire.
#
# Deliberately a scene node rather than state on the fleet or in GameState:
# it is input plumbing, it belongs to whichever scene is showing the stick,
# and a fleet that moves between scenes should arrive with its guns silent
# until the local stick says otherwise.
# The aim guide: a dotted line from the fleet along the heading being fired,
# out to the guns' own range. Deliberately faint and broken rather than a
# solid beam - it has to answer "where will this go and how far" at a glance
# without competing with the shots themselves for attention.
const GUIDE_DASH: float = 14.0
const GUIDE_GAP: float = 16.0
const GUIDE_WIDTH: float = 2.0
const GUIDE_COLOR: Color = Color(1.0, 0.55, 0.38, 0.28)
@onready var fleet: Node2D = get_node("../Fleet")
# Optional so a scene without a fire stick (a preview, a test harness) still
# runs - the guns simply stay quiet, which is the correct resting state.
@onready var stick: Node = get_node_or_null("../UI/FireStick")
var aim_direction: Vector2 = Vector2.ZERO
# Set by MouseCombatController (#80) when a desktop player holds the left
# button. Kept as a second input rather than a second aimer, so there is still
# exactly one thing in the scene that points a gun.
var external_direction: Vector2 = Vector2.ZERO
func _process(_delta: float) -> void:
var direction: Vector2 = stick.direction if stick != null else Vector2.ZERO
if direction == Vector2.ZERO:
# The stick wins when both are live: a finger on it is a deliberate
# act, a mouse resting somewhere is not.
direction = external_direction
aim(direction)
# The guide starts at the fleet, which is usually moving, so this redraws
# while aiming rather than only when the heading changes. Once the stick is
# released it redraws once more to clear the line, then stops.
var was_aiming: bool = aim_direction != Vector2.ZERO
aim_direction = direction
if direction != Vector2.ZERO or was_aiming:
queue_redraw()
# Also the entry point for anything driving the guns without a stick - tests
# aim through this rather than reaching into each weapon component, so what
# they exercise is the same path the stick uses.
func aim(direction: Vector2) -> void:
if fleet == null:
return
for ship in fleet.ships:
if not is_instance_valid(ship):
continue
var weapon: Node = ship.get_component("weapon")
if weapon != null:
weapon.set_fire_direction(direction)
# How far the guide reaches: the actual range of a gun that is actually
# aboard, so the line never promises reach the fleet does not have.
func guide_range() -> float:
for ship in fleet.ships:
if not is_instance_valid(ship):
continue
var weapon: Node = ship.get_component("weapon")
if weapon != null:
return float(weapon.fire_range)
return 0.0
func _draw() -> void:
if aim_direction == Vector2.ZERO or fleet == null or fleet.ships.is_empty():
return
var reach: float = guide_range()
if reach <= 0.0:
return
var origin: Vector2 = fleet.get_centroid()
var heading: Vector2 = aim_direction.normalized()
var travelled: float = GUIDE_GAP
while travelled < reach:
var dash_end: float = minf(travelled + GUIDE_DASH, reach)
draw_line(origin + heading * travelled, origin + heading * dash_end,
GUIDE_COLOR, GUIDE_WIDTH, true)
travelled = dash_end + GUIDE_GAP
func is_firing() -> bool:
for ship in fleet.ships:
if not is_instance_valid(ship):
continue
var weapon: Node = ship.get_component("weapon")
if weapon != null and weapon.is_firing():
return true
return false

View file

@ -0,0 +1 @@
uid://dpfqioif8n7rr

View file

@ -0,0 +1,366 @@
extends Node2D
@export var min_zoom: float = 0.4
@export var max_zoom: float = 6.0
@export var camera_follow_speed: float = 5.0
# Distance (world units) below which the camera snaps exactly onto the fleet
# instead of continuing to lerp. Without this the exponential lerp only ever
# approaches the target asymptotically and NEVER arrives, so the camera keeps
# drifting by a fraction of a pixel every single frame - forever, even with
# the fleet completely stationary. That kept the background shaders' camera_pos
# uniform changing every frame, which made screen tearing permanently visible
# as horizontal bands of the background shifted sideways against each other.
# Same class of bug (and same fix) as the ship arrival oscillation.
@export var camera_snap_distance: float = 0.5
@export var debug_input_logging: bool = false
@onready var camera: Camera2D = $Camera2D
@onready var fleet: Node2D = $Fleet
# Optional: scenes without a joystick (none currently) still work.
@onready var joystick: TouchJoystick = get_node_or_null("UI/Joystick")
# The right-hand stick: aims and fires. Optional for the same reason as the
# movement stick - a scene without one still runs, with the guns silent.
@onready var fire_stick: TouchJoystick = get_node_or_null("UI/FireStick")
# Navigation section. Optional so a scene without it still works.
@onready var pivot_cw_button: IconButton = get_node_or_null("UI/PivotCWButton")
@onready var pivot_ccw_button: IconButton = get_node_or_null("UI/PivotCCWButton")
@onready var gas_bomb_controller: GasBombController = get_node_or_null("GasBombController")
# Untyped on purpose: LaserController resolves fine as a static type when a
# script IS the -s entry point, but the same class_name reference fails to
# parse ("Could not find type") when the script holding it is only reached
# indirectly - e.g. Main.tscn loaded from inside a _screenshot_tool.gd
# preview - for a class_name this new. Node + duck-typed calls sidesteps it.
@onready var laser_controller: Node = get_node_or_null("LaserController")
@onready var speed_fader: Node = get_node_or_null("UI/SpeedFader")
var touches: Dictionary = {}
var pinch_active: bool = false
var pinch_start_distance: float = 0.0
var pinch_start_zoom: float = 1.0
# Held pivot buttons turn the formation continuously at this rate; a quick
# tap that accumulates less than PIVOT_MIN_STEP is topped up to it on
# release, so tapping still produces a clear, visible nudge.
const PIVOT_SPEED: float = 1.6
const PIVOT_MIN_STEP: float = PI / 10.0
# #72: the tap top-up below used to fire in full on EVERY release, with no
# regard for how recently the last one landed - so tapping fast enough
# granted rotation far above PIVOT_SPEED's smooth rate, well beyond what
# holding the button can ever produce. Each of those artificially-frequent
# re-seats (Fleet.apply_formation()) forces every ship into a fresh
# max-speed catch-up dash toward its new slot with no time to settle
# between them, and since outer-wedge ships swing further per pivot than
# inner ones, spamming made them visibly diverge in speed ("desync") while
# each individual ship's own bounded max_speed made the fleet as a whole
# read as constantly at its speed ceiling ("build up speed"). This cooldown
# is exactly the time a genuine hold needs to reach PIVOT_MIN_STEP on its
# own, so tap-spam can't out-pace holding the button down.
const PIVOT_TAP_COOLDOWN: float = PIVOT_MIN_STEP / PIVOT_SPEED
var pivot_hold_amount: float = 0.0
# Tracked PER DIRECTION, not shared - a single quick correction (tap the
# other way right after an overshoot) is normal play, not the spam this
# cooldown targets, and Q/E each independently producing their own minimum
# step back-to-back is an existing, tested guarantee (test_keyboard_input.gd).
# Only repeatedly re-tapping the SAME direction needs to be capped.
var pivot_cw_tap_cooldown_left: float = 0.0
var pivot_ccw_tap_cooldown_left: float = 0.0
# Touch index currently holding each pivot button, or -1. These buttons are
# driven straight from raw touch events rather than through _gui_input,
# because Godot's Control GUI treats touch as a single emulated pointer and
# will not reliably deliver a SECOND simultaneous finger to another
# Control - which meant pivoting while steering with the joystick silently
# did nothing. The joystick already bypasses the GUI for the same reason.
var pivot_cw_touch: int = -1
var pivot_ccw_touch: int = -1
# --- keyboard and mouse ----------------------------------------------------
# The touch UI is the whole story on a phone. On a machine with a keyboard -
# which in practice means the web build - WASD or the arrow keys steer, Q/E
# pivot the formation, and the wheel zooms. Clicking the icon buttons already
# works without any code here, because the project emulates touch from the
# mouse (see project.godot), which is the same path the buttons already read.
#
# Keys are tracked from events rather than polled off the Input singleton so
# the whole thing is drivable from a headless test.
const KEY_DIRECTIONS: Dictionary = {
KEY_W: Vector2.UP, KEY_UP: Vector2.UP,
KEY_S: Vector2.DOWN, KEY_DOWN: Vector2.DOWN,
KEY_A: Vector2.LEFT, KEY_LEFT: Vector2.LEFT,
KEY_D: Vector2.RIGHT, KEY_RIGHT: Vector2.RIGHT,
}
const KEY_PIVOT_CW: int = KEY_E
const KEY_PIVOT_CCW: int = KEY_Q
const WHEEL_ZOOM_STEP: float = 1.12
var keys_down: Dictionary = {}
var keyboard_seen: bool = false
var keyboard_steering: bool = false
func _ready() -> void:
if joystick != null:
joystick.direction_changed.connect(_on_joystick_direction)
func _held_pivot_direction() -> float:
var dir: float = 0.0
if pivot_cw_button != null and pivot_cw_button.pressed_down:
dir += 1.0
if pivot_ccw_button != null and pivot_ccw_button.pressed_down:
dir -= 1.0
if keys_down.has(KEY_PIVOT_CW):
dir += 1.0
if keys_down.has(KEY_PIVOT_CCW):
dir -= 1.0
return clampf(dir, -1.0, 1.0)
func keyboard_direction() -> Vector2:
var dir: Vector2 = Vector2.ZERO
for key in KEY_DIRECTIONS:
if keys_down.has(key):
dir += KEY_DIRECTIONS[key]
return Vector2.ZERO if dir == Vector2.ZERO else dir.normalized()
func _handle_key(event: InputEventKey) -> void:
if event.echo:
return
if event.pressed:
keys_down[event.keycode] = true
_note_keyboard()
else:
keys_down.erase(event.keycode)
# A quick tap of a pivot key gets the same minimum step a tapped
# button does, rather than turning by a barely-visible sliver.
if event.keycode == KEY_PIVOT_CW:
_finish_pivot_tap(1.0)
elif event.keycode == KEY_PIVOT_CCW:
_finish_pivot_tap(-1.0)
func _note_keyboard() -> void:
if keyboard_seen:
return
keyboard_seen = true
# The stick is only taken away where there is no touchscreen to work it
# with. A touch device that also has a keyboard keeps both, and a device
# wrongly reported as non-touch still shows the stick until the player
# has demonstrably used a keyboard - so nobody is ever left with no way
# to steer.
if joystick != null and not DisplayServer.is_touchscreen_available():
joystick.visible = false
func _button_holds(button: IconButton, at_position: Vector2) -> bool:
return button != null and Rect2(button.global_position, button.size).has_point(at_position)
func _set_button_held(button: IconButton, held: bool) -> void:
if button == null or button.pressed_down == held:
return
button.pressed_down = held
button.queue_redraw()
# Returns true when a pivot button consumed the touch.
func _handle_pivot_touch(event: InputEventScreenTouch) -> bool:
if event.pressed:
if _button_holds(pivot_cw_button, event.position) and pivot_cw_touch == -1:
pivot_cw_touch = event.index
_set_button_held(pivot_cw_button, true)
return true
if _button_holds(pivot_ccw_button, event.position) and pivot_ccw_touch == -1:
pivot_ccw_touch = event.index
_set_button_held(pivot_ccw_button, true)
return true
return false
if event.index == pivot_cw_touch:
pivot_cw_touch = -1
_set_button_held(pivot_cw_button, false)
_finish_pivot_tap(1.0)
return true
if event.index == pivot_ccw_touch:
pivot_ccw_touch = -1
_set_button_held(pivot_ccw_button, false)
_finish_pivot_tap(-1.0)
return true
return false
func _update_pivot(delta: float) -> void:
var dir: float = _held_pivot_direction()
if dir == 0.0:
return
var amount: float = PIVOT_SPEED * delta * dir
pivot_hold_amount += absf(amount)
_pivot(amount)
func _finish_pivot_tap(direction: float) -> void:
# Fired on release. A press held long enough has already turned far
# enough on its own; a brief tap gets topped up to the minimum step -
# but only once THAT DIRECTION's own cooldown has cleared, so spamming
# one side can't grant rotation faster than holding the button would.
var cooldown_left: float = pivot_cw_tap_cooldown_left if direction > 0.0 else pivot_ccw_tap_cooldown_left
if pivot_hold_amount < PIVOT_MIN_STEP and cooldown_left <= 0.0:
_pivot((PIVOT_MIN_STEP - pivot_hold_amount) * direction)
if direction > 0.0:
pivot_cw_tap_cooldown_left = PIVOT_TAP_COOLDOWN
else:
pivot_ccw_tap_cooldown_left = PIVOT_TAP_COOLDOWN
pivot_hold_amount = 0.0
func _pivot(radians: float) -> void:
# Turns the formation and re-seats the ships already flying it, so the
# change is visible immediately rather than only on the next rebuild.
# apply_formation() writes the new offsets into Fleet.formation_offsets,
# which move_to()/move_in_direction() also read - so a pivot applied
# while the joystick is steering survives the next steering update
# instead of being overwritten by the old layout.
GameState.rotate_formation(radians)
fleet.apply_formation()
func _on_joystick_direction(direction: Vector2) -> void:
# Steering is re-issued every frame in _process while the stick is held,
# so this only needs to handle the release (stick back to centre).
if direction == Vector2.ZERO:
fleet.stop()
func _process(delta: float) -> void:
pivot_cw_tap_cooldown_left = maxf(pivot_cw_tap_cooldown_left - delta, 0.0)
pivot_ccw_tap_cooldown_left = maxf(pivot_ccw_tap_cooldown_left - delta, 0.0)
_update_pivot(delta)
# Keep the heading fresh while a control is held. Fleet advances its own
# anchor in _process, so this only needs to report the direction. The
# keyboard takes precedence, and releasing the keys has to stop the fleet
# itself - unlike the stick, there is no centred-again signal to hook.
var keyed: Vector2 = keyboard_direction()
if keyed != Vector2.ZERO:
keyboard_steering = true
fleet.move_in_direction(keyed)
elif keyboard_steering:
keyboard_steering = false
fleet.stop()
elif joystick != null and joystick.direction != Vector2.ZERO:
fleet.move_in_direction(joystick.direction)
var target: Vector2 = fleet.get_centroid()
if camera.global_position.distance_to(target) <= camera_snap_distance:
# Snap and stop dead - see camera_snap_distance. Assigning the exact
# target means a settled fleet produces a bit-identical camera position
# frame after frame, so the background shaders stop re-rendering a
# shifted image and tearing has nothing to reveal.
camera.global_position = target
return
var t: float = clamp(camera_follow_speed * delta, 0.0, 1.0)
camera.global_position = camera.global_position.lerp(target, t)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventScreenTouch:
_handle_touch(event)
elif event is InputEventScreenDrag:
_handle_drag(event)
elif event is InputEventMagnifyGesture:
_handle_magnify(event)
elif event is InputEventKey:
_handle_key(event)
elif event is InputEventMouseButton and event.pressed:
# Wheel zoom, the desktop equivalent of the pinch.
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
_set_zoom(camera.zoom.x * WHEEL_ZOOM_STEP)
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
_set_zoom(camera.zoom.x / WHEEL_ZOOM_STEP)
func _handle_touch(event: InputEventScreenTouch) -> void:
# The joystick gets first refusal on every touch, and only claims ones
# that begin inside its own rect - so pinch-to-zoom still works anywhere
# else on screen.
if gas_bomb_controller != null and gas_bomb_controller.handle_touch(event):
return
if laser_controller != null and laser_controller.handle_touch(event):
return
if speed_fader != null and speed_fader.handle_touch(event):
return
if _handle_pivot_touch(event):
return
if joystick != null and joystick.visible and joystick.handle_touch(event):
return
# Order between the two sticks does not matter: each only claims touches
# that begin inside its own rect, and their rects do not overlap.
if fire_stick != null and fire_stick.visible and fire_stick.handle_touch(event):
return
if event.pressed:
touches[event.index] = event.position
if touches.size() == 2:
_start_pinch()
if debug_input_logging:
print("DEBUG touch pressed idx=%d pos=%s active_touches=%d" % [event.index, event.position, touches.size()])
else:
touches.erase(event.index)
if touches.size() < 2:
pinch_active = false
if debug_input_logging:
print("DEBUG touch released idx=%d active_touches=%d" % [event.index, touches.size()])
func _handle_drag(event: InputEventScreenDrag) -> void:
if gas_bomb_controller != null and gas_bomb_controller.handle_drag(event):
return
if laser_controller != null and laser_controller.handle_drag(event):
return
if speed_fader != null and speed_fader.handle_drag(event):
return
if joystick != null and joystick.visible and joystick.handle_drag(event):
return
if fire_stick != null and fire_stick.visible and fire_stick.handle_drag(event):
return
if not touches.has(event.index):
return
touches[event.index] = event.position
if pinch_active and touches.size() == 2 and pinch_start_distance > 0.0:
var positions: Array = touches.values()
var current_distance: float = positions[0].distance_to(positions[1])
# Spreading the fingers apart zooms IN. Camera2D.zoom is a
# magnification (larger = closer), so the ratio goes
# current/start, not start/current.
var new_zoom: float = pinch_start_zoom * (current_distance / max(pinch_start_distance, 1.0))
_set_zoom(new_zoom)
if debug_input_logging:
print("DEBUG pinch drag distance=%.1f zoom=%.3f" % [current_distance, camera.zoom.x])
func _handle_magnify(event: InputEventMagnifyGesture) -> void:
# Fallback path: some platforms (and desktop trackpads) report pinch as a
# single semantic gesture instead of two raw touch streams. factor > 1
# means the gesture is spreading outward, which zooms in - and since
# Camera2D.zoom is a magnification, that multiplies.
_set_zoom(camera.zoom.x * event.factor)
if debug_input_logging:
print("DEBUG magnify gesture factor=%.3f zoom=%.3f" % [event.factor, camera.zoom.x])
func _start_pinch() -> void:
pinch_active = true
var positions: Array = touches.values()
pinch_start_distance = positions[0].distance_to(positions[1])
pinch_start_zoom = camera.zoom.x
func _set_zoom(raw_zoom: float) -> void:
var z: float = clamp(raw_zoom, min_zoom, max_zoom)
camera.zoom = Vector2(z, z)

View file

@ -0,0 +1 @@
uid://5tvbknhs0afu

View file

@ -0,0 +1,128 @@
class_name GasBombController
extends "res://controllers/AimedFleetAbilityController.gd"
const BOMB_COLOR := Color(0.96, 0.22, 0.72)
const TARGET_COLOR := Color(0.78, 0.32, 1.0)
const MAX_RANGE := 950.0
const AOE_RADIUS := 180.0
const DRAG_RADIUS := 260.0
const PROJECTILE_SPEED := 720.0
const DAMAGE := 3
const COOLDOWN := 5.0
const GAS_PER_SHOT: int = 5
var projectiles: Array = []
var explosions: Array = []
var last_hit_count: int = 0
var projectile_active: bool:
get: return not projectiles.is_empty()
var explosion_time: float:
get:
var longest: float = 0.0
for explosion in explosions:
longest = maxf(longest, float(explosion.time))
return longest
func _button_path() -> NodePath: return "../UI/GasBombButton"
func _status_path() -> NodePath: return "../UI/GasBombStatus"
func _component_id() -> String: return "gas_bomber"
func _ammo_resource() -> String: return GameState.RESOURCE_GAS
func _ammo_cost() -> int: return GAS_PER_SHOT
func _aim_padding() -> float: return AOE_RADIUS
func _aim_max_range() -> float: return MAX_RANGE
func _has_effect_visuals() -> bool: return not projectiles.is_empty() or not explosions.is_empty()
func _calculate_aim_target(drag: Vector2, origin: Vector2) -> Vector2:
if drag.length() < 0.001:
return origin
var distance: float = minf(drag.length() / DRAG_RADIUS, 1.0) * MAX_RANGE
return origin + drag.normalized() * distance
func _consume_shot(component: Node) -> bool:
return component.consume_shot(COOLDOWN)
func _perform_shot(ship: Node2D, _component: Node) -> void:
projectiles.append({
"position": ship.global_position,
"target": aim_target,
"trail": PackedVector2Array([ship.global_position]),
})
func _next_ready_bomber() -> Node2D:
return _next_ready_ship()
func _advance_effects(delta: float) -> void:
for i in range(projectiles.size() - 1, -1, -1):
var projectile: Dictionary = projectiles[i]
var distance: float = (projectile.position as Vector2).distance_to(projectile.target)
if distance <= PROJECTILE_SPEED * delta:
projectile.position = projectile.target
_explode(projectile.target)
projectiles.remove_at(i)
else:
projectile.position = (projectile.position as Vector2).move_toward(
projectile.target, PROJECTILE_SPEED * delta)
var trail: PackedVector2Array = projectile.trail
trail.append(projectile.position)
while trail.size() > 12:
trail.remove_at(0)
projectile.trail = trail
for i in range(explosions.size() - 1, -1, -1):
var explosion: Dictionary = explosions[i]
explosion.time = maxf(float(explosion.time) - delta, 0.0)
if float(explosion.time) <= 0.0:
explosions.remove_at(i)
func _explode(at: Vector2) -> void:
explosions.append({"position": at, "time": 0.8})
last_hit_count = 0
# Asteroids too - a blast that clears everything around it but leaves the
# rocks untouched would need explaining, and they take damage the same way.
for enemy in get_tree().get_nodes_in_group("enemies") \
+ get_tree().get_nodes_in_group("asteroids"):
if is_instance_valid(enemy) and enemy is Node2D \
and enemy.global_position.distance_to(at) <= AOE_RADIUS:
enemy.take_damage(DAMAGE)
last_hit_count += 1
func _draw() -> void:
if aiming:
NeonDraw.draw_glowing_line(self, _aim_origin(), aim_target, TARGET_COLOR, 0.7, 0.8)
_draw_ring(aim_target, AOE_RADIUS, TARGET_COLOR, 0.8)
_draw_ring(aim_target, 18.0, BOMB_COLOR, 1.0)
for projectile in projectiles:
var trail: PackedVector2Array = projectile.trail
if trail.size() >= 2:
for i in range(1, trail.size()):
var alpha: float = float(i) / trail.size()
draw_line(trail[i - 1], trail[i], Color(BOMB_COLOR, alpha * 0.65),
4.0 + alpha * 4.0, true)
draw_circle(projectile.position, 12.0, Color(BOMB_COLOR, 0.35))
NeonDraw.draw_glowing_circle(self, projectile.position, 7.0, BOMB_COLOR, 1.0)
for explosion in explosions:
var progress: float = 1.0 - float(explosion.time) / 0.8
var radius: float = lerpf(24.0, AOE_RADIUS, minf(progress * 1.5, 1.0))
draw_circle(explosion.position, radius, Color(BOMB_COLOR, (1.0 - progress) * 0.16))
_draw_ring(explosion.position, radius, BOMB_COLOR, 1.0 - progress)
for i in range(7):
var angle: float = TAU * float(i) / 7.0 + progress
var cloud_at: Vector2 = explosion.position \
+ Vector2(cos(angle), sin(angle)) * radius * 0.55
draw_circle(cloud_at, 18.0 + 14.0 * progress,
Color(BOMB_COLOR, (1.0 - progress) * 0.22))
func _draw_ring(center: Vector2, radius: float, color: Color, alpha: float) -> void:
draw_arc(center, radius, 0.0, TAU, 64, Color(color, 0.16 * alpha), 16.0, true)
draw_arc(center, radius, 0.0, TAU, 64, Color(color, 0.5 * alpha), 6.0, true)
draw_arc(center, radius, 0.0, TAU, 64, Color(1.0, 0.86, 0.96, 0.9 * alpha), 2.0, true)

View file

@ -0,0 +1 @@
uid://dj1n53msflyde

View file

@ -0,0 +1,101 @@
class_name LaserController
extends "res://controllers/AimedFleetAbilityController.gd"
const BEAM_COLOR := Color(0.3, 0.9, 1.0)
const TARGET_COLOR := Color(0.6, 0.95, 1.0)
const BEAM_HALF_WIDTH := 38.0
# The DRAWN beam is scaled from the hit width rather than tuned separately.
# The two were independent numbers and had drifted apart: the beam struck
# anything within 22 units of the line while its outer glow spanned about 11,
# so it killed things it visibly missed. NeonDraw's widest layer is 18px at
# scale 1, so this makes the glow span exactly what the beam hits.
const BEAM_WIDTH_SCALE := BEAM_HALF_WIDTH * 2.0 / 18.0
const FLASH_DURATION := 0.22
const ENERGY_PER_SHOT: int = 8
var beams: Array = []
var last_hit_count: int = 0
func _button_path() -> NodePath: return "../UI/LaserButton"
func _status_path() -> NodePath: return "../UI/LaserStatus"
func _component_id() -> String: return "laser"
func _ammo_resource() -> String: return GameState.RESOURCE_ENERGY
func _ammo_cost() -> int: return ENERGY_PER_SHOT
func _aim_padding() -> float: return BEAM_HALF_WIDTH
func _has_effect_visuals() -> bool: return not beams.is_empty()
func _aim_max_range() -> float:
# Falls back to the VESSEL DEFINITION's own beam_range, not to a copy of
# the number. This used to hardcode 950.0 in both branches, which silently
# became a lie the moment the definition's range changed - the aim line
# would have been drawn to a different distance than the beam actually
# reaches, and only in the cases where the fallback fires.
var ship: Node2D = _next_ready_ship()
if ship != null:
var component: Node = ship.get_component(_component_id())
if component != null:
return component.beam_range
return GameState.vessel_stat(GameState.VESSEL_LASER, "beam_range")
func _calculate_aim_target(drag: Vector2, origin: Vector2) -> Vector2:
# A beam always reaches full range; dragging chooses its direction only.
var direction: Vector2 = drag.normalized() if drag.length() > 0.001 else Vector2.UP
return origin + direction * _aim_max_range()
func _consume_shot(component: Node) -> bool:
return component.consume_shot()
func _perform_shot(ship: Node2D, component: Node) -> void:
var direction: Vector2 = aim_target - _aim_origin()
direction = direction.normalized() if direction.length() > 0.001 else Vector2.UP
var beam_to: Vector2 = ship.global_position + direction * component.beam_range
_strike(ship.global_position, beam_to, direction, int(component.damage))
func _next_ready_laser_ship() -> Node2D:
return _next_ready_ship()
func _advance_effects(delta: float) -> void:
for i in range(beams.size() - 1, -1, -1):
var beam: Dictionary = beams[i]
beam.time = maxf(float(beam.time) - delta, 0.0)
if float(beam.time) <= 0.0:
beams.remove_at(i)
func _strike(from: Vector2, to: Vector2, direction: Vector2, damage: int) -> void:
beams.append({"from": from, "to": to, "time": FLASH_DURATION})
last_hit_count = 0
var range_along: float = from.distance_to(to)
# Asteroids share the enemies' take_damage() contract, so the beam sweeps
# both groups in one pass. Shooting a path clear through a field is a
# perfectly good use of a laser, and the ore it leaves behind is the point.
for enemy in get_tree().get_nodes_in_group("enemies") \
+ get_tree().get_nodes_in_group("asteroids"):
if not is_instance_valid(enemy) or not (enemy is Node2D):
continue
var to_enemy: Vector2 = enemy.global_position - from
var along: float = to_enemy.dot(direction)
if along < 0.0 or along > range_along:
continue
var perpendicular: float = (to_enemy - direction * along).length()
if perpendicular <= BEAM_HALF_WIDTH:
enemy.take_damage(damage)
last_hit_count += 1
func _draw() -> void:
if aiming:
NeonDraw.draw_glowing_line(self, _aim_origin(), aim_target,
TARGET_COLOR, 0.7, 0.6)
for beam in beams:
var progress: float = 1.0 - float(beam.time) / FLASH_DURATION
var alpha: float = 1.0 - progress
NeonDraw.draw_glowing_line(self, beam.from, beam.to,
BEAM_COLOR, alpha, BEAM_WIDTH_SCALE * (1.0 - progress * 0.45))

View file

@ -0,0 +1 @@
uid://cdqg3isy1lbuf

View file

@ -0,0 +1,123 @@
extends Node2D
# Forgejo #80: on a computer the mouse aims and the left button fires, and the
# number keys choose which weapon the click uses - 1 the fleet's own guns, 2
# and up the special abilities.
#
# The twin sticks are built for thumbs. On a desktop they mean dragging a
# virtual stick with a pointer that could simply BE the crosshair, so this
# offers the shape a mouse actually wants: point, click, hold to keep firing.
#
# Touch is unaffected, and deliberately so: Godot emulates a mouse button from
# every touch, so without a guard a tap anywhere on a phone would fire the
# guns. The guard is the presence of a touchscreen rather than a "has used a
# mouse" flag, because the wrong answer has to fail safe - a desktop wrongly
# reported as touch-capable loses a convenience, while a phone wrongly treated
# as a desktop fires its weapons at every tap.
# A literal, not PackedStringArray(...): a constructor call is not a constant
# expression, and the script fails to PARSE rather than to run - which shows up
# as "nonexistent function" on a node that looks fine in the scene.
const ABILITY_ORDER: PackedStringArray = ["GasBombController", "LaserController"]
@onready var fleet: Node2D = get_node("../Fleet")
@onready var camera: Camera2D = get_node("../Camera2D")
@onready var fire_stick_controller: Node = get_node_or_null("../FireStickController")
# 0 is the fleet's own guns; 1.. index into `abilities` below.
var selected_weapon: int = 0
var firing: bool = false
var aim_world: Vector2 = Vector2.ZERO
var abilities: Array = []
func _ready() -> void:
# Resolved by NAME in a fixed order, so "2" is the gas bomb on every
# scene and in every session. Discovering them by scanning children would
# make the number keys depend on scene node order, which is not something
# a player can see or rely on.
for node_name in ABILITY_ORDER:
var controller: Node = get_node_or_null("../%s" % node_name)
if controller != null:
abilities.append(controller)
# AUTO decides from the display, which is what every scene ships with. The
# explicit settings exist because "is there a touchscreen" is not a question
# that can be answered honestly in every environment - headless reports one,
# so with AUTO alone the aiming below could never be exercised by a test at
# all, and the only covered behaviour would be the guard itself.
enum PointerMode { AUTO, ON, OFF }
@export var pointer_mode: PointerMode = PointerMode.AUTO
func is_enabled() -> bool:
match pointer_mode:
PointerMode.ON:
return true
PointerMode.OFF:
return false
return not DisplayServer.is_touchscreen_available()
func weapon_count() -> int:
return abilities.size() + 1
# Which weapon a given number key selects. Public so a test can ask rather
# than hardcode the mapping a second time.
func select_weapon(index: int) -> bool:
if index < 0 or index >= weapon_count():
return false
selected_weapon = index
_refresh_highlight()
return true
func _refresh_highlight() -> void:
# The chosen ability's own button carries the selection, so the answer to
# "what will a click do" is on screen rather than remembered.
for i in range(abilities.size()):
var button: Node = abilities[i].get("button")
if button != null:
button.highlighted = selected_weapon == i + 1
button.queue_redraw()
func _unhandled_input(event: InputEvent) -> void:
if not is_enabled():
return
if event is InputEventMouseMotion:
aim_world = get_global_mouse_position()
return
if event is InputEventKey and event.pressed and not event.echo:
var index: int = event.keycode - KEY_1
if index >= 0 and index < weapon_count() and select_weapon(index):
get_viewport().set_input_as_handled()
return
if not (event is InputEventMouseButton) or event.button_index != MOUSE_BUTTON_LEFT:
return
aim_world = get_global_mouse_position()
firing = event.pressed
if not event.pressed:
return
get_viewport().set_input_as_handled()
if selected_weapon > 0:
# One shot per click. Abilities have their own cooldowns and costs;
# holding the button down must not queue them up.
abilities[selected_weapon - 1].fire_at(aim_world)
func _process(_delta: float) -> void:
if fire_stick_controller == null:
return
# Handed to the stick controller rather than driving the weapons here, so
# there is still exactly one thing in the scene that aims a gun. The stick
# wins when both are live - a finger on it is a deliberate act, while a
# mouse merely resting somewhere is not.
var heading: Vector2 = Vector2.ZERO
if is_enabled() and firing and selected_weapon == 0 and not fleet.ships.is_empty():
heading = aim_world - fleet.get_centroid()
fire_stick_controller.external_direction = heading

View file

@ -0,0 +1 @@
uid://bo3rdm8elge8m