Comments and documentation update
This commit is contained in:
@@ -3,7 +3,12 @@ extends Node2D
|
||||
signal score_changed(points: int)
|
||||
signal grid_state_loaded(grid_size: Vector2i, tile_types: int)
|
||||
|
||||
enum GameState { WAITING, SELECTING, SWAPPING, PROCESSING } # Waiting for player input # First tile selected # Animating tile swap # Processing matches and cascades
|
||||
## Match-3 Game State Machine
|
||||
## WAITING: Idle state, accepting player input for tile selection
|
||||
## SELECTING: First tile selected, waiting for second tile to complete swap
|
||||
## SWAPPING: Animation in progress, input blocked during tile swap
|
||||
## PROCESSING: Detecting matches, clearing tiles, dropping new ones, checking cascades
|
||||
enum GameState { WAITING, SELECTING, SWAPPING, PROCESSING }
|
||||
|
||||
var GRID_SIZE := Vector2i(8, 8)
|
||||
var TILE_TYPES := 5
|
||||
@@ -124,6 +129,7 @@ func _initialize_grid():
|
||||
|
||||
|
||||
func _has_match_at(pos: Vector2i) -> bool:
|
||||
"""Check if tile at position is part of a 3+ match horizontally or vertically"""
|
||||
# Bounds and null checks
|
||||
if not _is_valid_grid_position(pos):
|
||||
return false
|
||||
@@ -151,8 +157,8 @@ func _has_match_at(pos: Vector2i) -> bool:
|
||||
return matches_vertical.size() >= 3
|
||||
|
||||
|
||||
# Check for any matches on the board
|
||||
func _check_for_matches() -> bool:
|
||||
"""Scan entire grid to detect if any matches exist (used for cascade detection)"""
|
||||
for y in range(GRID_SIZE.y):
|
||||
for x in range(GRID_SIZE.x):
|
||||
if _has_match_at(Vector2i(x, y)):
|
||||
@@ -161,6 +167,12 @@ func _check_for_matches() -> bool:
|
||||
|
||||
|
||||
func _get_match_line(start: Vector2i, dir: Vector2i) -> Array:
|
||||
"""Find all consecutive matching tiles in a line from start position in given direction.
|
||||
|
||||
Returns array of tile nodes that form a continuous match.
|
||||
Direction must be unit vector (1,0), (-1,0), (0,1), or (0,-1).
|
||||
Searches bidirectionally from start position.
|
||||
"""
|
||||
# Validate input parameters
|
||||
if not _is_valid_grid_position(start):
|
||||
DebugManager.log_error(
|
||||
@@ -185,13 +197,14 @@ func _get_match_line(start: Vector2i, dir: Vector2i) -> Array:
|
||||
if not "tile_type" in start_tile:
|
||||
return []
|
||||
|
||||
var line = [start_tile]
|
||||
var type = start_tile.tile_type
|
||||
var line = [start_tile] # Initialize with start tile
|
||||
var type = start_tile.tile_type # Type to match against
|
||||
|
||||
# Check both directions with safety limits
|
||||
for offset in [1, -1]:
|
||||
# Check both directions from start position (bidirectional search)
|
||||
for offset in [1, -1]: # offset 1 = forward direction, -1 = backward direction
|
||||
var current = start + dir * offset
|
||||
var steps = 0
|
||||
# Safety limit prevents infinite loops in case of logic errors
|
||||
while steps < GRID_SIZE.x + GRID_SIZE.y and _is_valid_grid_position(current):
|
||||
if current.y >= grid.size() or current.x >= grid[current.y].size():
|
||||
break
|
||||
@@ -200,17 +213,23 @@ func _get_match_line(start: Vector2i, dir: Vector2i) -> Array:
|
||||
if not neighbor or not is_instance_valid(neighbor):
|
||||
break
|
||||
|
||||
# Stop if tile type doesn't match (end of matching sequence)
|
||||
if not "tile_type" in neighbor or neighbor.tile_type != type:
|
||||
break
|
||||
|
||||
line.append(neighbor)
|
||||
current += dir * offset
|
||||
steps += 1
|
||||
line.append(neighbor) # Add matching tile to sequence
|
||||
current += dir * offset # Move to next position
|
||||
steps += 1 # Increment safety counter
|
||||
|
||||
return line
|
||||
|
||||
|
||||
func _clear_matches():
|
||||
func _clear_matches() -> void:
|
||||
"""Find and remove all match groups of 3+ tiles, calculating score and triggering effects.
|
||||
|
||||
Uses flood-fill approach to group connected matches, prevents double-counting tiles.
|
||||
Handles tile removal, score calculation, and visual effects.
|
||||
"""
|
||||
# Check grid integrity
|
||||
if not _validate_grid_integrity():
|
||||
DebugManager.log_error("Grid integrity check failed in _clear_matches", "Match3")
|
||||
@@ -693,6 +712,14 @@ func _select_tile_at_cursor() -> void:
|
||||
|
||||
|
||||
func _on_tile_selected(tile: Node2D) -> void:
|
||||
"""Handle tile selection with state machine logic for match-3 gameplay.
|
||||
|
||||
State transitions:
|
||||
WAITING -> SELECTING: First tile selected
|
||||
SELECTING -> WAITING: Same tile clicked (deselect)
|
||||
SELECTING -> SWAPPING: Different tile clicked (attempt swap)
|
||||
"""
|
||||
# Block tile selection during busy states
|
||||
if current_state == GameState.SWAPPING or current_state == GameState.PROCESSING:
|
||||
DebugManager.log_debug(
|
||||
"Tile selection ignored - game busy (state: %s)" % [GameState.keys()[current_state]],
|
||||
@@ -736,7 +763,7 @@ func _on_tile_selected(tile: Node2D) -> void:
|
||||
func _select_tile(tile: Node2D) -> void:
|
||||
selected_tile = tile
|
||||
tile.is_selected = true
|
||||
current_state = GameState.SELECTING
|
||||
current_state = GameState.SELECTING # State transition: WAITING -> SELECTING
|
||||
DebugManager.log_debug(
|
||||
"Selected tile at (%d, %d)" % [tile.grid_position.x, tile.grid_position.y], "Match3"
|
||||
)
|
||||
@@ -816,8 +843,8 @@ func _attempt_swap(tile1: Node2D, tile2: Node2D) -> void:
|
||||
"Match3"
|
||||
)
|
||||
|
||||
current_state = GameState.SWAPPING
|
||||
await _swap_tiles(tile1, tile2)
|
||||
current_state = GameState.SWAPPING # State transition: SELECTING -> SWAPPING
|
||||
await _swap_tiles(tile1, tile2) # Animate tile swap
|
||||
|
||||
# Check if swap creates matches
|
||||
if _has_match_at(tile1.grid_position) or _has_match_at(tile2.grid_position):
|
||||
|
||||
Reference in New Issue
Block a user