using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.Serialization; public class CardsSpawner : MonoBehaviour { [SerializeField] private PresetCards _presetCards = null; [SerializeField] private Card _cardPrefub = null; [SerializeField] private UnityEvent _startCollect = new UnityEvent(); [FormerlySerializedAs("_grid")] [SerializeField] private Grid_Script gridScript = 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 playCardsSprites = _presetCards.GetPlayCardsSprites(); // Полный shuffled список индексов (2 копии) List allIndices = new List(); 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 = gridScript.GetScale(); float offsetX = Grid_Script.OffsetX * scale; float offsetsY = gridScript.OffsetsY * scale; float positionXStart = gridScript.GetPositionX() * scale + localTransform.position.x; float positionY = gridScript.GetPositionY() * scale + localTransform.position.y; // Старт верхнего ряда int rows = gridScript.GetRows(); // 2 int columns = gridScript.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(); } }