diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2021-12-09 00:52:59 +0100 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2021-12-09 00:52:59 +0100 |
commit | 0f518727c28d3204415db14c7ca0e4f7cb653677 (patch) | |
tree | a595121771b69a52f17154d8e5c61b7e4b2b8190 /scripts/path.gd |
working
Diffstat (limited to 'scripts/path.gd')
-rw-r--r-- | scripts/path.gd | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/scripts/path.gd b/scripts/path.gd new file mode 100644 index 0000000..bd3b40f --- /dev/null +++ b/scripts/path.gd @@ -0,0 +1,72 @@ +extends Node + +const dirs = [ + Vector3.UP, Vector3.DOWN, + Vector3.FORWARD, Vector3.BACK, + Vector3.LEFT, Vector3.RIGHT +] + +var nodes = [] + + +func _ready(): + pass # Replace with function body. + +func next_node (node, dir): + var pos : Vector3 = node.transform.origin + var rot : Quat = node.transform.basis.get_rotation_quat() + var forward : Vector3 = rot * dir + var probe : Vector3 = pos + forward * 0.5 + + for child in get_children(): + var diff : Vector3 = child.transform.origin - probe + var dist : float = diff.length_squared() + if dist < 0.1: + return child + + return null + +func load_nodes (): + var start = null + for child in get_children(): + if "start" in child.name: + start = child + if start == null: + return "failed to find start" + + var next = null + for dir in dirs: + var cand = next_node(start, dir) + if cand != null: + if next == null: + next = cand + else: + return "failed: more than one path from start" + if next == null: + return "failed to find path next to start" + + nodes = [start, next] + var iter = next + while true: + iter = next_node(iter, Vector3.UP) + nodes.append(iter) + if iter == null: + return "failed to follow path" + if "end" in iter.name: + break + + return "ok" + +func hide (): + for child in get_children(): + var mesh = child.find_node("MeshInstance") + mesh.visible = false + +func show (): + for child in get_children(): + var mesh = child.find_node("MeshInstance") + mesh.visible = false + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass |