@@ -15,6 +15,8 @@ signal player_detected(player: Node2D)
1515enum State {
1616 ## Going along the path.
1717 PATROLLING ,
18+ ## Waiting idle at a path end, or after losing track of player, before returning.
19+ WAITING ,
1820 ## Player is in sight, it takes some time until the player is detected.
1921 DETECTING ,
2022 ## Player was detected.
@@ -53,7 +55,9 @@ const DEFAULT_SPRITE_FRAMES = preload("uid://ovu5wqo15s5g")
5355 patrol_path = new_value
5456
5557## The wait time at each patrol point.
56- @export_range (0 , 5 , 0.1 , "or_greater" , "suffix:s" ) var wait_time : float = 1.0
58+ @export_range (0 , 5 , 0.1 , "or_greater" , "suffix:s" ) var wait_time : float = 1.0 :
59+ set = _set_wait_time
60+
5761## The speed at which the guard moves.
5862@export_range (20 , 300 , 5 , "or_greater" , "or_less" , "suffix:m/s" ) var move_speed : float = 100.0
5963
@@ -89,6 +93,8 @@ var breadcrumbs: Array[Vector2] = []
8993var state : State = State .PATROLLING :
9094 set = _set_state
9195
96+ var _previous_state : State
97+
9298# The player that's being detected.
9399var _player : Node2D
94100
@@ -110,6 +116,9 @@ var _player: Node2D
110116# gdlint:ignore = max-line-length
111117var character_animation_player_behavior : CharacterAnimationPlayerBehavior = % CharacterAnimationPlayerBehavior
112118
119+ ## Timer for the waiting state.
120+ @onready var waiting_timer : Timer = % WaitingTimer
121+
113122## Handles the velocity and movement of the guard.
114123@onready var guard_movement : GuardMovement = % GuardMovement
115124@onready var animated_sprite_2d : AnimatedSprite2D = % AnimatedSprite2D
@@ -153,7 +162,6 @@ func _ready() -> void:
153162 global_position = _patrol_point_position (0 )
154163
155164 guard_movement .destination_reached .connect (self ._on_destination_reached )
156- guard_movement .still_time_finished .connect (self ._on_still_time_finished )
157165 guard_movement .path_blocked .connect (self ._on_path_blocked )
158166
159167
@@ -228,35 +236,28 @@ func _update_debug_info() -> void:
228236 debug_info .text += "%s : %s \n " % ["state" , State .keys ()[state ]]
229237 debug_info .text += "%s : %s \n " % ["previous_patrol_point_idx" , previous_patrol_point_idx ]
230238 debug_info .text += "%s : %s \n " % ["current_patrol_point_idx" , current_patrol_point_idx ]
231- debug_info .text += "%s : %.2f \n " % ["time left" , guard_movement . still_time_left_in_seconds ]
239+ debug_info .text += "%s : %.2f \n " % ["time left" , waiting_timer . time_left ]
232240 debug_info .text += "%s : %s \n " % ["target point" , guard_movement .destination ]
233241
234242
235243## What happens when the guard reached the point it was walking towards
236244func _on_destination_reached () -> void :
237245 match state :
238246 State .PATROLLING :
239- guard_movement . wait_seconds ( wait_time )
247+ state = State . WAITING
240248 _advance_target_patrol_point ()
241249 State .INVESTIGATING :
242- guard_movement . wait_seconds ( wait_time )
250+ state = State . WAITING
243251 State .RETURNING :
244252 breadcrumbs .pop_back ()
245253
246254
247- ## What happens when the guard finished waiting on a point.
248- func _on_still_time_finished () -> void :
249- match state :
250- State .INVESTIGATING :
251- state = State .RETURNING
252-
253-
254255## What happens if the guard cannot reach their destination because it got
255256## stuck with a collider.
256257func _on_path_blocked () -> void :
257258 match state :
258259 State .PATROLLING :
259- guard_movement . wait_seconds ( wait_time )
260+ state = State . WAITING
260261 # This check makes sure that if the guard is blocked on start,
261262 # they won't try to set an invalid patrol point as destination.
262263 if previous_patrol_point_idx > - 1 :
@@ -274,6 +275,7 @@ func _set_state(new_state: State) -> void:
274275 if state == new_state :
275276 return
276277
278+ _previous_state = state
277279 state = new_state
278280
279281 match state :
@@ -289,8 +291,9 @@ func _set_state(new_state: State) -> void:
289291 player_awareness .tint_progress = Color .RED
290292 player_awareness .visible = true
291293 State .INVESTIGATING :
292- guard_movement .start_moving_now ()
293294 breadcrumbs .push_back (global_position )
295+ State .WAITING :
296+ waiting_timer .start ()
294297
295298
296299## Calculate and set the next point in the patrol path.
@@ -448,6 +451,11 @@ func _set_alert_other_sound_stream(new_value: AudioStream) -> void:
448451 _torch_hit_sound .stream = new_value
449452
450453
454+ func _set_wait_time (new_wait_time : float ) -> void :
455+ wait_time = new_wait_time
456+ waiting_timer .wait_time = wait_time
457+
458+
451459func _on_instant_detection_area_body_entered (body : Node2D ) -> void :
452460 state = State .ALERTED
453461 player_detected .emit (body )
@@ -470,3 +478,11 @@ func _on_detection_area_body_exited(body: Node2D) -> void:
470478 if state == State .DETECTING :
471479 guard_movement .stop_moving ()
472480 state = State .INVESTIGATING
481+
482+
483+ func _on_waiting_timer_timeout () -> void :
484+ match _previous_state :
485+ State .PATROLLING :
486+ state = State .PATROLLING
487+ State .INVESTIGATING :
488+ state = State .RETURNING
0 commit comments