diff --git a/MrBigsock/Assets/Code/FollowPlayer.cs b/MrBigsock/Assets/Code/FollowPlayer.cs index 0a4ad56964aed5f11084cd780aea7b7d36f0b7a0..28c604e8ab58c20fb794efac2eba97ca3ef9a6e5 100644 --- a/MrBigsock/Assets/Code/FollowPlayer.cs +++ b/MrBigsock/Assets/Code/FollowPlayer.cs @@ -8,44 +8,30 @@ namespace Bigsock { public class FollowPlayer : MonoBehaviour { - [SerializeField] - private NeighbourMapGenerator neighborMapGenerator; - [SerializeField] - private CinemachineConfiner2D cameraMap; - [SerializeField] GameObject VictoryScreen; + [SerializeField] NeighbourMapGenerator neighborMapGenerator; + [SerializeField] CinemachineConfiner2D cameraMap; + [SerializeField] GameObject victoryScreen; - private int levels = 0; - - - Vector3 offset = new Vector3(0, 0, -10); - int i = 0; + //Unity varibles + private GameObject[] boss; + private GameObject player; + private GameObject[] enemies; + private GameObject[] Chest; + private List<GameObject> chests; + private GameObject stair; - GameObject[] boss; - GameObject player; - GameObject[] enemies; - GameObject[] Chest; - List<GameObject> chests; - GameObject stair; - int finalLevel = 2; + //Normal variables + private int levels = 0; + private static int LEVEL_CAP = 2; private int roomNr = 0; - bool stairs_down; - int h = 0; + private bool stairs_down; + private int victoryHold = 0; void Start() { chests = new List<GameObject>(); - neighborMapGenerator.RunProceduralGeneration(); player = GameObject.Find("BigSock"); - stair = GameObject.Find("Stairs(Clone)"); - Chest = GameObject.FindGameObjectsWithTag("Chest"); - foreach(GameObject c in Chest) - { - chests.Add(c); - c.SetActive(false); - } - stairs_down = stair.GetComponent<Stairs>().stairs_touch; - stair.SetActive(false); - + InitializeLevel(); } void LateUpdate() @@ -53,63 +39,30 @@ namespace Bigsock boss = GameObject.FindGameObjectsWithTag("Boss"); enemies = GameObject.FindGameObjectsWithTag((roomNr).ToString()); - if (boss.Length == 0 && levels < 2) + if (boss.Length == 0 && levels < LEVEL_CAP) { if (!stair.activeInHierarchy) { - Debug.Log("hei"); levels++; } stair.SetActive(true); stairs_down = stair.GetComponent<Stairs>().stairs_touch; } - if (levels == finalLevel && h == 0) + if (levels == LEVEL_CAP && victoryHold == 0) { - Time.timeScale = 0; - GameObject canvas = GameObject.Find("Canvas"); - if(canvas != null) - { - Instantiate(VictoryScreen, canvas.transform); - h++; - } + VictoryScreen(); + victoryHold++; } - - - if (stairs_down) { - roomNr = 0; - foreach (GameObject c in chests) - { - DestroyImmediate(c); - } - DestroyImmediate(stair); - chests.Clear(); - TilemapGenerator.resetMaps(); - TilemapGenerator.SetRoomIDZero(); + ClearScene(); player.transform.position = new Vector3(9,5,0); - neighborMapGenerator.RunProceduralGeneration(); - Chest = GameObject.FindGameObjectsWithTag("Chest"); - stair = GameObject.Find("Stairs(Clone)"); - stairs_down = stair.GetComponent<Stairs>().stairs_touch; - foreach (GameObject c in Chest) - { - if(c != null) - { - c.SetActive(false); - chests.Add(c); - } - } - stair.SetActive(false); - + InitializeLevel(); } - int i = 0; - if(i == 0) { - cameraMap.InvalidateCache(); - } + cameraMap.InvalidateCache(); if (enemies.Length == 0) { @@ -126,16 +79,57 @@ namespace Bigsock } private void OnDestroy() + { + ClearScene(); + } + + /* + *Deletes all game objects from the scene that is not the player + *and also resets all arrays that have been made*/ + private void ClearScene() { roomNr = 0; foreach (GameObject c in chests) { DestroyImmediate(c); } + chests.Clear(); DestroyImmediate(stair); NeighbourMapGenerator.ClearRoomList(); TilemapGenerator.SetRoomIDZero(); TilemapGenerator.resetMaps(); } + + /* + *Initializes the victory screen*/ + private void VictoryScreen() + { + Time.timeScale = 0; + GameObject canvas = GameObject.Find("Canvas"); + if (canvas != null) + { + Instantiate(victoryScreen, canvas.transform); + } + } + + /* + *Creates a level and adds objects to the different arrays */ + private void InitializeLevel() + { + neighborMapGenerator.RunProceduralGeneration(); + Chest = GameObject.FindGameObjectsWithTag("Chest"); + stair = GameObject.Find("Stairs(Clone)"); + stairs_down = stair.GetComponent<Stairs>().stairs_touch; + foreach (GameObject c in Chest) + { + if (c != null) + { + c.SetActive(false); + chests.Add(c); + } + } + stairs_down = stair.GetComponent<Stairs>().stairs_touch; + stair.SetActive(false); + } } }