class_name Projectile
extends Area3D

const HEIGHT: float = 1

@export var _collision_debug_material: Material

var _start_position: Vector3
var _velocity: Vector3
var _lifetime: float

var _life_timer: float

var _debug_collision_shapes := DebugCollisionShapes.new()


func _ready() -> void:
	_life_timer = _lifetime
	_debug_collision_shapes.init(get_children(), self, _collision_debug_material)
	global_position = _start_position
	global_position.y = HEIGHT
	body_entered.connect(_on_body_entered)


func _physics_process(delta: float) -> void:
	if _life_timer <= 0:
		queue_free()

	_life_timer -= delta
	global_position += _velocity * delta


func init(velocity: Vector3, start_position: Vector3, lifetime: float = 10) -> void:
	_velocity = velocity
	_start_position = start_position
	_lifetime = lifetime


func set_velocity(velocity: Vector3) -> void:
	_velocity = velocity
	_life_timer = _lifetime


func _on_body_entered(node: Node3D) -> void:
	if node is not Player:
		return

	queue_free()
