ChessBuilder/Systems/TileManager.gd

117 lines
No EOL
3.7 KiB
GDScript

class_name TileManager
extends Node
var active_tiles: Dictionary = {} # location: Tile
var board_flow: FlowContainer
var game: ChessGame
const DoubleMovementTile = preload("res://Systems/Tiles/DoubleMovement.gd")
const FireWallTile = preload("res://Systems/Tiles/FireWall.gd")
const PawnBoostTile = preload("res://Systems/Tiles/PawnBoost.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(8):
for y in range(8):
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: # DoubleMovementTile tile
tile = DoubleMovementTile.new(container, is_white, -1)
1: # FireWallTile
tile = DoubleMovementTile.new(container, is_white, -1)
2: # PawnBoostTile tile
tile = DoubleMovementTile.new(container, is_white, -1)
add_tile(pos, tile)