ColumnMaker.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections.Generic;
  2. using System.Collections;
  3. using UnityEngine;
  4. namespace Generator
  5. {
  6. public class ColumnMaker : MonoBehaviour
  7. {
  8. private int _lastFrameX;
  9. private int _lastFrameY;
  10. private List<Vector2Int> _touchedCells;
  11. [SerializeField] private PathMaker _pathMaker;
  12. public delegate void ColumnDelegate(Vector2Int[] positions, int gravityDirection);
  13. public event ColumnDelegate OnCreateColumn;
  14. private IEnumerator BlockingCoroutine()
  15. {
  16. while (true)
  17. {
  18. _touchedCells = new List<Vector2Int>();
  19. while (_pathMaker.OnBlocks)
  20. {
  21. var position = transform.position;
  22. var currentX = Mathf.RoundToInt(position.x);
  23. var currentY = Mathf.RoundToInt(position.y);
  24. if (currentX != _lastFrameX)
  25. {
  26. if (_touchedCells.Count > 0)
  27. OnCreateColumn?.Invoke(_touchedCells.ToArray(), _pathMaker.GravityDirection);
  28. _touchedCells = new() { new Vector2Int(currentX, currentY) };
  29. }
  30. else if (currentY != _lastFrameY)
  31. _touchedCells.Add(new Vector2Int(currentX, currentY));
  32. _lastFrameX = currentX;
  33. _lastFrameY = currentY;
  34. yield return null;
  35. }
  36. yield return new WaitUntil(() => _pathMaker.OnBlocks);
  37. }
  38. }
  39. private void Start()
  40. {
  41. var position = transform.position;
  42. _touchedCells = new() { new Vector2Int((int)position.x, (int)position.y) };
  43. StartCoroutine(BlockingCoroutine());
  44. }
  45. }
  46. }