73 lines
2.7 KiB
C#
73 lines
2.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class CardsSpawner : MonoBehaviour
|
|
{
|
|
[SerializeField] private PresetCards _presetCards = null;
|
|
[SerializeField] private Card _cardPrefub = null;
|
|
[SerializeField] private UnityEvent _startCollect = new UnityEvent();
|
|
[SerializeField] private Grid _grid = null;
|
|
[SerializeField] private LevelData _levelData = null; // Для чтения MaxPlayCards
|
|
|
|
public void Spawn()
|
|
{
|
|
// Очистка старых карт
|
|
foreach (Transform child in transform)
|
|
{
|
|
Destroy(child.gameObject);
|
|
}
|
|
|
|
Transform localTransform = transform;
|
|
Card card;
|
|
Sprite backSprite = _presetCards.GetBackSprite();
|
|
|
|
int uniqueCards = _levelData.MaxPlayCards; // 4,6,8
|
|
_presetCards.SetCardMax(uniqueCards);
|
|
|
|
List<Sprite> playCardsSprites = _presetCards.GetPlayCardsSprites();
|
|
|
|
// Полный shuffled список индексов (2 копии)
|
|
List<int> allIndices = new List<int>();
|
|
for (int k = 0; k < 2; k++)
|
|
{
|
|
int[] oneSet = _presetCards.GetCardIndex();
|
|
allIndices.AddRange(oneSet);
|
|
}
|
|
for (int i = 0; i < allIndices.Count; i++)
|
|
{
|
|
int temp = allIndices[i];
|
|
int rnd = Random.Range(0, allIndices.Count);
|
|
allIndices[i] = allIndices[rnd];
|
|
allIndices[rnd] = temp;
|
|
}
|
|
|
|
float scale = _grid.GetScale();
|
|
float offsetX = _grid.OffsetX * scale;
|
|
float offsetsY = _grid.OffsetsY * scale;
|
|
float positionXStart = _grid.GetPositionX() * scale + localTransform.position.x;
|
|
float positionY = _grid.GetPositionY() * scale + localTransform.position.y; // Старт верхнего ряда
|
|
|
|
int rows = _grid.GetRows(); // 2
|
|
int columns = _grid.GetColumnsCount(); // 4,6,8
|
|
int indexCounter = 0;
|
|
|
|
for (int row = 0; row < rows; row++)
|
|
{
|
|
float positionX = positionXStart;
|
|
for (int col = 0; col < columns; col++)
|
|
{
|
|
if (indexCounter >= allIndices.Count) break;
|
|
|
|
card = Instantiate(_cardPrefub, new Vector3(positionX, positionY, 0), Quaternion.identity);
|
|
card.transform.parent = localTransform;
|
|
card.transform.localScale = Vector3.one * scale;
|
|
card.CardSettings(backSprite, playCardsSprites[allIndices[indexCounter]], allIndices[indexCounter]);
|
|
positionX += offsetX;
|
|
indexCounter++;
|
|
}
|
|
positionY -= offsetsY; // Переход к нижнему ряду
|
|
}
|
|
_startCollect.Invoke();
|
|
}
|
|
} |