class_name WideSpray extends Spray
## Wide spray pattern

const SPRAYCAST_GROUP := "SprayCast"

@export var spray_scale := 1.0
# NOTE: this is damage _per spray_, so multiply by up to 7 damage per frame depending on spread.
@export var damage := 0.015

var _horizontal := true
var _busy := false

@onready var spray_casts: Node3D = %SprayCasts
@onready var animation_player: AnimationPlayer = %AnimationPlayer
@onready var spray_sfx: SpraySFX = %SpraySFX


func switch_mode() -> void:
	if not _busy:
		if _horizontal:
			animation_player.play("rotate")
		else:
			animation_player.play_backwards("rotate")
		_horizontal = not _horizontal
		_busy = true
	else:
		# TODO: bonk
		pass


func _fire() -> void:
	if _busy:
		idle()
	else:
		super._fire()


func _spray() -> void:
	var prev_target: GunkBody = null
	var prev_point: Vector3
	var prev_normal: Vector3

	for laser: LaserCast in spray_casts.get_children():
		if laser.is_colliding():
			spray_sfx.activate()

			var collider := laser.get_collider()
			if collider is GunkBody:
				var target := collider as GunkBody
				var point := laser.get_collision_point()
				var normal := laser.get_collision_normal()

				# Always paint at least a dot, to cap the ends of the line
				target.paint_dot(point, normal, spray_scale)
				if target == prev_target:
					# Continue the multiline if possible
					target.add_to_multiline(prev_point, prev_normal, point, normal, spray_scale * 2)
					prev_point = point
					prev_normal = normal
				prev_target = target
				prev_point = point
				prev_normal = normal
			elif collider is GunkNode:
				(collider as GunkNode).hit(damage)


func _on_animation_finished(anim_name: StringName) -> void:
	if anim_name == "rotate":
		_busy = false
