1
0
Fork 0
spacetac/core/tools.gd

20 lines
710 B
GDScript

class_name Tools
# Get the nearest node from a list, satisfying a predicate
static func get_nearest(from: Node2D, nodes: Array[Node], predicate: Callable) -> Node2D:
var from_position := from.global_position
var nearest: Node2D = null
var distance := INF
for node in nodes:
if predicate.call(node):
var d := from_position.distance_to(node.global_position)
if d < distance:
distance = d
nearest = node
return nearest
# Get a random position around a fixed position, in a given radius
static func jitter(pos: Vector2, min_dist = 0.0, max_dist = 100.0) -> Vector2:
# TODO uniform distribution
return pos + Vector2.from_angle(randf_range(0.0, PI * 2.0)) * randf_range(min_dist, max_dist)