-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.gd
46 lines (26 loc) · 947 Bytes
/
navigation.gd
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
extends Navigation2D
export (float) var CHARACTER_SPEED = 400.0
var path = []
func _input(event):
if not event.is_action_pressed("click"):
return
_update_navigation_path($Character.position, get_local_mouse_position())
func _update_navigation_path(start_position, end_position):
path = get_simple_path(start_position, end_position, true)
path.remove(0)
set_process(true)
func _process(delta):
var walk_distance = CHARACTER_SPEED * delta
move_along_path(walk_distance)
func move_along_path(distance):
var last_point = $Character.position
while path.size():
var distance_between_points = last_point.distance_to(path[0])
if distance <= distance_between_points:
$Character.position = last_point.linear_interpolate(path[0], distance / distance_between_points)
return
distance -= distance_between_points
last_point = path[0]
path.remove(0)
$Character.position = last_point
set_process(false)