Unity_2lab/2/Assets/Scripts/Grid.cs

55 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
public class Grid : MonoBehaviour
{
public readonly float OffsetX = 2f; // Горизонтальный отступ (можно уменьшить до 1.8f, если наложение)
public readonly float OffsetsY = 3f; // Вертикальный отступ
[SerializeField] private LevelData _levelData = null;
private readonly float cardWidth = 2f; // Реальный width карты (из префаба, подставьте точный)
private readonly float cardHeight = 3f; // Реальный height
public int GetColumnsCount()
{
return _levelData.MaxPlayCards; // 4,6,8
}
public int GetRows()
{
return 2; // Фиксировано
}
public float GetPositionX()
{
int columns = GetColumnsCount();
return - (OffsetX * (columns - 1) / 2f); // Центр по X: для 4 = -3, для 6 = -5, для 8 = -7
}
public float GetPositionY()
{
return OffsetsY / 2f; // Центр по Y: верхний ряд на +1.5, нижний на -1.5 (для OffsetsY=3)
}
public float GetScale()
{
int columns = GetColumnsCount();
int rows = GetRows();
// Полный размер грида с учётом карт
float gridWidth = OffsetX * (columns - 1) + cardWidth;
float gridHeight = OffsetsY * (rows - 1) + cardHeight;
Camera mainCamera = Camera.main;
if (mainCamera == null) return 1f;
float aspect = mainCamera.aspect; // ~1.78
float orthSize = mainCamera.orthographicSize; // 5
float visibleWidth = 2f * orthSize * aspect; // ~17.8
float visibleHeight = 2f * orthSize; // 10
float padding = 0.8f; // 80% экрана для margins (можно 0.7 для большего запаса)
float scaleX = (visibleWidth * padding) / gridWidth;
float scaleY = (visibleHeight * padding) / gridHeight;
return Mathf.Min(1f, Mathf.Min(scaleX, scaleY)); // Scale <=1, чтобы влезло
}
}