97 lines
3.4 KiB
C#
97 lines
3.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class BoardFitter : MonoBehaviour
|
|
{
|
|
[Header("Рамка")]
|
|
[SerializeField] private SpriteRenderer boardRenderer;
|
|
|
|
[Header("Задний фон")]
|
|
[SerializeField] private SpriteRenderer backgroundRenderer;
|
|
[SerializeField] private float bgHorizontalPadding = 0.78f; //лево+право
|
|
[SerializeField] private float bgVerticalPadding = 0.68f; //верх+низ
|
|
|
|
[Header("Ячейки")]
|
|
[SerializeField] private GameObject cellPrefab;
|
|
[SerializeField] private Transform cellsRoot;
|
|
[SerializeField] private float cellScale = 1;
|
|
[SerializeField] private float cellOffset = 0.1f;
|
|
[SerializeField] private float cellGroupPadding = 3f;//2 стороны сразу
|
|
private List<GameObject> cells;
|
|
|
|
[Header("Подготовленная позциця")]
|
|
[SerializeField] private float stickedOffset = 2;
|
|
|
|
public BoardConfiguration GetConfiguration()
|
|
{
|
|
if (cells == null)
|
|
throw new System.NullReferenceException($"Объект не инициализирован {this}");
|
|
|
|
Vector2 offset = new Vector2(cellScale + cellOffset, -(cellScale + cellOffset));
|
|
|
|
return new BoardConfiguration(topLeft: cells[0].transform,
|
|
cellOffset: offset,
|
|
stickedYOffset: stickedOffset);
|
|
}
|
|
|
|
public void FitBoard(int columns, int rows)
|
|
{
|
|
columns = Mathf.Clamp(columns, 4, 8);
|
|
rows = Mathf.Clamp(rows, 4, 8);
|
|
|
|
ClearCells();
|
|
GenerateCells(columns, rows);
|
|
|
|
float groupWidth = (cellScale + cellOffset) * columns;
|
|
float groupHeight = (cellScale + cellOffset) * rows;
|
|
|
|
FitFrame(groupWidth, groupHeight);
|
|
FitCellsRoot(groupWidth, groupHeight);
|
|
}
|
|
|
|
private void FitCellsRoot(float groupWidth, float groupHeight)
|
|
{
|
|
float rootX = -groupWidth / 2;
|
|
float rootY = (groupHeight + boardRenderer.size.y - cellScale) / 2;
|
|
cellsRoot.localPosition = new Vector3(rootX, rootY, 0);
|
|
}
|
|
|
|
private void FitFrame(float groupWidth, float groupHeight)
|
|
{
|
|
boardRenderer.size = new Vector2(groupWidth + cellGroupPadding, groupHeight + cellGroupPadding);
|
|
backgroundRenderer.transform.localScale = new Vector3(boardRenderer.size.x - bgHorizontalPadding,
|
|
boardRenderer.size.y - bgVerticalPadding,
|
|
0);
|
|
}
|
|
|
|
private void GenerateCells(int columns, int rows)
|
|
{
|
|
cells = new List<GameObject>();
|
|
for (int row = 0; row < rows; row++)
|
|
{
|
|
for (int column = 0; column < columns; column++)
|
|
{
|
|
var cellGO = Instantiate(cellPrefab, cellsRoot);
|
|
var cell = cellGO.transform;
|
|
|
|
cell.localScale = new Vector3(cellScale, cellScale, 1);
|
|
|
|
float x = column * (cellScale + cellOffset) + cellScale / 2;
|
|
float y = -row * (cellScale + cellOffset) - cellScale / 2;
|
|
cell.localPosition = new Vector3(x, y, 0);
|
|
|
|
cells.Add(cellGO);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ClearCells()
|
|
{
|
|
for (int i = cellsRoot.childCount - 1; i >= 0; i--)
|
|
{
|
|
var cell = cellsRoot.GetChild(i).gameObject;
|
|
Destroy(cell);
|
|
}
|
|
}
|
|
}
|