@@ -5,6 +5,11 @@ class_name Pathfinder
55extends AStar2D
66
77
8+ const FLAG_ALLOW_SOURCE_OCCUPANT = 1 << 0
9+ const FLAG_ALLOW_TARGET_OCCUPANT = 1 << 1
10+ const FLAG_ALLOW_ALL_OCCUPANTS = 1 << 2
11+
12+
813func _init () -> void :
914 # Disable/re-enable occupied cells whenever a gamepiece moves.
1015 GamepieceRegistry .gamepiece_moved .connect (
@@ -33,36 +38,44 @@ func can_move_to(coord: Vector2i) -> bool:
3338## Find a path between two cells. Returns an empty array if no path is available.
3439## If allow_blocked_source or allow_blocked_target are false, the pathinder wlil fail if a gamepiece
3540## occupies the source or target cells, respectively.
36- func get_path_to_cell (source_coord : Vector2i , target_coord : Vector2i ,
37- allow_disabled_source : = true , allow_disabled_target : = false ) -> Array [Vector2i ]:
41+ func get_path_to_cell (source_coord : Vector2i , target_coord : Vector2i ,
42+ occupancy_flags : int = 1 ) -> Array [Vector2i ]:
3843 # Store the return value in a variable.
3944 var move_path : Array [Vector2i ] = []
4045
4146 # Find the source/target IDs and keep track of whether or not the cells are occupied.
4247 var source_id : = Gameboard .cell_to_index (source_coord )
4348 var target_id : = Gameboard .cell_to_index (target_coord )
4449
50+ # The pathfinder has several flags to ignore cell occupancy. We'll need to track which occupants
51+ # are temporarily ignored and then re-disable their pathfinder points once a path is found.
52+ # Key is point id, value is whether or not the point is disabled.
53+ var ignored_points : Dictionary [int , bool ] = {}
54+ if (occupancy_flags & FLAG_ALLOW_ALL_OCCUPANTS ) != 0 :
55+ print ("Allow all occupants!" )
56+ for id in get_point_ids ():
57+ if is_point_disabled (id ):
58+ ignored_points [id ] = true
59+ set_point_disabled (id , false )
60+
4561 if has_point (source_id ) and has_point (target_id ):
4662 # Check to see if we want to un-disable the source/target cells.
47- var is_source_disabled : = is_point_disabled ( source_id )
48- var is_target_disabled : = is_point_disabled ( target_id )
49- if allow_disabled_source :
63+ if ( occupancy_flags & FLAG_ALLOW_SOURCE_OCCUPANT ) != 0 :
64+ print ( "Allow source occupant!" )
65+ ignored_points [ source_id ] = is_point_disabled ( source_id )
5066 set_point_disabled (source_id , false )
51- if allow_disabled_target :
67+ if (occupancy_flags & FLAG_ALLOW_TARGET_OCCUPANT ) != 0 :
68+ print ("Allow target occupant!" )
69+ ignored_points [target_id ] = is_point_disabled (target_id )
5270 set_point_disabled (target_id , false )
5371
5472 for path_coord : Vector2i in get_point_path (source_id , target_id ):
5573 if path_coord != source_coord : # Don't include the source as the first path element.
5674 move_path .append (path_coord )
5775
58- # If the source/target cells had originally been disabled, re-disable them here.
59- if allow_disabled_source :
60- set_point_disabled (source_id , is_source_disabled )
61- if allow_disabled_target :
62- set_point_disabled (target_id , is_target_disabled )
63-
64-
65- # set_point_disabled(source_id, is_source_disabled)
76+ # Change any enabled cells back to their previous state.
77+ for id in ignored_points :
78+ set_point_disabled (id , ignored_points [id ])
6679
6780 return move_path
6881
0 commit comments