extends Node3D

@export var beam_width: float = 0.05
@export var beam_range: float = 100.0

@onready var beam: MeshInstance3D = $BeamMesh
@onready var raycast: RayCast3D = $RayCast3D

var active: bool = true

func _ready() -> void:
	raycast.target_position = raycast.target_position.normalized() * beam_range

func _process(_delta):
	if not active:
		return

	if not raycast.is_colliding():
		return

	# update the beam
	var direction = raycast.global_position - raycast.get_collision_point()
	var midpoint = (raycast.global_position + raycast.get_collision_point()) / 2.0

	beam.scale = Vector3(beam_width, direction.length() / 2, beam_width)
	beam.global_position = midpoint

	# --------------------
	# TODO  8
	# Player death
	# --------------------
	

func activate():
	active = true

func deactivate():
	active = false
	beam.scale = Vector3.ZERO
