class_name TileManager extends Node var active_tiles: Dictionary = {} # location: Tile var board_flow: FlowContainer var game: ChessGame const PortalTile = preload("res://Systems/Tiles/Portal.gd") func _init(flow: FlowContainer = null, chess_game: ChessGame = null) -> void: if flow: initialize(flow) game = chess_game func initialize(flow: FlowContainer) -> void: board_flow = flow clear_tiles() func add_tile(location: String, tile: Tile) -> void: if !board_flow: push_error("TileManager not initialized with board_flow") return var container = board_flow.get_node_or_null(location) as PieceContainer if !container: push_error("Container not found at location: " + location) return # Remove any existing tile remove_tile(location) tile.game = game # Add new tile active_tiles[location] = tile tile.effect_expired.connect(func(): remove_tile(location)) tile.update_appearance() func remove_tile(location: String) -> void: if active_tiles.has(location): var tile = active_tiles[location] tile.restore_base_appearance() active_tiles.erase(location) func get_tile(location: String) -> Tile: return active_tiles.get(location, null) func update_tile_durations() -> void: if active_tiles.is_empty(): return var expired_locations = [] var locations = active_tiles.keys() for location in locations: var tile = active_tiles[location] tile.update_duration() if !tile.is_effect_active(): expired_locations.append(location) for location in expired_locations: remove_tile(location) func apply_tile_effects() -> void: if active_tiles.is_empty(): return for location in active_tiles: var tile = active_tiles[location] var container = board_flow.get_node(location) as PieceContainer if container && container.has_piece(): var piece = container.get_piece() if tile.can_affect_piece(piece): tile.apply_effect(piece) func clear_tiles() -> void: var locations = active_tiles.keys() for location in locations: remove_tile(location) # Function to place random game tiles at the start of each match func place_random_game_tiles(num_tiles: int = 6) -> void: if !board_flow: push_error("TileManager not initialized with board_flow") return var available_positions = [] for x in range(game.boardXSize): for y in range(game.boardYSize): if y > 1 && y < 6: # Don't place on starting rows available_positions.append(str(x) + "-" + str(y)) available_positions.shuffle() for i in range(min(num_tiles, available_positions.size())): var pos = available_positions[i] var container = board_flow.get_node(pos) as PieceContainer if !container: continue var is_white = (int(pos.split("-")[0]) + int(pos.split("-")[1])) % 2 == 0 # Randomly choose a tile type var rng = RandomNumberGenerator.new() rng.randomize() var tile_type = rng.randi() % 3 var tile: Tile match tile_type: 0: # Wall tile # tile = WallTile.new(container, is_white, -1) # add_tile(pos, tile) continue 1: # Double Wall # tile = DoubleWallTile.new(container, is_white, -1) # add_tile(pos, tile) continue 2: # Portal pair # Only create portal pair if this isn't the last tile if i < available_positions.size() - 1: var next_pos = available_positions[i + 1] var next_container = board_flow.get_node(next_pos) as PieceContainer if next_container: var next_is_white = (int(next_pos.split("-")[0]) + int(next_pos.split("-")[1])) % 2 == 0 # Create portal pair with alternating blue/orange colors # var portal_color = Color.ORANGE if (i % 2 == 0) else Color.BLUE var portals = PortalTile.create_portal_pair( container, is_white, next_container, next_is_white, -1, # permanent duration i, # pair id Color.ORANGE, Color.BLUE ) # Add both portals add_tile(pos, portals[0]) add_tile(next_pos, portals[1]) # Skip the next position since we used it for the portal pair i += 1 else: # If we can't make a pair, fall back to a wall tile tile = WallTile.new(container, is_white, -1) add_tile(pos, tile) # func place_portal_pair(location1: String, location2: String, duration: int = -1) -> void: # var container1 = board_flow.get_node(location1) as PieceContainer # var container2 = board_flow.get_node(location2) as PieceContainer # if container1 && container2: # var is_white1 = (int(location1.split("-")[0]) + int(location1.split("-")[1])) % 2 == 0 # var is_white2 = (int(location2.split("-")[0]) + int(location2.split("-")[1])) % 2 == 0 # # Generate a unique color for this pair # var portal_color = Color(randf(), randf(), 1.0) # Blue-ish random color # # Create portal pair # var portals = PortalTile.create_portal_pair( # container1, is_white1, # container2, is_white2, # duration, # active_tiles.size(), # Use current size as unique pair ID # portal_color # ) # # Add both portals to active tiles # add_tile(location1, portals[0]) # add_tile(location2, portals[1])