extends Node2D var GRID_SIZE := Vector2i(8, 8) var TILE_TYPES := 5 const TILE_SCENE := preload("res://scenes/match3/tile.tscn") var grid := [] var tile_size: float = 48.0 var grid_offset: Vector2 func _ready(): # Set up initial gem pool var gem_indices = [] for i in range(TILE_TYPES): gem_indices.append(i) const TileScript = preload("res://scenes/match3/tile.gd") TileScript.set_active_gem_pool(gem_indices) _calculate_grid_layout() _initialize_grid() func _calculate_grid_layout(): var viewport_size = get_viewport().get_visible_rect().size var available_width = viewport_size.x * 0.8 # Use 80% of screen width var available_height = viewport_size.y * 0.7 # Use 70% of screen height # Calculate tile size based on available space var max_tile_width = available_width / GRID_SIZE.x var max_tile_height = available_height / GRID_SIZE.y tile_size = min(max_tile_width, max_tile_height) # Align grid to left side with some margin var total_grid_height = tile_size * GRID_SIZE.y grid_offset = Vector2( 50, # Left margin (viewport_size.y - total_grid_height) / 2 + 50 # Vertically centered with top margin ) func _initialize_grid(): # Update gem pool BEFORE creating any tiles var gem_indices = [] for i in range(TILE_TYPES): gem_indices.append(i) const TileScript = preload("res://scenes/match3/tile.gd") TileScript.set_active_gem_pool(gem_indices) for y in range(GRID_SIZE.y): grid.append([]) for x in range(GRID_SIZE.x): var tile = TILE_SCENE.instantiate() tile.position = grid_offset + Vector2(x, y) * tile_size tile.grid_position = Vector2i(x, y) add_child(tile) # Set tile type after adding to scene tree so sprite reference is available var new_type = randi() % TILE_TYPES tile.tile_type = new_type print("Debug: Created tile at (", x, ",", y, ") with type ", new_type) grid[y].append(tile) func _has_match_at(pos: Vector2i) -> bool: # Fixed: Add bounds and null checks if pos.x < 0 or pos.y < 0 or pos.x >= GRID_SIZE.x or pos.y >= GRID_SIZE.y: return false if not grid[pos.y][pos.x]: return false var matches_horizontal = _get_match_line(pos, Vector2i(1, 0)) if matches_horizontal.size() >= 3: return true var matches_vertical = _get_match_line(pos, Vector2i(0, 1)) return matches_vertical.size() >= 3 # Fixed: Add missing function to check for any matches on the board func _check_for_matches() -> bool: for y in range(GRID_SIZE.y): for x in range(GRID_SIZE.x): if _has_match_at(Vector2i(x, y)): return true return false func _get_match_line(start: Vector2i, dir: Vector2i) -> Array: var line = [grid[start.y][start.x]] var type = grid[start.y][start.x].tile_type # Fixed: Check in both directions separately to avoid infinite loops for offset in [1, -1]: var current = start + dir * offset while current.x >= 0 and current.y >= 0 and current.x < GRID_SIZE.x and current.y < GRID_SIZE.y: var neighbor = grid[current.y][current.x] # Fixed: Add null check to prevent crashes if not neighbor or neighbor.tile_type != type: break line.append(neighbor) current += dir * offset return line func _clear_matches(): var to_clear := [] for y in range(GRID_SIZE.y): for x in range(GRID_SIZE.x): # Fixed: Add null check before processing if not grid[y][x]: continue var pos = Vector2i(x, y) var horiz = _get_match_line(pos, Vector2i(1, 0)) if horiz.size() >= 3: to_clear.append_array(horiz) var vert = _get_match_line(pos, Vector2i(0, 1)) if vert.size() >= 3: to_clear.append_array(vert) # Remove duplicates using dictionary keys trick var unique_dict := {} for tile in to_clear: unique_dict[tile] = true to_clear = unique_dict.keys() # Fixed: Only proceed if there are matches to clear if to_clear.size() == 0: return for tile in to_clear: grid[tile.grid_position.y][tile.grid_position.x] = null tile.queue_free() _drop_tiles() await get_tree().create_timer(0.2).timeout _fill_empty_cells() func _drop_tiles(): var moved = true while moved: moved = false for x in range(GRID_SIZE.x): # Fixed: Start from GRID_SIZE.y - 1 to avoid out of bounds for y in range(GRID_SIZE.y - 1, -1, -1): var tile = grid[y][x] # Fixed: Check bounds before accessing y + 1 if tile and y + 1 < GRID_SIZE.y and not grid[y + 1][x]: grid[y + 1][x] = tile grid[y][x] = null tile.grid_position = Vector2i(x, y + 1) # You can animate position here using Tween for smooth drop: # tween.interpolate_property(tile, "position", tile.position, grid_offset + Vector2(x, y + 1) * tile_size, 0.2) tile.position = grid_offset + Vector2(x, y + 1) * tile_size moved = true func _fill_empty_cells(): for y in range(GRID_SIZE.y): for x in range(GRID_SIZE.x): if not grid[y][x]: var tile = TILE_SCENE.instantiate() tile.grid_position = Vector2i(x, y) tile.tile_type = randi() % TILE_TYPES tile.position = grid_offset + Vector2(x, y) * tile_size grid[y][x] = tile add_child(tile) # Fixed: Add recursion protection to prevent stack overflow await get_tree().create_timer(0.1).timeout var max_iterations = 10 var iteration = 0 while _check_for_matches() and iteration < max_iterations: _clear_matches() await get_tree().create_timer(0.1).timeout iteration += 1 func regenerate_grid(): # Use time-based seed to ensure different patterns each time var new_seed = Time.get_ticks_msec() seed(new_seed) print("Debug: Regenerating grid with seed: ", new_seed) # Fixed: Clear ALL tile children, not just ones in grid array # This handles any orphaned tiles that might not be tracked in the grid var children_to_remove = [] for child in get_children(): if child.has_method("get_script") and child.get_script(): var script_path = child.get_script().resource_path if script_path == "res://scenes/match3/tile.gd": children_to_remove.append(child) # Remove all found tile children for child in children_to_remove: child.free() # Also clear grid array references for y in range(grid.size()): if grid[y] and grid[y] is Array: for x in range(grid[y].size()): # Set to null since we already freed the nodes above grid[y][x] = null # Clear the grid array grid.clear() # No need to wait for nodes to be freed since we used free() # Fixed: Recalculate grid layout before regenerating tiles _calculate_grid_layout() # Regenerate the grid (gem pool is updated in _initialize_grid) _initialize_grid() func set_tile_types(new_count: int): TILE_TYPES = new_count # Regenerate grid with new tile types (gem pool is updated in regenerate_grid) await regenerate_grid() func set_grid_size(new_size: Vector2i): print("Debug: Changing grid size from ", GRID_SIZE, " to ", new_size) GRID_SIZE = new_size # Regenerate grid with new size await regenerate_grid()