FoldLinesGenerator.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using JTSystems;
  5. public class FoldLinesGenerator : MonoBehaviour
  6. {
  7. [Header(" Components ")]
  8. [SerializeField] private LineRenderer linePrefab;
  9. private Paper currentPaper;
  10. private void Awake()
  11. {
  12. LevelManager.onPaperInstantiated += DrawFoldingLines;
  13. UIManager.onLevelCompleteSet += ClearPaperDelegates;
  14. }
  15. private void OnDestroy()
  16. {
  17. LevelManager.onPaperInstantiated -= DrawFoldingLines;
  18. }
  19. private void ClearPaperDelegates(int none)
  20. {
  21. currentPaper.onPaperStateChanged -= UpdateFoldingLines;
  22. currentPaper = null;
  23. UpdateFoldingLines();
  24. }
  25. private void DrawFoldingLines(Paper paper)
  26. {
  27. currentPaper = paper;
  28. currentPaper.onPaperStateChanged += UpdateFoldingLines;
  29. UpdateFoldingLines();
  30. }
  31. private void UpdateFoldingLines()
  32. {
  33. transform.Clear();
  34. if (currentPaper == null) return;
  35. Folding[] foldings = currentPaper.GetFoldings(false);
  36. foreach (Folding folding in foldings)
  37. if (!folding.IsFolded())
  38. DrawFolding(folding);
  39. }
  40. private void DrawFolding(Folding folding)
  41. {
  42. LineRenderer line = Instantiate(linePrefab, transform);
  43. Vector3[] foldingDirection = folding.GetRotationAxis().AsLine();
  44. Vector3 p0 = foldingDirection[0].With(y: currentPaper.transform.position.y + -0.05f);
  45. Vector3 p1 = foldingDirection[1].With(y: currentPaper.transform.position.y + -0.05f);
  46. line.SetPosition(0, p0);
  47. line.SetPosition(1, p1);
  48. }
  49. }