@@ -105,6 +105,9 @@ var _player: Node2D
105105## Behavior to use when patrolling.
106106@onready var patrolling_behavior : PointsWalkBehavior = % PatrollingBehavior
107107
108+ ## Behavior to use when returning.
109+ @onready var returning_behavior : PointsWalkBehavior = % ReturningBehavior
110+
108111## Reference to the node controlling the AnimationPlayer for walking / being idle,
109112## so it can be disabled to play the alerted animation.
110113@onready
@@ -147,6 +150,8 @@ func _ready() -> void:
147150 player_awareness .value = 0.0
148151
149152 patrolling_behavior .speeds .walk_speed = move_speed
153+ returning_behavior .speeds .walk_speed = move_speed
154+
150155 _set_patrol_path (patrol_path )
151156 _set_sprite_frames (sprite_frames )
152157
@@ -171,7 +176,7 @@ func _process(delta: float) -> void:
171176 return
172177
173178 match state :
174- State .WAITING , State .INVESTIGATING , State . RETURNING :
179+ State .WAITING , State .INVESTIGATING :
175180 guard_movement .move ()
176181 State .DETECTING :
177182 if _previous_state != State .PATROLLING :
@@ -219,7 +224,7 @@ func _update_debug_info() -> void:
219224 match state :
220225 State .WAITING :
221226 debug_info .text += "%s : %.2f \n " % ["time left" , waiting_timer .time_left ]
222- State .DETECTING , State .INVESTIGATING , State . RETURNING :
227+ State .DETECTING , State .INVESTIGATING :
223228 debug_info .text += "%s : %s \n " % ["breadcrumbs" , breadcrumbs .size ()]
224229 State .PATROLLING :
225230 debug_info .text += (
@@ -235,13 +240,6 @@ func _on_destination_reached() -> void:
235240 match state :
236241 State .INVESTIGATING :
237242 state = State .WAITING
238- State .RETURNING :
239- breadcrumbs .pop_back ()
240- if breadcrumbs :
241- var target_position : Vector2 = breadcrumbs .back ()
242- guard_movement .set_destination (target_position )
243- else :
244- state = State .PATROLLING
245243
246244
247245## What happens if the guard cannot reach their destination because it got
@@ -250,12 +248,6 @@ func _on_path_blocked() -> void:
250248 match state :
251249 State .INVESTIGATING :
252250 state = State .RETURNING
253- State .RETURNING :
254- if breadcrumbs :
255- var target_position : Vector2 = breadcrumbs .back ()
256- guard_movement .set_destination (target_position )
257- else :
258- state = State .PATROLLING
259251
260252
261253func _set_state (new_state : State ) -> void :
@@ -268,13 +260,16 @@ func _set_state(new_state: State) -> void:
268260 match state :
269261 State .PATROLLING :
270262 patrolling_behavior .process_mode = Node .PROCESS_MODE_INHERIT
263+ returning_behavior .process_mode = Node .PROCESS_MODE_DISABLED
271264 State .DETECTING :
272265 if _previous_state != State .PATROLLING :
273266 patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
267+ returning_behavior .process_mode = Node .PROCESS_MODE_DISABLED
274268 if not _alert_sound .playing :
275269 _alert_sound .play ()
276270 State .ALERTED :
277271 patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
272+ returning_behavior .process_mode = Node .PROCESS_MODE_DISABLED
278273 character_animation_player_behavior .process_mode = Node .PROCESS_MODE_DISABLED
279274 if not _alert_sound .playing :
280275 _alert_sound .play ()
@@ -285,12 +280,25 @@ func _set_state(new_state: State) -> void:
285280 guard_movement .stop_moving ()
286281 State .INVESTIGATING :
287282 patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
283+ returning_behavior .process_mode = Node .PROCESS_MODE_DISABLED
288284 breadcrumbs .push_back (global_position )
289285 State .WAITING :
290286 patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
287+ returning_behavior .process_mode = Node .PROCESS_MODE_DISABLED
291288 waiting_timer .start ()
289+ guard_movement .stop_moving ()
292290 State .RETURNING :
293291 patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
292+ var returning_path := Path2D .new ()
293+ returning_path .curve = Curve2D .new ()
294+ breadcrumbs .push_back (global_position )
295+ breadcrumbs .reverse ()
296+ for point : Vector2 in breadcrumbs :
297+ returning_path .curve .add_point (point )
298+ returning_behavior .walking_path = returning_path
299+ returning_behavior .process_mode = Node .PROCESS_MODE_INHERIT
300+ breadcrumbs = []
301+ guard_movement .stop_moving ()
294302
295303
296304## Checks if a straight line can be traced from the Guard to a certain point.
@@ -439,3 +447,7 @@ func _on_patrolling_behavior_point_reached() -> void:
439447
440448func _on_patrolling_behavior_got_stuck () -> void :
441449 state = State .WAITING
450+
451+
452+ func _on_returning_behavior_ending_reached () -> void :
453+ state = State .PATROLLING
0 commit comments