aboutsummaryrefslogtreecommitdiff
path: root/scripts/path.gd
blob: 3774bfed59ff6e052422817e7eeb32664c1ced8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
extends Node

const dirs = [ 
	Vector3.UP, Vector3.DOWN, 
	Vector3.FORWARD, Vector3.BACK, 
	Vector3.LEFT, Vector3.RIGHT 
]

var nodes = []


func refresh_path (vis):
	if vis:
		show()
	else:
		var _res = load_nodes()
		hide()

	
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.get_node("MeshInstance")
		mesh.visible = false
		
func show ():
	for child in get_children():
		var mesh = child.get_node("MeshInstance")
		mesh.visible = true

# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass