extends Node2D

var direction := Vector2.ZERO # Default direction
var fade_delay := 0.14 # When to start fading (mid-move)
var move_duration := 0.2 # Slash exists for 0.3 seconds
var fade_duration := 0.06 # Time to fade out
var stretch_amount := Vector2(1, 1.4) # How much to stretch the sprite
var slash_amount = 8
var initiated_by: Node2D = null

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	call_deferred("_initialize_thrust")
	pass # Replace with function body.
	
func _initialize_thrust():
	var tween = create_tween()
	var move_target = global_position + (direction.normalized() * slash_amount) # Moves in given direction
	tween.set_trans(Tween.TRANS_CUBIC) # Smooth acceleration & deceleration
	tween.set_ease(Tween.EASE_OUT) # Fast start, then slows down
	tween.tween_property(self, "global_position", move_target, move_duration)
	'	
	# Create stretch tween (grow and shrink slightly)
	var stretch_tween = create_tween()
	stretch_tween.set_trans(Tween.TRANS_CUBIC)
	stretch_tween.set_ease(Tween.EASE_OUT)
	stretch_tween.tween_property($Sprite2D, "scale", Vector2.ONE, move_duration / 2)  # start normal
	stretch_tween.tween_property($Sprite2D, "scale", stretch_amount, move_duration / 2)
	'
	
	# Wait until mid-move to start fade
	await get_tree().create_timer(fade_delay).timeout
	
	# Start fade-out effect
	var fade_tween = create_tween()
	fade_tween.tween_property($Sprite2D, "modulate:a", 0.0, fade_duration) # Fade to transparent
	await fade_tween.finished
	queue_free()
	pass

func _on_damage_area_body_entered(body: Node2D) -> void:
	if body.get_parent() == initiated_by or body == initiated_by:
		return
	if body.get_parent() is CharacterBody2D and body.get_parent().stats.is_invulnerable == false and body.get_parent().stats.hp > 0: # hit an enemy
		$MeleeImpact.play()
		body.take_damage(self, initiated_by)
		pass
	else:
		$MeleeImpactWall.play()
		pass
	pass # Replace with function body.


func _on_damage_area_area_entered(body: Area2D) -> void:
	if body.get_parent() == initiated_by:
		return
	if body.get_parent() is CharacterBody2D and body.get_parent().stats.is_invulnerable == false and body.get_parent().stats.hp > 0: # hit an enemy
		$MeleeImpact.play()
		body.get_parent().take_damage(self, initiated_by)
		pass
	else:
		$MeleeImpactWall.play()
		pass
	pass # Replace with function body.
