using System; using System.Collections.Generic; using UnityEngine; public class Board : MonoBehaviour { //визуал [SerializeField] Transform topLeftCell; [SerializeField] ChipPointer pointer; [SerializeField] Vector2 cellOffset = new(1.444f, 1.48f); [SerializeField] float stickedY = 5f; //состояние public int columns; public int rows; public Chip[,] grid; public int MovesCount => GetMovesCount(); public void Init(int columns, int rows) { this.columns = columns; this.rows = rows; MakeClearField(); } public Vector2 GetStickedPosition(int column) => new Vector2(GetXPosition(column), stickedY); public Vector2 GetTargetPosition(int column) => new Vector2(GetXPosition(column), GetYPosition(column)); public void MakeClearField() { PerformClear(); grid = new Chip[columns, rows]; } public bool CanDrop(int column) => grid[column, 0] == null; public void AttachChipToGrid(Chip chip, int column, int row) { if (column < 0 || column >= columns) { Debug.LogError($"Попытка поставить фишку в колонку {column}. Колонок: {columns}"); return; } if (row < 0 || row >= rows) { Debug.LogError($"Попытка поставить фишку в ряд {row}. Рядов: {rows}"); return; } grid[column, row] = chip; } public int[] GetNonFullColumns() { List temp = new(); for (int column = 0; column < columns; column++) if (CanDrop(column)) temp.Add(column); return temp.ToArray(); } public int GetLowestEmptyRow(int colIndex) { if (colIndex < 0 || colIndex >= columns) throw new ArgumentOutOfRangeException($"Попытка проверить колонку {colIndex}. Колонок: {columns}"); for (int row = rows - 1; row >= 0; row--) if (grid[colIndex, row] == null) return row; return -1; } private float GetXPosition(int column) => topLeftCell.position.x + cellOffset.x * column; private float GetYPosition(int column) => topLeftCell.position.y - GetLowestEmptyRow(column) * cellOffset.y; private void PerformClear() { SoundManager.instance.playExplosion(); GameManager.instance.currentChip?.RemoveFromField(); if (grid == null) return; foreach (Chip chip in grid) chip?.RemoveFromField(); } private int GetMovesCount() { if (grid == null) return 0; int count = 0; foreach (Chip chip in grid) if (chip != null) count++; return count; } public CellState[,] GridToTeam() { CellState[,] res = new CellState[columns, rows]; for (int col = 0; col < columns; col++) { for (int row = 0; row < rows; row++) { var chip = grid[col, row]; if (chip == null) res[col, row] = CellState.Empty; else res[col, row] = chip.Team; } } return res; } }