hadean-godot/Scripts/Util.gd

43 lines
926 B
GDScript
Raw Normal View History

2024-05-13 18:16:23 -04:00
@tool
extends Node
2025-10-10 03:01:08 -04:00
# probably move this to some sort of settings at some point
var animation_speed = 0.8
func find(type: Script):
2025-09-24 20:01:45 -04:00
var root = null
if Engine.is_editor_hint():
root = get_tree().edited_scene_root
else:
root = get_tree().root
2025-10-10 03:01:08 -04:00
return find_child_type(root, type)
2024-05-13 18:16:23 -04:00
2025-09-24 20:01:45 -04:00
func find_parent_type(node: Node, type: Script):
var current_node = node
if node == null:
return null
var i = 0
while current_node != null and i < 10:
i += 1
if is_instance_of(current_node, type):
return current_node
current_node = current_node.get_parent()
return null;
2024-05-13 18:16:23 -04:00
2025-10-10 03:01:08 -04:00
func find_child_type(base: Node, type: Script):
2025-09-24 20:01:45 -04:00
if is_instance_of(base, type):
return base
for child in base.get_children():
2025-10-10 03:01:08 -04:00
var found_node = find_child_type(child, type)
2025-09-24 20:01:45 -04:00
if found_node != null:
return found_node
return null
func center_rect(rect: Rect2) -> Rect2:
return Rect2(rect.size / -2, rect.size)
2024-05-13 18:16:23 -04:00
func is_debug():
2025-09-24 20:01:45 -04:00
return true