extends CharacterBody2D
class_name Bullet

var base_speed = 2000.0
var speed_bound = 1000.0
var speed = base_speed
var target_node: Node2D = null
var damage = 0
var timeout: Timer = Timer.new()
var last_vector: Vector2 = Vector2(0, 0)

func get_class():
    return "Bullet"
    
func _ready():
    timeout.set_wait_time(10.0)
    add_child(timeout)
    timeout.start()
    timeout.connect("timeout", _on_timeout)
    speed = base_speed

func _process(delta: float):
    if target_node != null:
        if target_node.dead:
            target_node = null
        else:
            look_at(target_node.get_global_position())

func _on_timeout():
    get_parent().remove_child(self)
    queue_free()

func _physics_process(delta):
    if target_node != null and speed > speed_bound + 1.0:
        last_vector = Vector2(target_node.get_global_position() - get_global_position()).normalized()
        velocity = last_vector * speed
    else:
        velocity = last_vector * speed
    speed -= delta * base_speed
    speed = max(speed, speed_bound)
    move_and_slide()
