11## Defines the playable area of the game and where everything on it lies.
22##
33## The gameboard is defined, essentially, as a grid of [Vector2i] cells. Anything may be
4- ## placed on one of these cells, so the gameboard determines where each cell is located. In this
4+ ## placed on one of these cells, so the gameboard determines where each cell is located. In this
55## case, we are using a simple orthographic (square) projection.
66## [br][br]The grid is contained within the playable [member boundaries] and its constituent cells.
77extends Node
@@ -12,12 +12,13 @@ signal properties_set
1212
1313## Emitted whenever the [member pathfinder] state changes.
1414## This signal is emitted automatically in response to changed [GameboardLayer]s.
15- ## [/br][/br]Note: This signal is only emitted when the actual movement state of the Gameboard
15+ ##
16+ ## Note: This signal is only emitted when the actual movement state of the Gameboard
1617## changes. [GameboardLayer]s may change their cells without actually changing the pathfinder's
1718## state (i.e. a visual update only), in which case this signal is not emitted.
1819signal pathfinder_changed (added_cells : Array [Vector2i ], removed_cells : Array [Vector2i ])
1920
20- ## An invalid cell is not part of the gameboard. Note that this requires positive
21+ ## An invalid cell is not part of the gameboard. Note that this requires positive
2122## [member boundaries].
2223const INVALID_CELL : = Vector2i (- 1 , - 1 )
2324
@@ -43,7 +44,7 @@ func cell_to_pixel(cell_coordinates: Vector2i) -> Vector2:
4344func pixel_to_cell (pixel_coordinates : Vector2 ) -> Vector2i :
4445 @warning_ignore ("integer_division" )
4546 return Vector2i (
46- floori (pixel_coordinates .x / properties .cell_size .x ),
47+ floori (pixel_coordinates .x / properties .cell_size .x ),
4748 floori (pixel_coordinates .y / properties .cell_size .y )
4849 )
4950
@@ -73,9 +74,9 @@ func index_to_cell(index: int) -> Vector2i:
7374 index % properties .extents .size .x + properties .extents .position .x ,
7475 index / properties .extents .size .x + properties .extents .position .y
7576 )
76-
77+
7778 if properties .extents .has_point (cell ):
78- return cell
79+ return cell
7980 return INVALID_CELL
8081
8182
@@ -94,7 +95,7 @@ func get_adjacent_cells(cell: Vector2i) -> Array[Vector2i]:
9495 var neighbour = get_adjacent_cell (cell , direction )
9596 if not neighbour == INVALID_CELL and not neighbour == cell :
9697 neighbours .append (neighbour )
97-
98+
9899 return neighbours
99100
100101
@@ -107,13 +108,13 @@ func register_gameboard_layer(board_map: GameboardLayer) -> void:
107108 # Compare the changed cells with those already in the pathfinder. Any changes will cause the
108109 # Pathfinder to be updated.
109110 board_map .cells_changed .connect (
110- func _on_gameboard_layer_cells_changed (cleared_cells : Array [Vector2i ],
111+ func _on_gameboard_layer_cells_changed (cleared_cells : Array [Vector2i ],
111112 blocked_cells : Array [Vector2i ]):
112113 if board_map .name == "DoorGameboardLayer" :
113114 print ("Door layer " , cleared_cells , " " , blocked_cells )
114115 var added_cells : = _add_cells_to_pathfinder (cleared_cells )
115116 var removed_cells : = _remove_cells_from_pathfinder (blocked_cells )
116-
117+
117118 _connect_new_pathfinder_cells (added_cells )
118119 if not added_cells .is_empty () or not removed_cells .is_empty ():
119120 pathfinder_changed .emit (added_cells .values (), removed_cells )
@@ -125,7 +126,7 @@ func register_gameboard_layer(board_map: GameboardLayer) -> void:
125126# from cleared_cells). Key = cell id (int, see cell_to_index), value = coordinate (Vector2i)
126127func _add_cells_to_pathfinder (cleared_cells : Array [Vector2i ]) -> Dictionary [int , Vector2i ]:
127128 var added_cells : Dictionary [int , Vector2i ] = {}
128-
129+
129130 # Verify whether or not cleared/blocked cells will change the state of the pathfinder.
130131 # If there is no change in state, we will not pass along the cell to other systems and
131132 # the pathfinder won't actually be changed.
@@ -136,15 +137,15 @@ func _add_cells_to_pathfinder(cleared_cells: Array[Vector2i]) -> Dictionary[int,
136137 var uid : = cell_to_index (cell )
137138 pathfinder .add_point (uid , cell )
138139 added_cells [uid ] = cell
139-
140+
140141 # Flag the cell as disabled if it is occupied.
141142 if GamepieceRegistry .get_gamepiece (cell ):
142143 pathfinder .set_point_disabled (uid )
143144 return added_cells
144145
145146
146- # Remove cells from the pathfinder so that Gamepieces can no longer move through them.
147- # Only one Gameboard layer needs to block a cell for it to be considered blocked.
147+ # Remove cells from the pathfinder so that Gamepieces can no longer move through them.
148+ # Only one Gameboard layer needs to block a cell for it to be considered blocked.
148149# Returns an array of cell coordinates that have been blocked. Cells that were already not in the
149150# pathfinder will be excluded from this array.
150151func _remove_cells_from_pathfinder (blocked_cells : Array [Vector2i ]) -> Array [Vector2i ]:
@@ -170,23 +171,25 @@ func _connect_new_pathfinder_cells(added_cells: Dictionary[int, Vector2i]) -> vo
170171 pathfinder .connect_points (uid , neighbor_id )
171172
172173
173- # Checks all [TileMapLayers] in the [constant GameboardLayer.GROUP] to see if the cell is clear
174- # (returns true) or blocked (returns false).
175- # [/br][/br]A clear cell must fulfill two criteria:
176- # [/br]- Exists in at least one of the [GameboardLayer]s.
177- # [/br]- None of the layers block movement at this cell, as defined by the
178- # [constant GameboardLayer.BLOCKED_CELL_DATA_LAYER] custom data layer (see
179- # [method TileData.get_custom_data])
174+ ## Checks all [TileMapLayers] in the [constant GameboardLayer.GROUP] to see if the cell is clear
175+ ## (returns true) or blocked (returns false).
176+ ##
177+ ## A clear cell must fulfill two criteria:
178+ ##
179+ ## - Exists in at least one of the [GameboardLayer]s.[br]
180+ ## - None of the layers block movement at this cell, as defined by the
181+ ## [constant GameboardLayer.BLOCKED_CELL_DATA_LAYER] custom data layer (see
182+ ## [method TileData.get_custom_data])
180183func _is_cell_clear (coord : Vector2i ) -> bool :
181184 # Check to make sure that cell exists.
182185 var cell_exists : = false
183-
186+
184187 for tilemap : GameboardLayer in get_tree ().get_nodes_in_group (GameboardLayer .GROUP ):
185188 if tilemap and coord in tilemap .get_used_cells ():
186189 cell_exists = true
187190 if not tilemap .is_cell_clear (coord ):
188191 return false
189-
192+
190193 # There is no terrain blocking cell movement. However we only want to allow movement if the cell
191194 # actually exists in one of the tilemap layers.
192195 return cell_exists
0 commit comments