Separated shot positioning math to helper utility and refactored shoot
component - introduced shot origin issue.
This commit is contained in:
parent
6c5a0a0bae
commit
99a5d9838d
17 changed files with 147 additions and 98 deletions
|
|
@ -2,12 +2,14 @@ class_name ShootComponent extends Node
|
|||
|
||||
@onready var weapon_component: Node = %WeaponComponent
|
||||
@onready var player = $".."
|
||||
@onready var ship: Sprite2D = get_node("../Ship")
|
||||
@onready var fire_helper: WeaponFireHelper = %WeaponFireHelper
|
||||
|
||||
func shoot():
|
||||
var weapon_data = weapon_component.weapon_data
|
||||
var current_time = Time.get_ticks_msec() / 1000.0
|
||||
|
||||
if player.travel > weapon_data.spacing:
|
||||
if player.travel > weapon_data.projectile_vertical_spacing:
|
||||
_shoot_standard(weapon_data, current_time)
|
||||
_shoot_grouped(weapon_data, current_time)
|
||||
|
||||
|
|
@ -15,105 +17,61 @@ func shoot():
|
|||
player.travel = 0.0
|
||||
|
||||
|
||||
# Standard projectile logic (backward compatible)
|
||||
# Standard projectile logic (delegates to WeaponFireHelper)
|
||||
func _shoot_standard(weapon_data: WeaponShot, current_time: float):
|
||||
var total_projectiles = weapon_data.projectiles
|
||||
var center_index = (total_projectiles - 1) / 2.0
|
||||
var total_projectiles: int = weapon_data.projectiles
|
||||
|
||||
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
|
||||
|
||||
# Calculate timing offset for staggered firing (center-first, pairs outward)
|
||||
var time_offset: float = abs(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 symmetrical vertical spacing
|
||||
bullet.position = player.position + Vector2(bullet_horizontal_offset, weapon_data.origin + (vertical_offset + weapon_data.origin - total_projectiles))
|
||||
|
||||
# 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)
|
||||
|
||||
# Set timing properties on the bullet
|
||||
bullet.time_offset = time_offset
|
||||
bullet.fire_time = current_time + time_offset
|
||||
bullet.angle = bullet_angle
|
||||
# Delegate math to the helper, with spawn position offset by sprite height and weapon origin
|
||||
var spawn_position = ship.global_position + Vector2(0, ship.get_texture().get_height() / 2.0) + weapon_data.origin
|
||||
var props = fire_helper.compute_standard_props(weapon_data, b, spawn_position, current_time)
|
||||
bullet.position = props.position
|
||||
bullet.angle = props.angle
|
||||
bullet.time_offset = props.time_offset
|
||||
bullet.fire_time = props.fire_time
|
||||
|
||||
# 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)
|
||||
bullet.set_animation_offset(props.animation_offset)
|
||||
|
||||
|
||||
# Grouped projectile logic (new functionality)
|
||||
# Grouped projectile logic (delegates to WeaponFireHelper)
|
||||
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)
|
||||
# Apply computed properties for the original bullet
|
||||
var spawn_position = ship.global_position + Vector2(0, ship.get_texture().get_height() / 2.0) + weapon_data.origin
|
||||
var props = fire_helper.compute_grouped_props(group, weapon_data, b, spawn_position, current_time, false)
|
||||
bullet.position = props.position
|
||||
bullet.angle = props.angle
|
||||
bullet.time_offset = props.time_offset
|
||||
bullet.fire_time = props.fire_time
|
||||
bullet.set_weapon_data(weapon_data.bullet_scene, weapon_data)
|
||||
if weapon_data.stagger_animation:
|
||||
bullet.set_animation_offset(props.animation_offset)
|
||||
|
||||
# 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)
|
||||
# Apply computed properties for the mirrored bullet
|
||||
var mirror_spawn = ship.global_position + Vector2(0, ship.get_texture().get_height() / 2.0) + weapon_data.origin
|
||||
var mirror_props = fire_helper.compute_grouped_props(group, weapon_data, b, mirror_spawn, current_time, true)
|
||||
mirror_bullet.position = mirror_props.position
|
||||
mirror_bullet.angle = mirror_props.angle
|
||||
mirror_bullet.time_offset = mirror_props.time_offset
|
||||
mirror_bullet.fire_time = mirror_props.fire_time
|
||||
mirror_bullet.set_weapon_data(weapon_data.bullet_scene, weapon_data)
|
||||
if weapon_data.stagger_animation:
|
||||
mirror_bullet.set_animation_offset(mirror_props.animation_offset)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue