@@ -78,11 +78,6 @@ const DEFAULT_SPRITE_FRAMES = preload("uid://ovu5wqo15s5g")
7878## Toggles visibility of debug info.
7979@export var show_debug_info : bool = false
8080
81- ## Index of the previous patrol point, -1 means that there isn't a previous
82- ## point yet.
83- var previous_patrol_point_idx : int = - 1
84- ## Index of the current patrol point.
85- var current_patrol_point_idx : int = 0
8681## Breadcrumbs for tracking guards position while investigating, before
8782## returning to patrol, the guard walks through all these positions.
8883var breadcrumbs : Array [Vector2 ] = []
@@ -158,11 +153,6 @@ func _ready() -> void:
158153 if detection_area :
159154 detection_area .scale = Vector2 .ONE * detection_area_scale
160155
161- # When the level starts, the guard is placed at the beginning of the
162- # patrol path.
163- if patrol_path :
164- global_position = _patrol_point_position (0 )
165-
166156 guard_movement .destination_reached .connect (self ._on_destination_reached )
167157 guard_movement .path_blocked .connect (self ._on_path_blocked )
168158
@@ -171,7 +161,6 @@ func _ready() -> void:
171161 await get_tree ().process_frame
172162 await get_tree ().process_frame
173163
174- _advance_target_patrol_point ()
175164 state = State .WAITING
176165
177166
@@ -234,14 +223,11 @@ func _update_debug_info() -> void:
234223 debug_info .text += "%s : %s \n " % ["breadcrumbs" , breadcrumbs .size ()]
235224 State .PATROLLING :
236225 debug_info .text += (
237- "%s : %s \n " % ["previous_patrol_point_idx " , patrolling_behavior .previous_point_index ]
226+ "%s : %s \n " % ["previous_point_index " , patrolling_behavior .previous_point_index ]
238227 )
239228 debug_info .text += (
240- "%s : %s \n " % ["current_patrol_point_idx " , patrolling_behavior .current_point_index ]
229+ "%s : %s \n " % ["current_point_index " , patrolling_behavior .current_point_index ]
241230 )
242- _ :
243- debug_info .text += "%s : %s \n " % ["previous_patrol_point_idx" , previous_patrol_point_idx ]
244- debug_info .text += "%s : %s \n " % ["current_patrol_point_idx" , current_patrol_point_idx ]
245231
246232
247233## What happens when the guard reached the point it was walking towards
@@ -307,41 +293,6 @@ func _set_state(new_state: State) -> void:
307293 patrolling_behavior .process_mode = Node .PROCESS_MODE_DISABLED
308294
309295
310- ## Calculate and set the next point in the patrol path.
311- ## The guard would circle back if the path is open, and go in rounds if the
312- ## path is closed.
313- func _advance_target_patrol_point () -> void :
314- if not patrol_path or not patrol_path .curve or _amount_of_patrol_points () < 2 :
315- return
316-
317- var new_patrol_point_idx : int
318-
319- if _is_patrol_path_closed ():
320- # amount of points - 1 is used here because in a closed path, the
321- # last and first patrol points are the same. So, this lets us skip
322- # that repeated point and go for the first one that is different
323- new_patrol_point_idx = (current_patrol_point_idx + 1 ) % (_amount_of_patrol_points () - 1 )
324- else :
325- var at_last_point : bool = current_patrol_point_idx == (_amount_of_patrol_points () - 1 )
326- var at_first_point : bool = current_patrol_point_idx == 0
327- var going_backwards_in_path : bool = previous_patrol_point_idx > current_patrol_point_idx
328- if at_last_point :
329- # When reaching the end of the path, it starts walking back
330- new_patrol_point_idx = current_patrol_point_idx - 1
331- elif at_first_point :
332- # If it's at first point is either because it was walking back
333- # or because it's the first time it will move, in any case, it moves
334- # forward
335- new_patrol_point_idx = current_patrol_point_idx + 1
336- elif going_backwards_in_path :
337- new_patrol_point_idx = current_patrol_point_idx - 1
338- else :
339- new_patrol_point_idx = current_patrol_point_idx + 1
340-
341- previous_patrol_point_idx = current_patrol_point_idx
342- current_patrol_point_idx = new_patrol_point_idx
343-
344-
345296## Checks if a straight line can be traced from the Guard to a certain point.
346297## It returns true if the path to the point is free of walls.
347298## Note: it only detects sight_occluders collisions, not wall collisions, this
@@ -352,39 +303,13 @@ func _is_sight_to_point_blocked(point_position: Vector2) -> bool:
352303 return sight_ray_cast .is_colliding ()
353304
354305
355- ## Patrol point index to global position
356- func _patrol_point_position (point_idx : int ) -> Vector2 :
357- var local_point_position : Vector2 = patrol_path .curve .get_point_position (point_idx )
358- return patrol_path .to_global (local_point_position )
359-
360-
361- func _amount_of_patrol_points () -> int :
362- return patrol_path .curve .point_count
363-
364-
365- ## Returns true if the end of the patrol path is the same point as the beginning
366- func _is_patrol_path_closed () -> bool :
367- if not patrol_path :
368- return false
369-
370- var curve : Curve2D = patrol_path .curve
371- if curve .point_count < 3 :
372- return false
373-
374- var first_point_position : Vector2 = curve .get_point_position (0 )
375- var last_point_position : Vector2 = curve .get_point_position (curve .point_count - 1 )
376-
377- return first_point_position .is_equal_approx (last_point_position )
378-
379-
380306## Resets the guard to its initial values and placement on screen so it starts
381307## patrolling again as if the level just started.
382308func _reset () -> void :
383- previous_patrol_point_idx = - 1
384- current_patrol_point_idx = 0
385309 velocity = Vector2 .ZERO
386310 if patrol_path :
387- global_position = _patrol_point_position (0 )
311+ var local_point_position : Vector2 = patrol_path .curve .get_point_position (0 )
312+ global_position = patrol_path .to_global (local_point_position )
388313
389314
390315## When the scene is saved, resets the Guard's position to the beginning of
0 commit comments