123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace JTSystems
- {
- public class RoadManager : MonoBehaviour
- {
- [Header(" Debug ")]
- public bool DEBUG;
- public int levelToPlay;
- int level;
- [Header(" Road Chunks ")]
- public RoadChunk initialChunk;
- public RoadChunk finishChunk;
- private RoadChunk previousChunk;
- Vector3 finishPos;
- Vector3 spawnPos;
- [Header(" Predefined Levels ")]
- public LevelSequence[] levelSequences;
- List<RoadChunk> levelChunks = new List<RoadChunk>();
- static RoadManager instance;
- private void Awake()
- {
- level = PlayerPrefs.GetInt("LEVEL");
- }
- // Start is called before the first frame update
- void Start()
- {
- instance = this;
- UIManager.onLevelCompleteSet += IncreaseLevelIndex;
- //UIManager.onNextLevelButtonPressed += SpawnLevel;
- UIManager.onRetryButtonPressed += RetryLevel;
- SpawnLevel();
- }
- // Update is called once per frame
- void Update()
- {
- }
- private void IncreaseLevelIndex(int useless)
- {
- level++;
- PlayerPrefs.SetInt("LEVEL", level);
- }
- public void SpawnLevel()
- {
- // Delete the children
- ClearLevel();
- levelChunks.Clear();
- spawnPos = Vector3.zero;
- int currentLevel = level;
- if (DEBUG)
- currentLevel = levelToPlay;
- if (currentLevel >= levelSequences.Length)
- {
- SpawnLevelSequence(Random.Range(0, levelSequences.Length));
- }
- else
- SpawnLevelSequence(currentLevel);
- }
- private void SpawnLevelSequence(int currentLevel)
- {
- for (int i = 0; i < levelSequences[currentLevel].chunks.Length; i++)
- {
- RoadChunk chunkToSpawn = levelSequences[currentLevel].chunks[i];
- Instantiate(chunkToSpawn, spawnPos, Quaternion.identity, transform);
- spawnPos.z += chunkToSpawn.length;
- previousChunk = chunkToSpawn;
- levelChunks.Add(chunkToSpawn);
- }
- // We can then spawn the finish chunk
- Instantiate(finishChunk, spawnPos, Quaternion.identity, transform);
- levelChunks.Add(finishChunk);
- // Store the finish pos for progression use
- finishPos = spawnPos;
- }
- private void ClearLevel()
- {
- while (transform.childCount > 0)
- {
- Transform t = transform.GetChild(0);
- t.SetParent(null);
- Destroy(t.gameObject);
- }
- }
- public Vector3 GetFinishLinePosition()
- {
- return finishPos;
- }
- public void RetryLevel()
- {
- ClearLevel();
- spawnPos = Vector3.zero;
- for (int i = 0; i < levelChunks.Count; i++)
- {
- RoadChunk spawnedChunk = Instantiate(levelChunks[i], spawnPos, Quaternion.identity, transform);
- spawnPos.z += levelChunks[i].length;
- }
- }
- public float GetFinishLineZ()
- {
- return finishPos.z;
- }
- public static Vector3 GetFinishPosition()
- {
- return instance.GetFinishLinePosition();
- }
- }
- [System.Serializable]
- public struct LevelSequence
- {
- public RoadChunk[] chunks;
- }
- }
|