extends Node2D

# Smoke Puff Effect - Plays animation and fades out while moving slowly

@export var animation_speed: float = 10.0
@export var fade_duration: float = 0.5
@export var move_speed: float = 15.0  # Pixels per second movement speed
@export var move_duration: float = 1.5  # How long to move before starting fade

@onready var sprite: Sprite2D = $Sprite2D

var current_frame: int = 0
var frame_timer: float = 0.0
var total_frames: int = 4  # 4 frames per row
var puff_type: int = 0  # 0 or 1 for first or second row
var move_direction: Vector2 = Vector2.ZERO  # Direction to move in

func _ready():
	# Add to group for easy cleanup
	add_to_group("smoke_puff")
	
	# Wait for sprite to be ready (ensure @onready variable is set)
	await get_tree().process_frame
	
	# Verify sprite exists
	if not sprite:
		push_error("SmokePuff: ERROR - Sprite2D not found! Check that scene has Sprite2D child node.")
		queue_free()
		return
	
	# Randomly choose puff type
	puff_type = randi() % 2
	
	# Randomly choose movement direction (random angle, slow movement)
	var random_angle = randf() * TAU  # 0 to 2*PI
	move_direction = Vector2(cos(random_angle), sin(random_angle))
	
	# Set initial frame
	sprite.frame = puff_type * total_frames
	current_frame = 0
	
	print("SmokePuff: Starting animation, sprite: ", sprite, ", frame: ", sprite.frame, ", move_direction: ", move_direction)
	
	# Start animation
	animate_puff()

func animate_puff():
	# Verify sprite still exists
	if not sprite:
		push_error("SmokePuff: ERROR - Sprite is null during animation!")
		queue_free()
		return
	
	# Calculate frame animation timing
	var frame_interval = 1.0 / animation_speed  # Time per frame
	var frame_animation_duration = float(total_frames) * frame_interval
	
	# Set initial frame
	sprite.frame = puff_type * total_frames
	current_frame = 0
	frame_timer = 0.0
	
	# Start movement tween
	var move_distance = move_speed * move_duration
	var target_position = global_position + move_direction * move_distance
	var move_tween = create_tween()
	if move_tween:
		move_tween.tween_property(self, "global_position", target_position, move_duration)
	
	# After animation completes, fade out and remove
	var total_animation_time = max(frame_animation_duration, move_duration)
	await get_tree().create_timer(total_animation_time).timeout
	
	# Fade out
	if sprite:
		print("SmokePuff: Starting fade out...")
		var fade_tween = create_tween()
		if fade_tween:
			fade_tween.tween_property(sprite, "modulate:a", 0.0, fade_duration)
			await fade_tween.finished
		
		print("SmokePuff: Animation complete, removing...")
		queue_free()

func _process(delta):
	# Handle frame animation in _process() for more reliable timing
	if not sprite:
		return
	
	# Update frame animation
	if current_frame < total_frames:
		frame_timer += delta
		var frame_interval = 1.0 / animation_speed
		
		if frame_timer >= frame_interval:
			frame_timer = 0.0
			current_frame += 1
			
			if current_frame < total_frames:
				sprite.frame = puff_type * total_frames + current_frame
				print("SmokePuff: Frame ", current_frame, " -> ", sprite.frame)
