Skip to content

Commit f557809

Browse files
committed
Guard script: Add returning behavior
Use a PointsWalkBehavior and dynamically create the path from the breadcrumbs. Go back to patrolling after walking through all the breadcrumbs. That is, when the path end is met.
1 parent 27bbeb5 commit f557809

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

261253
func _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

440448
func _on_patrolling_behavior_got_stuck() -> void:
441449
state = State.WAITING
450+
451+
452+
func _on_returning_behavior_ending_reached() -> void:
453+
state = State.PATROLLING

scenes/game_elements/characters/enemies/guard/guard.tscn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ bus = &"SFX"
369369
[connection signal="timeout" from="WaitingTimer" to="." method="_on_waiting_timer_timeout"]
370370
[connection signal="got_stuck" from="PatrollingBehavior" to="." method="_on_patrolling_behavior_got_stuck"]
371371
[connection signal="point_reached" from="PatrollingBehavior" to="." method="_on_patrolling_behavior_point_reached"]
372+
[connection signal="ending_reached" from="ReturningBehavior" to="." method="_on_returning_behavior_ending_reached"]
372373
[connection signal="body_entered" from="InstantDetectionArea" to="." method="_on_instant_detection_area_body_entered"]
373374
[connection signal="body_entered" from="DetectionArea" to="." method="_on_detection_area_body_entered"]
374375
[connection signal="body_exited" from="DetectionArea" to="." method="_on_detection_area_body_exited"]

scenes/game_logic/walk_behaviors/points_walk_behavior.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ extends BaseCharacterBehavior
1212
## [br][br]
1313
## If the character gets stuck while walking the path, they turn around.
1414

15+
## Emitted when [member character] reaches the ending of the path.
16+
signal ending_reached
17+
1518
## Emitted when a point of the path is reached.
1619
## This could be used to wait standing for a bit in these points.
1720
signal point_reached
@@ -92,6 +95,11 @@ func _physics_process(delta: float) -> void:
9295
)
9396
and walking_path
9497
):
98+
if (
99+
(current_point_index == walking_path.curve.point_count - 1)
100+
or (current_point_index == 0)
101+
):
102+
ending_reached.emit()
95103
_advance_target_patrol_point()
96104
var local_point_position: Vector2 = walking_path.curve.get_point_position(
97105
current_point_index

0 commit comments

Comments
 (0)