Implemented dynamic projectile spread.
This commit is contained in:
parent
029175ccde
commit
a9af69e7bb
5 changed files with 146 additions and 72 deletions
83
Scratch
Normal file
83
Scratch
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
@export_category("Stagger")
|
||||
@export var horizontal_offset: float = 6.5 # Horizontal distance between projectiles
|
||||
@export var stagger_offset: float = 5 # Time delay per projectile index
|
||||
@export var firing_angle: float = 0.0 # Angle in degrees (0 = straight up, positive = angled right)
|
||||
@export var num_groups: int = 1 # Number of symmetrical groups to fire (1 = single direction, 2 = left/right, 3 = left/center/right)
|
||||
|
||||
|
||||
---
|
||||
|
||||
func shoot():
|
||||
var weapon_data = weapon_component.data # WeaponShot resource configuration
|
||||
var current_time = Time.get_ticks_msec() / 1000.0
|
||||
var total_projectiles = weapon_data.projectiles
|
||||
|
||||
if player.travel > weapon_data.spacing:
|
||||
|
||||
# Calculate center index for symmetrical staggering
|
||||
var center_index = (total_projectiles - 1) / 2.0
|
||||
|
||||
if total_projectiles > 0:
|
||||
for group in range(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
|
||||
|
||||
if num_groups == 1:
|
||||
group_start = 0
|
||||
group_end = total_projectiles
|
||||
elif num_groups == 2:
|
||||
group_start = 0
|
||||
group_end = total_projectiles / 2 if total_projectiles % 2 == 0 else (total_projectiles - 1) / 2
|
||||
elif num_groups == 3:
|
||||
var third = total_projectiles / 3
|
||||
group_start = 0
|
||||
group_end = third if third > 0 else 1
|
||||
else:
|
||||
continue # Skip if num_groups > 3
|
||||
|
||||
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 = -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
|
||||
|
|
@ -8,9 +8,10 @@ script = ExtResource("2_by0nb")
|
|||
shot_name = "Stock Shot"
|
||||
bullet_scene = ExtResource("1_by0nb")
|
||||
speed = 500
|
||||
projectiles = 3
|
||||
spacing = 25.0
|
||||
origin = -12
|
||||
horizontal_offset = 8.0
|
||||
stagger_offset = 0.35
|
||||
projectiles = 5
|
||||
spacing = 20.0
|
||||
origin = -25
|
||||
horizontal_offset = 6.0
|
||||
stagger_offset = 5.5
|
||||
angle = 2.0
|
||||
metadata/_custom_type_script = "uid://7n1itonn35fm"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
class_name WeaponShot
|
||||
extends Resource
|
||||
class_name WeaponShot extends Resource
|
||||
|
||||
@export_category("Info")
|
||||
@export var shot_name: String
|
||||
|
|
@ -15,5 +14,6 @@ extends Resource
|
|||
@export_category("Stagger")
|
||||
@export var horizontal_offset: float = 6.5 # Horizontal distance between projectiles
|
||||
@export var stagger_offset: float = 5 # Time delay per projectile index
|
||||
@export var firing_angle: float = 0.0 # Angle in degrees (0 = straight up, positive = angled right)
|
||||
@export var num_groups: int = 1 # Number of symmetrical groups to fire (1 = single direction, 2 = left/right, 3 = left/center/right)
|
||||
|
||||
# Angle in degrees, default is 0
|
||||
@export var angle: float = 0.0 # Angle at which the bullet should be fired
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
extends Area2D
|
||||
|
||||
|
||||
@onready var bullet: PackedScene = load("res://scenes/player_weapons/weapon_stock.tscn")
|
||||
@onready var shot_data: = load("res://resources/player_weapon_resources/weapon_shot_stock.tres")
|
||||
|
||||
|
||||
|
||||
# Time offset when this bullet was fired (relative to other bullets in the same shot)
|
||||
var time_offset: float = 0.5
|
||||
|
||||
|
|
@ -15,6 +13,9 @@ var fire_time: float = 0.0
|
|||
# Track if this bullet is currently active in the shot
|
||||
var is_active: bool = false
|
||||
|
||||
# Angle at which this bullet should fire (in degrees)
|
||||
var angle: float = 0.0
|
||||
|
||||
func _process(delta):
|
||||
var current_time = Time.get_ticks_msec() / 1000.0
|
||||
|
||||
|
|
@ -22,14 +23,20 @@ func _process(delta):
|
|||
if !is_active and current_time >= fire_time:
|
||||
activate()
|
||||
|
||||
# Calculate the movement vector based on speed, angle, and delta
|
||||
var angle_rad = deg_to_rad(angle)
|
||||
var move_x = cos(angle_rad) * shot_data.speed * delta
|
||||
var move_y = sin(angle_rad) * shot_data.speed * delta
|
||||
|
||||
# Move the bullet
|
||||
position.y -= shot_data.speed * delta
|
||||
position.x += move_x
|
||||
position.y += move_y
|
||||
|
||||
func activate():
|
||||
is_active = true
|
||||
visible = true
|
||||
|
||||
# # Emit signal for hit detection
|
||||
# Emit signal for hit detection (optional)
|
||||
# emit_signal("activate")
|
||||
|
||||
func _on_visible_on_screen_notifier_2d_screen_exited():
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
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
|
||||
else:
|
||||
continue # Skip if weapon_data.num_groups > 3
|
||||
|
||||
for b in range(group_start, group_end):
|
||||
for b in range(total_projectiles):
|
||||
var bullet_scene = weapon_data.bullet_scene
|
||||
var bullet := bullet_scene.instantiate() as Node
|
||||
var bullet := bullet_scene.instantiate() as Node2D
|
||||
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 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 = abs(index_from_center) * stagger_offset
|
||||
var time_offset: float = index_from_center * weapon_data.stagger_offset
|
||||
|
||||
# Calculate horizontal offset for this bullet (symmetrical within group)
|
||||
# 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 within group)
|
||||
# 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 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
|
||||
# Final position combines symmetrical horizontal spread with fixed vertical offset from weapon origin
|
||||
bullet.position = player.position + Vector2(bullet_horizontal_offset, weapon_data.origin)
|
||||
|
||||
# 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
|
||||
|
||||
# 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:
|
||||
# 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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Subtract projectile spacing from current Player travel for next shot
|
||||
player.travel -= weapon_data.spacing
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue