facsimile-wing/scenes/player_weapons/weapon_stock.gd

61 lines
2.1 KiB
GDScript

extends Area2D
# Data to be set externally when this bullet is instantiated
var bullet_scene: PackedScene = null
var shot_data: WeaponShot = null
# Time offset when this bullet was fired (relative to other bullets in the same shot)
var time_offset: float = 0.5
# When this bullet should fire (absolute time)
var fire_time: float = 0.0
# Track if this bullet is currently active in the shot
var is_active: bool = false
func _process(delta):
var current_time = Time.get_ticks_msec() / 1000.0
# Fire if it's time and bullet isn't already active
if !is_active and current_time >= fire_time:
activate()
# Move the bullet
position.y -= shot_data.speed * delta
func activate():
is_active = true
visible = true
# # Emit signal for hit detection
# emit_signal("activate")
func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()
# Set fire_time for the next shot (staggered firing)
var current_time = Time.get_ticks_msec() / 1000.0
fire_time = current_time + shot_data.stagger_offset
# Function to set the bullet data (called from parent scene)
func set_weapon_data(bullet_scene_param: PackedScene, shot_data_param: WeaponShot):
bullet_scene = bullet_scene_param
shot_data = shot_data_param
# Function to offset animation playback for staggered visual effect
func set_animation_offset(offset: float) -> void:
var sprite = $AnimatedSprite2D
if sprite:
var frames = sprite.sprite_frames
if frames:
var animation = sprite.animation
var frame_count = frames.get_frame_count(animation)
if frame_count > 0:
# Calculate average frame duration from actual frame timings
var total_duration = 0.0
for i in range(frame_count):
total_duration += frames.get_frame_duration(animation, i)
var avg_frame_duration = total_duration / frame_count
# Convert time offset to frame offset, wrap within animation length
var frame_offset = fposmod(offset / avg_frame_duration, float(frame_count))
sprite.frame = int(frame_offset)