@@ -51,8 +51,7 @@ const DEFAULT_SPRITE_FRAMES = preload("uid://ovu5wqo15s5g")
5151@export_tool_button ("Add/Edit Patrol Path" ) var _edit_patrol_path : Callable = edit_patrol_path
5252## The path the guard follows while patrolling.
5353@export var patrol_path : Path2D :
54- set (new_value ):
55- patrol_path = new_value
54+ set = _set_patrol_path
5655
5756## The wait time at each patrol point.
5857@export_range (0 , 5 , 0.1 , "or_greater" , "suffix:s" ) var wait_time : float = 1.0 :
@@ -108,6 +107,9 @@ var _player: Node2D
108107## Control to hold debug info that can be toggled on or off.
109108@onready var debug_info : Label = % DebugInfo
110109
110+ ## Behavior to use when patrolling.
111+ @onready var patrolling_behavior : PointsWalkBehavior = % PatrollingBehavior
112+
111113## Reference to the node controlling the AnimationPlayer for walking / being idle,
112114## so it can be disabled to play the alerted animation.
113115@onready
@@ -149,6 +151,8 @@ func _ready() -> void:
149151 player_awareness .max_value = time_to_detect_player
150152 player_awareness .value = 0.0
151153
154+ patrolling_behavior .speeds .walk_speed = move_speed
155+ _set_patrol_path (patrol_path )
152156 _set_sprite_frames (sprite_frames )
153157
154158 if detection_area :
@@ -163,8 +167,9 @@ func _ready() -> void:
163167 guard_movement .path_blocked .connect (self ._on_path_blocked )
164168
165169 # Wait 2 frames before starting to match backwards compatibility.
166- await get_tree ().process_frame
167- await get_tree ().process_frame
170+ if not Engine .is_editor_hint ():
171+ await get_tree ().process_frame
172+ await get_tree ().process_frame
168173
169174 _advance_target_patrol_point ()
170175 state = State .WAITING
@@ -177,8 +182,11 @@ func _process(delta: float) -> void:
177182 return
178183
179184 match state :
180- State .PATROLLING , State . WAITING , State . DETECTING , State .INVESTIGATING , State .RETURNING :
185+ State .WAITING , State .INVESTIGATING , State .RETURNING :
181186 guard_movement .move ()
187+ State .DETECTING :
188+ if _previous_state != State .PATROLLING :
189+ guard_movement .move ()
182190
183191 if state != State .ALERTED :
184192 _update_player_awareness (delta )
@@ -224,6 +232,13 @@ func _update_debug_info() -> void:
224232 debug_info .text += "%s : %.2f \n " % ["time left" , waiting_timer .time_left ]
225233 State .DETECTING , State .INVESTIGATING , State .RETURNING :
226234 debug_info .text += "%s : %s \n " % ["breadcrumbs" , breadcrumbs .size ()]
235+ State .PATROLLING :
236+ debug_info .text += (
237+ "%s : %s \n " % ["previous_patrol_point_idx" , patrolling_behavior .previous_point_index ]
238+ )
239+ debug_info .text += (
240+ "%s : %s \n " % ["current_patrol_point_idx" , patrolling_behavior .current_point_index ]
241+ )
227242 _ :
228243 debug_info .text += "%s : %s \n " % ["previous_patrol_point_idx" , previous_patrol_point_idx ]
229244 debug_info .text += "%s : %s \n " % ["current_patrol_point_idx" , current_patrol_point_idx ]
@@ -232,9 +247,6 @@ func _update_debug_info() -> void:
232247## What happens when the guard reached the point it was walking towards
233248func _on_destination_reached () -> void :
234249 match state :
235- State .PATROLLING :
236- state = State .WAITING
237- _advance_target_patrol_point ()
238250 State .INVESTIGATING :
239251 state = State .WAITING
240252 State .RETURNING :
@@ -250,14 +262,6 @@ func _on_destination_reached() -> void:
250262## stuck with a collider.
251263func _on_path_blocked () -> void :
252264 match state :
253- State .PATROLLING :
254- state = State .WAITING
255- # This check makes sure that if the guard is blocked on start,
256- # they won't try to set an invalid patrol point as destination.
257- if previous_patrol_point_idx > - 1 :
258- var new_patrol_point : int = previous_patrol_point_idx
259- previous_patrol_point_idx = current_patrol_point_idx
260- current_patrol_point_idx = new_patrol_point
261265 State .INVESTIGATING :
262266 state = State .RETURNING
263267 State .RETURNING :
@@ -277,12 +281,14 @@ func _set_state(new_state: State) -> void:
277281
278282 match state :
279283 State .PATROLLING :
280- var target_position : Vector2 = _patrol_point_position (current_patrol_point_idx )
281- guard_movement .set_destination (target_position )
284+ patrolling_behavior .process_mode = Node .PROCESS_MODE_INHERIT
282285 State .DETECTING :
286+ if _previous_state != State .PATROLLING :
287+ patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
283288 if not _alert_sound .playing :
284289 _alert_sound .play ()
285290 State .ALERTED :
291+ patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
286292 character_animation_player_behavior .process_mode = Node .PROCESS_MODE_DISABLED
287293 if not _alert_sound .playing :
288294 _alert_sound .play ()
@@ -292,9 +298,13 @@ func _set_state(new_state: State) -> void:
292298 player_awareness .visible = true
293299 guard_movement .stop_moving ()
294300 State .INVESTIGATING :
301+ patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
295302 breadcrumbs .push_back (global_position )
296303 State .WAITING :
304+ patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
297305 waiting_timer .start ()
306+ State .RETURNING :
307+ patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
298308
299309
300310## Calculate and set the next point in the patrol path.
@@ -452,6 +462,12 @@ func _set_alert_other_sound_stream(new_value: AudioStream) -> void:
452462 _torch_hit_sound .stream = new_value
453463
454464
465+ func _set_patrol_path (new_patrol_path : Path2D ) -> void :
466+ patrol_path = new_patrol_path
467+ if patrolling_behavior :
468+ patrolling_behavior .walking_path = patrol_path
469+
470+
455471func _set_wait_time (new_wait_time : float ) -> void :
456472 wait_time = new_wait_time
457473 waiting_timer .wait_time = wait_time
@@ -489,3 +505,11 @@ func _on_waiting_timer_timeout() -> void:
489505 state = State .PATROLLING
490506 State .INVESTIGATING :
491507 state = State .RETURNING
508+
509+
510+ func _on_patrolling_behavior_point_reached () -> void :
511+ state = State .WAITING
512+
513+
514+ func _on_patrolling_behavior_got_stuck () -> void :
515+ state = State .WAITING
0 commit comments