RoadManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace JTSystems
  5. {
  6. public class RoadManager : MonoBehaviour
  7. {
  8. [Header(" Debug ")]
  9. public bool DEBUG;
  10. public int levelToPlay;
  11. int level;
  12. [Header(" Road Chunks ")]
  13. public RoadChunk initialChunk;
  14. public RoadChunk finishChunk;
  15. private RoadChunk previousChunk;
  16. Vector3 finishPos;
  17. Vector3 spawnPos;
  18. [Header(" Predefined Levels ")]
  19. public LevelSequence[] levelSequences;
  20. List<RoadChunk> levelChunks = new List<RoadChunk>();
  21. static RoadManager instance;
  22. private void Awake()
  23. {
  24. level = PlayerPrefs.GetInt("LEVEL");
  25. }
  26. // Start is called before the first frame update
  27. void Start()
  28. {
  29. instance = this;
  30. UIManager.onLevelCompleteSet += IncreaseLevelIndex;
  31. //UIManager.onNextLevelButtonPressed += SpawnLevel;
  32. UIManager.onRetryButtonPressed += RetryLevel;
  33. SpawnLevel();
  34. }
  35. // Update is called once per frame
  36. void Update()
  37. {
  38. }
  39. private void IncreaseLevelIndex(int useless)
  40. {
  41. level++;
  42. PlayerPrefs.SetInt("LEVEL", level);
  43. }
  44. public void SpawnLevel()
  45. {
  46. // Delete the children
  47. ClearLevel();
  48. levelChunks.Clear();
  49. spawnPos = Vector3.zero;
  50. int currentLevel = level;
  51. if (DEBUG)
  52. currentLevel = levelToPlay;
  53. if (currentLevel >= levelSequences.Length)
  54. {
  55. SpawnLevelSequence(Random.Range(0, levelSequences.Length));
  56. }
  57. else
  58. SpawnLevelSequence(currentLevel);
  59. }
  60. private void SpawnLevelSequence(int currentLevel)
  61. {
  62. for (int i = 0; i < levelSequences[currentLevel].chunks.Length; i++)
  63. {
  64. RoadChunk chunkToSpawn = levelSequences[currentLevel].chunks[i];
  65. Instantiate(chunkToSpawn, spawnPos, Quaternion.identity, transform);
  66. spawnPos.z += chunkToSpawn.length;
  67. previousChunk = chunkToSpawn;
  68. levelChunks.Add(chunkToSpawn);
  69. }
  70. // We can then spawn the finish chunk
  71. Instantiate(finishChunk, spawnPos, Quaternion.identity, transform);
  72. levelChunks.Add(finishChunk);
  73. // Store the finish pos for progression use
  74. finishPos = spawnPos;
  75. }
  76. private void ClearLevel()
  77. {
  78. while (transform.childCount > 0)
  79. {
  80. Transform t = transform.GetChild(0);
  81. t.SetParent(null);
  82. Destroy(t.gameObject);
  83. }
  84. }
  85. public Vector3 GetFinishLinePosition()
  86. {
  87. return finishPos;
  88. }
  89. public void RetryLevel()
  90. {
  91. ClearLevel();
  92. spawnPos = Vector3.zero;
  93. for (int i = 0; i < levelChunks.Count; i++)
  94. {
  95. RoadChunk spawnedChunk = Instantiate(levelChunks[i], spawnPos, Quaternion.identity, transform);
  96. spawnPos.z += levelChunks[i].length;
  97. }
  98. }
  99. public float GetFinishLineZ()
  100. {
  101. return finishPos.z;
  102. }
  103. public static Vector3 GetFinishPosition()
  104. {
  105. return instance.GetFinishLinePosition();
  106. }
  107. }
  108. [System.Serializable]
  109. public struct LevelSequence
  110. {
  111. public RoadChunk[] chunks;
  112. }
  113. }