Skip to content

Commit 6d37841

Browse files
committed
Guard script: Do PATROLLING state change only once
The destination point was being set on every process loop.
1 parent 80303e9 commit 6d37841

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

  • scenes/game_elements/characters/enemies/guard/components

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var current_patrol_point_idx: int = 0
8888
## returning to patrol, the guard walks through all these positions.
8989
var breadcrumbs: Array[Vector2] = []
9090
## Current state of the guard.
91-
var state: State = State.PATROLLING:
91+
var state: State:
9292
set = _set_state
9393

9494
var _previous_state: State
@@ -162,6 +162,13 @@ func _ready() -> void:
162162
guard_movement.destination_reached.connect(self._on_destination_reached)
163163
guard_movement.path_blocked.connect(self._on_path_blocked)
164164

165+
# Wait 2 frames before starting to match backwards compatibility.
166+
await get_tree().process_frame
167+
await get_tree().process_frame
168+
169+
_advance_target_patrol_point()
170+
state = State.WAITING
171+
165172

166173
func _process(delta: float) -> void:
167174
_update_debug_info()
@@ -170,14 +177,7 @@ func _process(delta: float) -> void:
170177
return
171178

172179
match state:
173-
State.PATROLLING:
174-
if patrol_path:
175-
var target_position: Vector2 = _patrol_point_position(current_patrol_point_idx)
176-
guard_movement.set_destination(target_position)
177-
else:
178-
guard_movement.stop_moving()
179-
guard_movement.move()
180-
State.WAITING, State.DETECTING, State.INVESTIGATING, State.RETURNING:
180+
State.PATROLLING, State.WAITING, State.DETECTING, State.INVESTIGATING, State.RETURNING:
181181
guard_movement.move()
182182

183183
if state != State.ALERTED:
@@ -276,6 +276,9 @@ func _set_state(new_state: State) -> void:
276276
state = new_state
277277

278278
match state:
279+
State.PATROLLING:
280+
var target_position: Vector2 = _patrol_point_position(current_patrol_point_idx)
281+
guard_movement.set_destination(target_position)
279282
State.DETECTING:
280283
if not _alert_sound.playing:
281284
_alert_sound.play()

0 commit comments

Comments
 (0)