Skip to content

Commit 4f66e1d

Browse files
committed
Guard script: Use timer for the waiting
Add a timer node, and remove the custom time handling from the guard movement.
1 parent 3d0215b commit 4f66e1d

2 files changed

Lines changed: 31 additions & 42 deletions

File tree

scenes/game_elements/characters/enemies/guard/components/guard.gd

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ signal player_detected(player: Node2D)
1515
enum 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] = []
8993
var state: State = State.PATROLLING:
9094
set = _set_state
9195

96+
var _previous_state: State
97+
9298
# The player that's being detected.
9399
var _player: Node2D
94100

@@ -110,6 +116,9 @@ var _player: Node2D
110116
# gdlint:ignore = max-line-length
111117
var 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
236244
func _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.
256257
func _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+
451459
func _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

scenes/game_elements/characters/enemies/guard/components/guard_movement.gd

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
class_name GuardMovement
55
extends Node2D
66

7-
## Emitted when [member still_time_left] reaches 0
8-
signal still_time_finished
97
## Emitted when [member guard] reached [member destination]
108
signal destination_reached
119
## Emitted when [member guard] got stuck trying to reach [member destination]
1210
signal path_blocked
1311

14-
## While this time is greater than 0, the guard won't move
15-
var still_time_left_in_seconds: float = 0.0
1612
var _destination_reached: bool = true
1713

1814
## Target position into which the guard will move, in absolute coordinates
@@ -21,11 +17,6 @@ var _destination_reached: bool = true
2117

2218

2319
func _process(delta: float) -> void:
24-
if still_time_left_in_seconds > 0.0:
25-
still_time_left_in_seconds = move_toward(still_time_left_in_seconds, 0.0, delta)
26-
if still_time_left_in_seconds <= 0.0:
27-
still_time_finished.emit()
28-
2920
if (
3021
not _destination_reached
3122
and guard.global_position.distance_to(destination) <= guard.velocity.length() * delta
@@ -50,35 +41,17 @@ func move() -> void:
5041
## Returns the velocity the guard should have, receives the delta time since
5142
## the last frame as a parameter
5243
func calculate_velocity() -> Vector2:
53-
if still_time_left_in_seconds > 0.0 or _destination_reached:
44+
if _destination_reached:
5445
return Vector2.ZERO
5546
return guard.global_position.direction_to(destination) * guard.move_speed
5647

5748

58-
## Make the guard stop for the given time, in seconds
59-
func wait_seconds(time: float) -> void:
60-
still_time_left_in_seconds = time
61-
62-
6349
## Sets the next point into which the guard will move.
64-
## It won't make the guard move if [member still_time_left_in_seconds] if
65-
## greater than 0.
6650
func set_destination(new_destination: Vector2) -> void:
6751
_destination_reached = false
6852
destination = new_destination
6953

7054

71-
func start_moving_now() -> void:
72-
still_time_left_in_seconds = 0.0
73-
74-
75-
## Sets the next point into which the guard will move AND makes it start moving
76-
## even if [member still_time_left_in_seconds] was positive.
77-
func start_moving_towards(new_destination: Vector2) -> void:
78-
set_destination(new_destination)
79-
still_time_left_in_seconds = 0.0
80-
81-
8255
## Sets the destination to the same point the guard is at so it doesn't try to
8356
## travel to any new point
8457
func stop_moving() -> void:

0 commit comments

Comments
 (0)