-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperators.gd
More file actions
63 lines (39 loc) · 1.14 KB
/
Copy pathOperators.gd
File metadata and controls
63 lines (39 loc) · 1.14 KB
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
extends "res://Scripts/PlayerDetection.gd"
var motion = Vector2()
var possible_destinations = []
var path = []
var destination = Vector2()
export var walk_slowdown = 0.5
export var nav_stop_threshold = 5
onready var navigation = Global.citynavigation
onready var available_destinations = Global.citydestinations
func _ready():
possible_destinations = available_destinations.get_children()
make_path()
func _process(delta):
navigate()
func navigate():
var distance_to_destination = position.distance_to(path[0])
destination = path[0]
if distance_to_destination > nav_stop_threshold:
move()
else:
update_path()
func move():
look_at(destination)
motion = (destination-position).normalized() * (MAX_SPEED * walk_slowdown)
if is_on_wall():
make_path()
move_and_slide(motion)
func make_path():
randomize()
var next_destination = possible_destinations[randi() % possible_destinations.size()]
path = navigation.get_simple_path(global_position, next_destination.global_position, false)
func update_path():
if path.size() == 1:
if $Timer.is_stopped():
$Timer.start()
else:
path.remove(0)
func _on_Timer_timeout():
make_path()