45 lines
1.2 KiB
GDScript
45 lines
1.2 KiB
GDScript
extends Node2D
|
|
class_name Radar
|
|
|
|
@export var threat_packed: PackedScene
|
|
|
|
func declare_threat(node: Node2D) -> void:
|
|
var instance: Threat = threat_packed.instantiate()
|
|
instance.node = node
|
|
add_child(instance)
|
|
|
|
func release_threat(node: Node2D) -> void:
|
|
for i: int in get_child_count():
|
|
var threat: Threat = get_child(i)
|
|
if threat.node == node:
|
|
remove_child(threat)
|
|
return
|
|
|
|
func declare_danger(node: Node2D) -> void:
|
|
for i: int in get_child_count():
|
|
var threat: Threat = get_child(i)
|
|
if threat.node == node:
|
|
threat.declare_danger()
|
|
func release_danger(node: Node2D) -> void:
|
|
for i: int in get_child_count():
|
|
var threat: Threat = get_child(i)
|
|
if threat.node == node:
|
|
threat.release_danger()
|
|
|
|
func _process(_delta: float) -> void:
|
|
global_rotation = 0.0
|
|
|
|
|
|
# returns 100 if there is no threat, else -PI to PI
|
|
func get_closest_threat_angle(angle: float) -> float:
|
|
var min_diff: float = 100
|
|
for i: int in get_child_count():
|
|
var threat: Threat = get_child(i)
|
|
var a := global_position.angle_to_point(threat.node.global_position) - angle + PI / 2
|
|
while a > PI:
|
|
a -= 2 * PI
|
|
while a < -PI:
|
|
a += 2 * PI
|
|
if abs(a) < abs(min_diff):
|
|
min_diff = a
|
|
return min_diff
|