|
|
|
|
@@ -49,13 +49,21 @@ func _ready() -> void:
|
|
|
|
|
instance_id = "Match3_%d" % get_instance_id()
|
|
|
|
|
|
|
|
|
|
if grid_initialized:
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
"[%s] Match3 _ready() called multiple times, skipping initialization" % instance_id,
|
|
|
|
|
"Match3"
|
|
|
|
|
(
|
|
|
|
|
DebugManager
|
|
|
|
|
. log_warn(
|
|
|
|
|
(
|
|
|
|
|
"[%s] Match3 _ready() called multiple times, skipping initialization"
|
|
|
|
|
% instance_id
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
DebugManager.log_debug("[%s] Match3 _ready() started" % instance_id, "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"[%s] Match3 _ready() started" % instance_id, "Match3"
|
|
|
|
|
)
|
|
|
|
|
grid_initialized = true
|
|
|
|
|
|
|
|
|
|
# Calculate grid layout
|
|
|
|
|
@@ -64,12 +72,16 @@ func _ready() -> void:
|
|
|
|
|
# Try to load saved state, otherwise use default
|
|
|
|
|
var loaded_saved_state = await load_saved_state()
|
|
|
|
|
if not loaded_saved_state:
|
|
|
|
|
DebugManager.log_info("No saved state found, using default grid initialization", "Match3")
|
|
|
|
|
DebugManager.log_info(
|
|
|
|
|
"No saved state found, using default grid initialization", "Match3"
|
|
|
|
|
)
|
|
|
|
|
_initialize_grid()
|
|
|
|
|
else:
|
|
|
|
|
DebugManager.log_info("Successfully loaded saved grid state", "Match3")
|
|
|
|
|
|
|
|
|
|
DebugManager.log_debug("Match3 _ready() completed, calling debug structure check", "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Match3 _ready() completed, calling debug structure check", "Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Notify UI that grid state is loaded
|
|
|
|
|
grid_state_loaded.emit(grid_size, tile_types)
|
|
|
|
|
@@ -91,7 +103,8 @@ func _calculate_grid_layout():
|
|
|
|
|
# Align grid to left side with margins
|
|
|
|
|
var total_grid_height = tile_size * grid_size.y
|
|
|
|
|
grid_offset = Vector2(
|
|
|
|
|
GRID_LEFT_MARGIN, (viewport_size.y - total_grid_height) / 2 + GRID_TOP_MARGIN
|
|
|
|
|
GRID_LEFT_MARGIN,
|
|
|
|
|
(viewport_size.y - total_grid_height) / 2 + GRID_TOP_MARGIN
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -136,7 +149,9 @@ func _has_match_at(pos: Vector2i) -> bool:
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
if pos.y >= grid.size() or pos.x >= grid[pos.y].size():
|
|
|
|
|
DebugManager.log_error("Grid array bounds exceeded at (%d,%d)" % [pos.x, pos.y], "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid array bounds exceeded at (%d,%d)" % [pos.x, pos.y], "Match3"
|
|
|
|
|
)
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
var tile = grid[pos.y][pos.x]
|
|
|
|
|
@@ -146,7 +161,8 @@ func _has_match_at(pos: Vector2i) -> bool:
|
|
|
|
|
# Check if tile has required properties
|
|
|
|
|
if not "tile_type" in tile:
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
"Tile at (%d,%d) missing tile_type property" % [pos.x, pos.y], "Match3"
|
|
|
|
|
"Tile at (%d,%d) missing tile_type property" % [pos.x, pos.y],
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
@@ -177,13 +193,18 @@ func _get_match_line(start: Vector2i, dir: Vector2i) -> Array:
|
|
|
|
|
# Validate input parameters
|
|
|
|
|
if not _is_valid_grid_position(start):
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Invalid start position for match line: (%d,%d)" % [start.x, start.y], "Match3"
|
|
|
|
|
(
|
|
|
|
|
"Invalid start position for match line: (%d,%d)"
|
|
|
|
|
% [start.x, start.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
if abs(dir.x) + abs(dir.y) != 1 or (dir.x != 0 and dir.y != 0):
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Invalid direction vector for match line: (%d,%d)" % [dir.x, dir.y], "Match3"
|
|
|
|
|
"Invalid direction vector for match line: (%d,%d)" % [dir.x, dir.y],
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
@@ -206,7 +227,10 @@ func _get_match_line(start: Vector2i, dir: Vector2i) -> Array:
|
|
|
|
|
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):
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
@@ -233,7 +257,9 @@ func _clear_matches() -> void:
|
|
|
|
|
"""
|
|
|
|
|
# Check grid integrity
|
|
|
|
|
if not _validate_grid_integrity():
|
|
|
|
|
DebugManager.log_error("Grid integrity check failed in _clear_matches", "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid integrity check failed in _clear_matches", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
var match_groups := []
|
|
|
|
|
@@ -282,7 +308,9 @@ func _clear_matches() -> void:
|
|
|
|
|
else:
|
|
|
|
|
match_score = match_size + max(0, match_size - 2) # n + (n-2) for n >= 4
|
|
|
|
|
total_score += match_score
|
|
|
|
|
print("Debug: Match of ", match_size, " gems = ", match_score, " points")
|
|
|
|
|
print(
|
|
|
|
|
"Debug: Match of ", match_size, " gems = ", match_score, " points"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Remove duplicates from all matches combined
|
|
|
|
|
var to_clear := []
|
|
|
|
|
@@ -308,7 +336,9 @@ func _clear_matches() -> void:
|
|
|
|
|
|
|
|
|
|
# Validate tile has grid_position property
|
|
|
|
|
if not "grid_position" in tile:
|
|
|
|
|
DebugManager.log_warn("Tile missing grid_position during removal", "Match3")
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
"Tile missing grid_position during removal", "Match3"
|
|
|
|
|
)
|
|
|
|
|
tile.queue_free()
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
@@ -322,7 +352,10 @@ func _clear_matches() -> void:
|
|
|
|
|
grid[tile_pos.y][tile_pos.x] = null
|
|
|
|
|
else:
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
"Invalid grid position during tile removal: (%d,%d)" % [tile_pos.x, tile_pos.y],
|
|
|
|
|
(
|
|
|
|
|
"Invalid grid position during tile removal: (%d,%d)"
|
|
|
|
|
% [tile_pos.x, tile_pos.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -358,7 +391,9 @@ func _drop_tiles():
|
|
|
|
|
func _fill_empty_cells():
|
|
|
|
|
# Safety check for grid integrity
|
|
|
|
|
if not _validate_grid_integrity():
|
|
|
|
|
DebugManager.log_error("Grid integrity check failed in _fill_empty_cells", "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid integrity check failed in _fill_empty_cells", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Create gem pool for current tile types
|
|
|
|
|
@@ -374,14 +409,17 @@ func _fill_empty_cells():
|
|
|
|
|
|
|
|
|
|
for x in range(grid_size.x):
|
|
|
|
|
if x >= grid[y].size():
|
|
|
|
|
DebugManager.log_error("Grid column %d does not exist in row %d" % [x, y], "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid column %d does not exist in row %d" % [x, y], "Match3"
|
|
|
|
|
)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if not grid[y][x]:
|
|
|
|
|
var tile = TILE_SCENE.instantiate()
|
|
|
|
|
if not tile:
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Failed to instantiate tile at (%d,%d)" % [x, y], "Match3"
|
|
|
|
|
"Failed to instantiate tile at (%d,%d)" % [x, y],
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
@@ -393,13 +431,17 @@ func _fill_empty_cells():
|
|
|
|
|
if tile.has_method("set_active_gem_types"):
|
|
|
|
|
tile.set_active_gem_types(gem_indices)
|
|
|
|
|
else:
|
|
|
|
|
DebugManager.log_warn("Tile missing set_active_gem_types method", "Match3")
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
"Tile missing set_active_gem_types method", "Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Set random tile type with bounds checking
|
|
|
|
|
if tile_types > 0:
|
|
|
|
|
tile.tile_type = randi() % tile_types
|
|
|
|
|
else:
|
|
|
|
|
DebugManager.log_error("tile_types is 0, cannot set tile type", "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"tile_types is 0, cannot set tile type", "Match3"
|
|
|
|
|
)
|
|
|
|
|
tile.queue_free()
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
@@ -408,7 +450,9 @@ func _fill_empty_cells():
|
|
|
|
|
if tile.has_signal("tile_selected"):
|
|
|
|
|
tile.tile_selected.connect(_on_tile_selected)
|
|
|
|
|
else:
|
|
|
|
|
DebugManager.log_warn("Tile missing tile_selected signal", "Match3")
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
"Tile missing tile_selected signal", "Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
tiles_created += 1
|
|
|
|
|
|
|
|
|
|
@@ -423,12 +467,15 @@ func _fill_empty_cells():
|
|
|
|
|
iteration += 1
|
|
|
|
|
|
|
|
|
|
if iteration >= MAX_CASCADE_ITERATIONS:
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
(
|
|
|
|
|
"Maximum cascade iterations reached (%d), stopping to prevent infinite loop"
|
|
|
|
|
% MAX_CASCADE_ITERATIONS
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
(
|
|
|
|
|
DebugManager
|
|
|
|
|
. log_warn(
|
|
|
|
|
(
|
|
|
|
|
"Maximum cascade iterations reached (%d), stopping to prevent infinite loop"
|
|
|
|
|
% MAX_CASCADE_ITERATIONS
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Save grid state after cascades complete
|
|
|
|
|
@@ -444,20 +491,27 @@ func regenerate_grid():
|
|
|
|
|
or grid_size.y > MAX_GRID_SIZE
|
|
|
|
|
):
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Invalid grid size for regeneration: %dx%d" % [grid_size.x, grid_size.y], "Match3"
|
|
|
|
|
(
|
|
|
|
|
"Invalid grid size for regeneration: %dx%d"
|
|
|
|
|
% [grid_size.x, grid_size.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if tile_types < 3 or tile_types > MAX_TILE_TYPES:
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Invalid tile types count for regeneration: %d" % tile_types, "Match3"
|
|
|
|
|
"Invalid tile types count for regeneration: %d" % tile_types,
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Use time-based seed to ensure different patterns each time
|
|
|
|
|
var new_seed = Time.get_ticks_msec()
|
|
|
|
|
seed(new_seed)
|
|
|
|
|
DebugManager.log_debug("Regenerating grid with seed: " + str(new_seed), "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Regenerating grid with seed: " + str(new_seed), "Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Safe tile cleanup with improved error handling
|
|
|
|
|
var children_to_remove = []
|
|
|
|
|
@@ -477,7 +531,9 @@ func regenerate_grid():
|
|
|
|
|
children_to_remove.append(child)
|
|
|
|
|
removed_count += 1
|
|
|
|
|
|
|
|
|
|
DebugManager.log_debug("Found %d tile children to remove" % removed_count, "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Found %d tile children to remove" % removed_count, "Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# First clear grid array references to prevent access to nodes being freed
|
|
|
|
|
for y in range(grid.size()):
|
|
|
|
|
@@ -508,20 +564,30 @@ func regenerate_grid():
|
|
|
|
|
func set_tile_types(new_count: int):
|
|
|
|
|
# Input validation
|
|
|
|
|
if new_count < 3:
|
|
|
|
|
DebugManager.log_error("Tile types count too low: %d (minimum 3)" % new_count, "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Tile types count too low: %d (minimum 3)" % new_count, "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if new_count > MAX_TILE_TYPES:
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Tile types count too high: %d (maximum %d)" % [new_count, MAX_TILE_TYPES], "Match3"
|
|
|
|
|
(
|
|
|
|
|
"Tile types count too high: %d (maximum %d)"
|
|
|
|
|
% [new_count, MAX_TILE_TYPES]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if new_count == tile_types:
|
|
|
|
|
DebugManager.log_debug("Tile types count unchanged, skipping regeneration", "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Tile types count unchanged, skipping regeneration", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
DebugManager.log_debug("Changing tile types from %d to %d" % [tile_types, new_count], "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Changing tile types from %d to %d" % [tile_types, new_count], "Match3"
|
|
|
|
|
)
|
|
|
|
|
tile_types = new_count
|
|
|
|
|
|
|
|
|
|
# Regenerate grid with new tile types (gem pool is updated in regenerate_grid)
|
|
|
|
|
@@ -551,10 +617,14 @@ func set_grid_size(new_size: Vector2i):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if new_size == grid_size:
|
|
|
|
|
DebugManager.log_debug("Grid size unchanged, skipping regeneration", "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Grid size unchanged, skipping regeneration", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
DebugManager.log_debug("Changing grid size from %s to %s" % [grid_size, new_size], "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Changing grid size from %s to %s" % [grid_size, new_size], "Match3"
|
|
|
|
|
)
|
|
|
|
|
grid_size = new_size
|
|
|
|
|
|
|
|
|
|
# Regenerate grid with new size
|
|
|
|
|
@@ -577,13 +647,19 @@ func reset_all_visual_states() -> void:
|
|
|
|
|
|
|
|
|
|
func _debug_scene_structure() -> void:
|
|
|
|
|
DebugManager.log_debug("=== Scene Structure Debug ===", "Match3")
|
|
|
|
|
DebugManager.log_debug("Match3 node children count: %d" % get_child_count(), "Match3")
|
|
|
|
|
DebugManager.log_debug("Match3 global position: %s" % global_position, "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Match3 node children count: %d" % get_child_count(), "Match3"
|
|
|
|
|
)
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Match3 global position: %s" % global_position, "Match3"
|
|
|
|
|
)
|
|
|
|
|
DebugManager.log_debug("Match3 scale: %s" % scale, "Match3")
|
|
|
|
|
|
|
|
|
|
# Check if grid is properly initialized
|
|
|
|
|
if not grid or grid.size() == 0:
|
|
|
|
|
DebugManager.log_error("Grid not initialized when debug structure called", "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid not initialized when debug structure called", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Check tiles
|
|
|
|
|
@@ -593,23 +669,33 @@ func _debug_scene_structure() -> void:
|
|
|
|
|
if y < grid.size() and x < grid[y].size() and grid[y][x]:
|
|
|
|
|
tile_count += 1
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Created %d tiles out of %d expected" % [tile_count, grid_size.x * grid_size.y], "Match3"
|
|
|
|
|
(
|
|
|
|
|
"Created %d tiles out of %d expected"
|
|
|
|
|
% [tile_count, grid_size.x * grid_size.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Check first tile in detail
|
|
|
|
|
if grid.size() > 0 and grid[0].size() > 0 and grid[0][0]:
|
|
|
|
|
var first_tile = grid[0][0]
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"First tile global position: %s" % first_tile.global_position, "Match3"
|
|
|
|
|
"First tile global position: %s" % first_tile.global_position,
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"First tile local position: %s" % first_tile.position, "Match3"
|
|
|
|
|
)
|
|
|
|
|
DebugManager.log_debug("First tile local position: %s" % first_tile.position, "Match3")
|
|
|
|
|
|
|
|
|
|
# Check parent chain
|
|
|
|
|
var current_node = self
|
|
|
|
|
var depth = 0
|
|
|
|
|
while current_node and depth < 10:
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Parent level %d: %s (type: %s)" % [depth, current_node.name, current_node.get_class()],
|
|
|
|
|
(
|
|
|
|
|
"Parent level %d: %s (type: %s)"
|
|
|
|
|
% [depth, current_node.name, current_node.get_class()]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
current_node = current_node.get_parent()
|
|
|
|
|
@@ -618,16 +704,27 @@ func _debug_scene_structure() -> void:
|
|
|
|
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
|
|
|
# Debug key to reset all visual states
|
|
|
|
|
if event.is_action_pressed("action_east") and DebugManager.is_debug_enabled():
|
|
|
|
|
if (
|
|
|
|
|
event.is_action_pressed("action_east")
|
|
|
|
|
and DebugManager.is_debug_enabled()
|
|
|
|
|
):
|
|
|
|
|
reset_all_visual_states()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if current_state == GameState.SWAPPING or current_state == GameState.PROCESSING:
|
|
|
|
|
if (
|
|
|
|
|
current_state == GameState.SWAPPING
|
|
|
|
|
or current_state == GameState.PROCESSING
|
|
|
|
|
):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Handle action_east (B button/ESC) to deselect selected tile
|
|
|
|
|
if event.is_action_pressed("action_east") and current_state == GameState.SELECTING:
|
|
|
|
|
DebugManager.log_debug("action_east pressed - deselecting current tile", "Match3")
|
|
|
|
|
if (
|
|
|
|
|
event.is_action_pressed("action_east")
|
|
|
|
|
and current_state == GameState.SELECTING
|
|
|
|
|
):
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"action_east pressed - deselecting current tile", "Match3"
|
|
|
|
|
)
|
|
|
|
|
_deselect_tile()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
@@ -652,18 +749,23 @@ func _input(event: InputEvent) -> void:
|
|
|
|
|
func _move_cursor(direction: Vector2i) -> void:
|
|
|
|
|
# Input validation for direction vector
|
|
|
|
|
if abs(direction.x) > 1 or abs(direction.y) > 1:
|
|
|
|
|
DebugManager.log_error("Invalid cursor direction vector: " + str(direction), "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Invalid cursor direction vector: " + str(direction), "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if direction.x != 0 and direction.y != 0:
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Diagonal cursor movement not supported: " + str(direction), "Match3"
|
|
|
|
|
"Diagonal cursor movement not supported: " + str(direction),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Validate grid integrity before cursor operations
|
|
|
|
|
if not _validate_grid_integrity():
|
|
|
|
|
DebugManager.log_error("Grid integrity check failed in cursor movement", "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid integrity check failed in cursor movement", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
var old_pos = cursor_position
|
|
|
|
|
@@ -676,19 +778,30 @@ func _move_cursor(direction: Vector2i) -> void:
|
|
|
|
|
if new_pos != cursor_position:
|
|
|
|
|
# Safe access to old tile
|
|
|
|
|
var old_tile = _safe_grid_access(old_pos)
|
|
|
|
|
if old_tile and "is_selected" in old_tile and "is_highlighted" in old_tile:
|
|
|
|
|
if (
|
|
|
|
|
old_tile
|
|
|
|
|
and "is_selected" in old_tile
|
|
|
|
|
and "is_highlighted" in old_tile
|
|
|
|
|
):
|
|
|
|
|
if not old_tile.is_selected:
|
|
|
|
|
old_tile.is_highlighted = false
|
|
|
|
|
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Cursor moved from (%d,%d) to (%d,%d)" % [old_pos.x, old_pos.y, new_pos.x, new_pos.y],
|
|
|
|
|
(
|
|
|
|
|
"Cursor moved from (%d,%d) to (%d,%d)"
|
|
|
|
|
% [old_pos.x, old_pos.y, new_pos.x, new_pos.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
cursor_position = new_pos
|
|
|
|
|
|
|
|
|
|
# Safe access to new tile
|
|
|
|
|
var new_tile = _safe_grid_access(cursor_position)
|
|
|
|
|
if new_tile and "is_selected" in new_tile and "is_highlighted" in new_tile:
|
|
|
|
|
if (
|
|
|
|
|
new_tile
|
|
|
|
|
and "is_selected" in new_tile
|
|
|
|
|
and "is_highlighted" in new_tile
|
|
|
|
|
):
|
|
|
|
|
if not new_tile.is_selected:
|
|
|
|
|
new_tile.is_highlighted = true
|
|
|
|
|
|
|
|
|
|
@@ -708,13 +821,19 @@ func _select_tile_at_cursor() -> void:
|
|
|
|
|
var tile = _safe_grid_access(cursor_position)
|
|
|
|
|
if tile:
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Keyboard selection at cursor (%d,%d)" % [cursor_position.x, cursor_position.y],
|
|
|
|
|
(
|
|
|
|
|
"Keyboard selection at cursor (%d,%d)"
|
|
|
|
|
% [cursor_position.x, cursor_position.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
_on_tile_selected(tile)
|
|
|
|
|
else:
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
"No valid tile at cursor position (%d,%d)" % [cursor_position.x, cursor_position.y],
|
|
|
|
|
(
|
|
|
|
|
"No valid tile at cursor position (%d,%d)"
|
|
|
|
|
% [cursor_position.x, cursor_position.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -728,9 +847,15 @@ func _on_tile_selected(tile: Node2D) -> void:
|
|
|
|
|
SELECTING -> SWAPPING: Different tile clicked (attempt swap)
|
|
|
|
|
"""
|
|
|
|
|
# Block tile selection during busy states
|
|
|
|
|
if current_state == GameState.SWAPPING or current_state == GameState.PROCESSING:
|
|
|
|
|
if (
|
|
|
|
|
current_state == GameState.SWAPPING
|
|
|
|
|
or current_state == GameState.PROCESSING
|
|
|
|
|
):
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Tile selection ignored - game busy (state: %s)" % [GameState.keys()[current_state]],
|
|
|
|
|
(
|
|
|
|
|
"Tile selection ignored - game busy (state: %s)"
|
|
|
|
|
% [GameState.keys()[current_state]]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
@@ -773,7 +898,11 @@ func _select_tile(tile: Node2D) -> void:
|
|
|
|
|
tile.is_selected = true
|
|
|
|
|
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"
|
|
|
|
|
(
|
|
|
|
|
"Selected tile at (%d, %d)"
|
|
|
|
|
% [tile.grid_position.x, tile.grid_position.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -784,12 +913,17 @@ func _deselect_tile() -> void:
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
(
|
|
|
|
|
"Deselecting tile at (%d,%d)"
|
|
|
|
|
% [selected_tile.grid_position.x, selected_tile.grid_position.y]
|
|
|
|
|
% [
|
|
|
|
|
selected_tile.grid_position.x,
|
|
|
|
|
selected_tile.grid_position.y
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
DebugManager.log_debug("Deselecting tile (no grid position available)", "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Deselecting tile (no grid position available)", "Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Safe property access for selection state
|
|
|
|
|
if "is_selected" in selected_tile:
|
|
|
|
|
@@ -833,7 +967,9 @@ func _are_adjacent(tile1: Node2D, tile2: Node2D) -> bool:
|
|
|
|
|
|
|
|
|
|
func _attempt_swap(tile1: Node2D, tile2: Node2D) -> void:
|
|
|
|
|
if not _are_adjacent(tile1, tile2):
|
|
|
|
|
DebugManager.log_debug("Tiles are not adjacent, selecting new tile instead", "Match3")
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Tiles are not adjacent, selecting new tile instead", "Match3"
|
|
|
|
|
)
|
|
|
|
|
_deselect_tile()
|
|
|
|
|
_select_tile(tile2)
|
|
|
|
|
return
|
|
|
|
|
@@ -886,7 +1022,9 @@ func _attempt_swap(tile1: Node2D, tile2: Node2D) -> void:
|
|
|
|
|
|
|
|
|
|
func _swap_tiles(tile1: Node2D, tile2: Node2D) -> void:
|
|
|
|
|
if not tile1 or not tile2:
|
|
|
|
|
DebugManager.log_error("Cannot swap tiles - one or both tiles are null", "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Cannot swap tiles - one or both tiles are null", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Update grid positions
|
|
|
|
|
@@ -934,7 +1072,9 @@ func serialize_grid_state() -> Array:
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if grid.size() == 0:
|
|
|
|
|
DebugManager.log_error("Grid array is empty during serialization!", "Match3")
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid array is empty during serialization!", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
var serialized_grid = []
|
|
|
|
|
@@ -950,7 +1090,11 @@ func serialize_grid_state() -> Array:
|
|
|
|
|
# Only log first few for brevity
|
|
|
|
|
if valid_tiles <= 5:
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Serializing tile (%d,%d): type %d" % [x, y, grid[y][x].tile_type], "Match3"
|
|
|
|
|
(
|
|
|
|
|
"Serializing tile (%d,%d): type %d"
|
|
|
|
|
% [x, y, grid[y][x].tile_type]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
row.append(-1) # Invalid/empty tile
|
|
|
|
|
@@ -958,7 +1102,8 @@ func serialize_grid_state() -> Array:
|
|
|
|
|
# Only log first few nulls for brevity
|
|
|
|
|
if null_tiles <= 5:
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Serializing tile (%d,%d): NULL/empty (-1)" % [x, y], "Match3"
|
|
|
|
|
"Serializing tile (%d,%d): NULL/empty (-1)" % [x, y],
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
serialized_grid.append(row)
|
|
|
|
|
|
|
|
|
|
@@ -1002,7 +1147,9 @@ func save_current_state():
|
|
|
|
|
func load_saved_state() -> bool:
|
|
|
|
|
# Check if there's a saved grid state
|
|
|
|
|
if not SaveManager.has_saved_grid():
|
|
|
|
|
DebugManager.log_info("No saved grid state found, using default generation", "Match3")
|
|
|
|
|
DebugManager.log_info(
|
|
|
|
|
"No saved grid state found, using default generation", "Match3"
|
|
|
|
|
)
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
var saved_state = SaveManager.get_saved_grid_state()
|
|
|
|
|
@@ -1015,12 +1162,21 @@ func load_saved_state() -> bool:
|
|
|
|
|
saved_gems.append(int(gem))
|
|
|
|
|
var saved_layout = saved_state.grid_layout
|
|
|
|
|
|
|
|
|
|
DebugManager.log_info(
|
|
|
|
|
(
|
|
|
|
|
"[%s] Loading saved grid state: size(%d,%d), %d tile types, layout_size=%d"
|
|
|
|
|
% [instance_id, saved_size.x, saved_size.y, tile_types, saved_layout.size()]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
(
|
|
|
|
|
DebugManager
|
|
|
|
|
. log_info(
|
|
|
|
|
(
|
|
|
|
|
"[%s] Loading saved grid state: size(%d,%d), %d tile types, layout_size=%d"
|
|
|
|
|
% [
|
|
|
|
|
instance_id,
|
|
|
|
|
saved_size.x,
|
|
|
|
|
saved_size.y,
|
|
|
|
|
tile_types,
|
|
|
|
|
saved_layout.size()
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Debug: Print first few rows of loaded layout
|
|
|
|
|
@@ -1058,7 +1214,10 @@ func load_saved_state() -> bool:
|
|
|
|
|
# Recalculate layout if size changed
|
|
|
|
|
if old_size != saved_size:
|
|
|
|
|
DebugManager.log_info(
|
|
|
|
|
"Grid size changed from %s to %s, recalculating layout" % [old_size, saved_size],
|
|
|
|
|
(
|
|
|
|
|
"Grid size changed from %s to %s, recalculating layout"
|
|
|
|
|
% [old_size, saved_size]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
_calculate_grid_layout()
|
|
|
|
|
@@ -1069,7 +1228,9 @@ func load_saved_state() -> bool:
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _restore_grid_from_layout(grid_layout: Array, active_gems: Array[int]) -> void:
|
|
|
|
|
func _restore_grid_from_layout(
|
|
|
|
|
grid_layout: Array, active_gems: Array[int]
|
|
|
|
|
) -> void:
|
|
|
|
|
DebugManager.log_info(
|
|
|
|
|
(
|
|
|
|
|
"[%s] Starting grid restoration: layout_size=%d, active_gems=%s"
|
|
|
|
|
@@ -1088,7 +1249,8 @@ func _restore_grid_from_layout(grid_layout: Array, active_gems: Array[int]) -> v
|
|
|
|
|
all_tile_children.append(child)
|
|
|
|
|
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"Found %d existing tile children to remove" % all_tile_children.size(), "Match3"
|
|
|
|
|
"Found %d existing tile children to remove" % all_tile_children.size(),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Remove all found tile children
|
|
|
|
|
@@ -1133,17 +1295,24 @@ func _restore_grid_from_layout(grid_layout: Array, active_gems: Array[int]) -> v
|
|
|
|
|
if saved_tile_type >= 0 and saved_tile_type < tile_types:
|
|
|
|
|
tile.tile_type = saved_tile_type
|
|
|
|
|
DebugManager.log_debug(
|
|
|
|
|
"✓ Restored tile (%d,%d) with saved type %d" % [x, y, saved_tile_type], "Match3"
|
|
|
|
|
(
|
|
|
|
|
"✓ Restored tile (%d,%d) with saved type %d"
|
|
|
|
|
% [x, y, saved_tile_type]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
# Fallback for invalid tile types
|
|
|
|
|
tile.tile_type = randi() % tile_types
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
(
|
|
|
|
|
"✗ Invalid saved tile type %d at (%d,%d), using random %d"
|
|
|
|
|
% [saved_tile_type, x, y, tile.tile_type]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
(
|
|
|
|
|
DebugManager
|
|
|
|
|
. log_error(
|
|
|
|
|
(
|
|
|
|
|
"✗ Invalid saved tile type %d at (%d,%d), using random %d"
|
|
|
|
|
% [saved_tile_type, x, y, tile.tile_type]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Connect tile signals
|
|
|
|
|
@@ -1151,13 +1320,22 @@ func _restore_grid_from_layout(grid_layout: Array, active_gems: Array[int]) -> v
|
|
|
|
|
grid[y].append(tile)
|
|
|
|
|
|
|
|
|
|
DebugManager.log_info(
|
|
|
|
|
"Completed grid restoration: %d tiles restored" % [grid_size.x * grid_size.y], "Match3"
|
|
|
|
|
(
|
|
|
|
|
"Completed grid restoration: %d tiles restored"
|
|
|
|
|
% [grid_size.x * grid_size.y]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Safety and validation helper functions
|
|
|
|
|
func _is_valid_grid_position(pos: Vector2i) -> bool:
|
|
|
|
|
return pos.x >= 0 and pos.y >= 0 and pos.x < grid_size.x and pos.y < grid_size.y
|
|
|
|
|
return (
|
|
|
|
|
pos.x >= 0
|
|
|
|
|
and pos.y >= 0
|
|
|
|
|
and pos.x < grid_size.x
|
|
|
|
|
and pos.y < grid_size.y
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _validate_grid_integrity() -> bool:
|
|
|
|
|
@@ -1168,7 +1346,8 @@ func _validate_grid_integrity() -> bool:
|
|
|
|
|
|
|
|
|
|
if grid.size() != grid_size.y:
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid height mismatch: %d vs %d" % [grid.size(), grid_size.y], "Match3"
|
|
|
|
|
"Grid height mismatch: %d vs %d" % [grid.size(), grid_size.y],
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
@@ -1179,7 +1358,11 @@ func _validate_grid_integrity() -> bool:
|
|
|
|
|
|
|
|
|
|
if grid[y].size() != grid_size.x:
|
|
|
|
|
DebugManager.log_error(
|
|
|
|
|
"Grid row %d width mismatch: %d vs %d" % [y, grid[y].size(), grid_size.x], "Match3"
|
|
|
|
|
(
|
|
|
|
|
"Grid row %d width mismatch: %d vs %d"
|
|
|
|
|
% [y, grid[y].size(), grid_size.x]
|
|
|
|
|
),
|
|
|
|
|
"Match3"
|
|
|
|
|
)
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
@@ -1192,7 +1375,9 @@ func _safe_grid_access(pos: Vector2i) -> Node2D:
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
if pos.y >= grid.size() or pos.x >= grid[pos.y].size():
|
|
|
|
|
DebugManager.log_warn("Grid bounds exceeded: (%d,%d)" % [pos.x, pos.y], "Match3")
|
|
|
|
|
DebugManager.log_warn(
|
|
|
|
|
"Grid bounds exceeded: (%d,%d)" % [pos.x, pos.y], "Match3"
|
|
|
|
|
)
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
var tile = grid[pos.y][pos.x]
|
|
|
|
|
|