Implemented dynamic projectile spread.

This commit is contained in:
Henry Faber 2026-06-16 03:13:50 +01:00
parent 029175ccde
commit a9af69e7bb
5 changed files with 146 additions and 72 deletions

View file

@ -1,13 +1,13 @@
class_name ShootComponent extends Node
@export var stagger_offset: float = 0.5 # Time delay per projectile index (seconds)
@export var horizontal_offset: float = 1.0 # Horizontal distance per projectile index
@export var stagger_offset: float = 0.5 # Time delay per projectile index (seconds)
@export var horizontal_offset: float = 1.0 # Horizontal distance per projectile index
@onready var weapon_component: Node = %WeaponComponent
@onready var player = $".."
func shoot():
var weapon_data = weapon_component.data # WeaponShot resource configuration
var weapon_data = weapon_component.data # WeaponShot resource configuration
var current_time = Time.get_ticks_msec() / 1000.0
var total_projectiles = weapon_data.projectiles
@ -17,66 +17,49 @@ func shoot():
var center_index = (total_projectiles - 1) / 2.0
if total_projectiles > 0:
for group in range(weapon_data.num_groups):
# Calculate projectile indices for this group
# For 1 group: 0..N-1
# For 2 groups: 0..N/2-1 and N/2..N-1
# For 3 groups: 0..N/3-1 and N/3..2N/3-1 and 2N/3..N-1
var group_start: int
var group_end: int
for b in range(total_projectiles):
var bullet_scene = weapon_data.bullet_scene
var bullet := bullet_scene.instantiate() as Node2D
get_tree().root.add_child(bullet)
if weapon_data.num_groups == 1:
group_start = 0
group_end = total_projectiles
elif weapon_data.num_groups == 2:
group_start = 0
group_end = total_projectiles / 2 if total_projectiles % 2 == 0 else (total_projectiles - 1) / 2
elif weapon_data.num_groups == 3:
var third = total_projectiles / 3
group_start = 0
group_end = third if third > 0 else 1
# Calculate index relative to center (0 = center, -1/+1 = first from center)
var index_from_center: float = b - center_index
# Calculate timing offset for staggered firing
var time_offset: float = index_from_center * weapon_data.stagger_offset
# Calculate horizontal offset for this bullet (symmetrical: left=-ve, right=+ve)
var bullet_horizontal_offset: float = index_from_center * weapon_data.horizontal_offset
# Calculate vertical offset from center point (symmetrical vertical spread)
var distance_from_center: float = abs(index_from_center)
var vertical_offset: float = (distance_from_center * weapon_data.origin * -1) / 2
# Final position combines symmetrical horizontal spread with fixed vertical offset from weapon origin
bullet.position = player.position + Vector2(bullet_horizontal_offset, weapon_data.origin)
# Set timing properties on the bullet
bullet.time_offset = time_offset
bullet.fire_time = current_time + time_offset
# Set the angle for bullets - symmetrical spread around vertical (straight up = -90°)
if weapon_data.angle == 0:
# If angle is 0, all projectiles fire straight up
bullet.angle = -90.0
else:
continue # Skip if weapon_data.num_groups > 3
# Set angle for bullets - symmetrical spread around vertical (straight up = -90°)
if index_from_center == 0:
# Center projectile fires straight up
bullet.angle = -90.0
else:
# Left projectiles: positive angle (up and to the left), Right projectiles: negative angle (up and to the right)
bullet.angle = -90.0 + weapon_data.angle * sign(index_from_center)
for b in range(group_start, group_end):
var bullet_scene = weapon_data.bullet_scene
var bullet := bullet_scene.instantiate() as Node
get_tree().root.add_child(bullet)
# Calculate index relative to group center for staggering
var group_center: float = (group_start + group_end - 1) / 2.0
var index_from_center: float = b - group_center
# Calculate timing offset for staggered firing
var time_offset: float = abs(index_from_center) * stagger_offset
# Calculate horizontal offset for this bullet (symmetrical within group)
var bullet_horizontal_offset: float = index_from_center * weapon_data.horizontal_offset
# Calculate vertical offset from center point (symmetrical vertical spread within group)
var distance_from_center: float = abs(index_from_center)
var vertical_offset: float = (distance_from_center * weapon_data.origin * -1) / 2
# Calculate final position based on firing angle using polar coordinates
var angle_rad: float = weapon_data.firing_angle * 0.0174533 # Convert degrees to radians
var spread_angle: float = -weapon_data.firing_angle * 0.0174533 # Spread opposite to firing angle
# Calculate position using polar coordinates from player origin
var origin_x: float = weapon_data.origin * 0.5 # Distribute origin horizontally
var origin_y: float = weapon_data.origin
var pos_x: float = player.position.x + origin_x + bullet_horizontal_offset
var pos_y: float = player.position.y + origin_y + vertical_offset
# Apply angular offset
var angle_offset_x: float = -weapon_data.origin * sin(spread_angle)
var angle_offset_y: float = -weapon_data.origin * cos(spread_angle)
bullet.position = Vector2(pos_x + angle_offset_x, pos_y + angle_offset_y)
# Set timing properties on the bullet
bullet.time_offset = time_offset
bullet.fire_time = current_time + time_offset
# Subtract projectile spacing from current Player travel for next shot
player.travel -= weapon_data.spacing