Added grouped shots as sub resources, as well as the abilit to mirror

pairs.
This commit is contained in:
Henry Faber 2026-06-26 23:54:53 +01:00
parent 313e013543
commit 3303d937fd
8 changed files with 166 additions and 34 deletions

View file

@ -6,50 +6,114 @@ class_name ShootComponent extends Node
func shoot():
var weapon_data = weapon_component.weapon_data
var current_time = Time.get_ticks_msec() / 1000.0
var total_projectiles = weapon_data.projectiles
if player.travel > weapon_data.spacing:
_shoot_standard(weapon_data, current_time)
_shoot_grouped(weapon_data, current_time)
# Calculate center index for symmetrical staggering
var center_index = (total_projectiles - 1) / 2.0
# Reset travel after firing to prevent multiple bursts per tap
player.travel = 0.0
if total_projectiles > 0:
for b in range(total_projectiles):
var bullet := weapon_data.bullet_scene.instantiate() as Area2D
get_tree().root.add_child(bullet)
# Calculate index relative to center (0 = center, -1/+1 = first from center)
var index_from_center: float = b - center_index
# Standard projectile logic (backward compatible)
func _shoot_standard(weapon_data: WeaponShot, current_time: float):
var total_projectiles = weapon_data.projectiles
var center_index = (total_projectiles - 1) / 2.0
# Calculate timing offset for staggered firing (center-first, pairs outward)
var time_offset: float = abs(index_from_center) * weapon_data.stagger_offset
if total_projectiles > 0:
for b in range(total_projectiles):
var bullet := weapon_data.bullet_scene.instantiate() as Area2D
get_tree().root.add_child(bullet)
# Calculate horizontal offset for this bullet (symmetrical: left=-ve, right=+ve)
var bullet_horizontal_offset: float = index_from_center * weapon_data.horizontal_offset
# Calculate index relative to center (0 = center, -1/+1 = first from center)
var index_from_center: float = b - center_index
# 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
# Calculate timing offset for staggered firing (center-first, pairs outward)
var time_offset: float = abs(index_from_center) * weapon_data.stagger_offset
# Final position combines symmetrical horizontal spread with symmetrical vertical spacing
bullet.position = player.position + Vector2(bullet_horizontal_offset, weapon_data.origin + (vertical_offset + weapon_data.origin - total_projectiles))
# Calculate horizontal offset for this bullet (symmetrical: left=-ve, right=+ve)
var bullet_horizontal_offset: float = index_from_center * weapon_data.horizontal_offset
# Calculate angle for this bullet (only when projectiles >= 3)
var bullet_angle: float = 0.0
if total_projectiles >= 3:
bullet_angle = (index_from_center / center_index) * (weapon_data.spread_angle / 2.0)
# 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
# Set timing properties on the bullet
bullet.time_offset = time_offset
bullet.fire_time = current_time + time_offset
bullet.angle = bullet_angle
# Final position combines symmetrical horizontal spread with symmetrical vertical spacing
bullet.position = player.position + Vector2(bullet_horizontal_offset, weapon_data.origin + (vertical_offset + weapon_data.origin - total_projectiles))
# Set the bullet data
bullet.set_weapon_data(weapon_data.bullet_scene, weapon_data)
# Calculate angle for this bullet (only when projectiles >= 3)
var bullet_angle: float = 0.0
if total_projectiles >= 3:
bullet_angle = (index_from_center / center_index) * (weapon_data.spread_angle / 2.0)
# Offset animation playback per projectile if enabled
if weapon_data.stagger_animation:
bullet.set_animation_offset(index_from_center * weapon_data.stagger_offset)
# Set timing properties on the bullet
bullet.time_offset = time_offset
bullet.fire_time = current_time + time_offset
bullet.angle = bullet_angle
# Reset travel after firing to prevent multiple bursts per tap
player.travel = 0.0
# Set the bullet data
bullet.set_weapon_data(weapon_data.bullet_scene, weapon_data)
# Offset animation playback per projectile if enabled
if weapon_data.stagger_animation:
bullet.set_animation_offset(index_from_center * weapon_data.stagger_offset)
# Grouped projectile logic (new functionality)
func _shoot_grouped(weapon_data: WeaponShot, current_time: float):
for group: WeaponProjectileGroup in weapon_data.groups.filter(func(g): return g != null):
for b in range(group.bullet_count):
var bullet := weapon_data.bullet_scene.instantiate() as Area2D
get_tree().root.add_child(bullet)
_fire_grouped_bullet(bullet, group, b, current_time, weapon_data, false)
# If mirroring is enabled, fire a mirrored copy
if group.mirror:
var mirror_bullet := weapon_data.bullet_scene.instantiate() as Area2D
get_tree().root.add_child(mirror_bullet)
_fire_grouped_bullet(mirror_bullet, group, b, current_time, weapon_data, true)
# Fire a single grouped bullet (original or mirrored)
func _fire_grouped_bullet(bullet: Area2D, group: WeaponProjectileGroup, b: int,
current_time: float, weapon_data: WeaponShot, mirror: bool):
# Calculate index relative to this group's center
var center_index: float = (group.bullet_count - 1) / 2.0
var index_from_center: float = b - center_index
# Horizontal offset: group offset + within-group spread
# Mirror: negate the horizontal offset contribution
var bullet_horizontal_offset: float = group.horizontal_offset + index_from_center * group.horizontal_spacing
if mirror:
bullet_horizontal_offset = -bullet_horizontal_offset
# Base angle for the group, plus within-group spread
# Mirror: negate the angle and the spread contribution
var bullet_angle: float = group.angle_offset
if mirror:
bullet_angle = -group.angle_offset
if group.bullet_count >= 3:
var spread_contribution = (index_from_center / center_index) * (group.spread_angle / 2.0)
if mirror:
bullet_angle -= spread_contribution
else:
bullet_angle += spread_contribution
# Position: apply horizontal offset and vertical origin from weapon data
bullet.position = player.position + Vector2(bullet_horizontal_offset, weapon_data.origin)
# Timing: group delay + within-group stagger
var time_offset: float = group.group_delay + abs(index_from_center) * group.stagger_offset
# Set timing properties on the bullet
bullet.time_offset = time_offset
bullet.fire_time = current_time + time_offset
bullet.angle = bullet_angle
# Set the bullet data
bullet.set_weapon_data(weapon_data.bullet_scene, weapon_data)
# Offset animation playback per projectile if enabled
if weapon_data.stagger_animation:
bullet.set_animation_offset(index_from_center * weapon_data.stagger_offset)