41 lines
871 B
GDScript
41 lines
871 B
GDScript
extends ProjectileBase
|
|
|
|
var enemies_hit = 0
|
|
|
|
|
|
func _ready() -> void:
|
|
speed = 500
|
|
super()
|
|
var first = get_nearest_enemy(global_position)
|
|
if first == null:
|
|
queue_free()
|
|
return
|
|
launch(first.global_position)
|
|
|
|
func _on_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("enemies"):
|
|
enemies_hit += 1
|
|
body.hit()
|
|
if enemies_hit == 20:
|
|
queue_free()
|
|
else:
|
|
var next = get_nearest_enemy(global_position, body)
|
|
if next == null:
|
|
queue_free()
|
|
else:
|
|
launch(next.global_position)
|
|
|
|
func get_nearest_enemy(from: Vector2, exclude: Node = null) -> Node:
|
|
var nearest = null
|
|
var min_distance = INF
|
|
for enemy in get_tree().get_nodes_in_group("enemies"):
|
|
if enemy == exclude or enemy.is_dying:
|
|
continue
|
|
var dist = from.distance_to(enemy.global_position)
|
|
if dist < min_distance:
|
|
min_distance = dist
|
|
nearest = enemy
|
|
return nearest
|
|
|
|
|