SpawnManagerV2.cs (5601B)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class SpawnManagerV2 : MonoBehaviour { 6 7 //Player GameObjects 8 private GameObject[] slimePlayer; 9 private GameObject[] hats; 10 private GameObject[] swords; 11 12 public GameObject sword; 13 14 private GameObject spawnedSlime; 15 private GameObject spawnedSword; 16 private GameObject spawnedHat; 17 private GameObject spawnedSwordModel; 18 19 public Camera cam; 20 public GameObject dataH; 21 public GameObject hud; 22 23 public List<GameObject> spawnedPlayers; 24 public List<GameObject> spawnedSwords; 25 26 [HideInInspector] public List<GameObject> existingPlayers; 27 //Array of the spawnPoint Transforms 28 public List<Transform> spawnPoints; 29 private List<int> usableSpawnPoints; 30 //Int to hold the number of players in the game 31 private int numberPlayers = 4; 32 33 //Int to hold the nubmer of spawn points 34 public int numberOfSpawns = 4; 35 36 private WinChecker winChecker; 37 38 private void Start() 39 { 40 slimePlayer = PublicStatHandler.GetInstance().slimes.ToArray(); 41 hats = PublicStatHandler.GetInstance().hats.ToArray(); 42 swords = PublicStatHandler.GetInstance().swords.ToArray(); 43 44 usableSpawnPoints = new List<int>(); 45 46 existingPlayers = new List<GameObject>(); 47 48 numberPlayers = PublicStatHandler.GetInstance().amountOfPlayers; 49 50 Spawn(); 51 52 cam.gameObject.SetActive(true); 53 dataH.SetActive(true); 54 hud.SetActive(true); 55 winChecker = dataH.GetComponent<WinChecker>(); 56 } 57 58 // Use this for initialization 59 void Spawn() { 60 61 GameObject[] existingPlayers = FindExistingPlayers("Slime"); 62 foreach (GameObject slime in existingPlayers) 63 { 64 Destroy(slime.GetComponent<PlayerMovement>().Sword); 65 slime.SetActive(false); 66 } 67 existingPlayers = null; 68 69 ResetUseableSpawns(); 70 71 for (int s = 0; s < numberPlayers; s++) { 72 73 Debug.Log("The number of players is: " + numberPlayers); 74 //Spawns player at the random index 75 spawnedSlime = (GameObject)Instantiate(slimePlayer[s]); 76 spawnedSlime.gameObject.name = "Slime" + (s + 1); 77 spawnedSlime.SetActive(true); 78 spawnedPlayers.Add(spawnedSlime); 79 80 PlayerMovement playerMov = spawnedSlime.GetComponent<PlayerMovement>(); 81 82 if (hats.Length >= (s + 1)) 83 { 84 if (hats[s] != null) 85 { 86 spawnedHat = (GameObject)Instantiate(hats[s]); 87 spawnedHat.GetComponent<HatScript>().Owner = spawnedSlime; 88 playerMov.Hat = spawnedHat; 89 } 90 } 91 92 if (swords.Length >= (s + 1) && swords[s] != null) { 93 spawnedSword = (GameObject)Instantiate(swords[s]); 94 spawnedSwords.Add(spawnedSword); 95 spawnedSword.GetComponent<SwordScript>().Owner = spawnedSlime; 96 playerMov.Sword = spawnedSword; 97 spawnedSword.gameObject.name = "Sword" + (s + 1); 98 } 99 else 100 { 101 spawnedSword = (GameObject)Instantiate(sword); 102 spawnedSword.GetComponent<SwordScript>().Owner = spawnedSlime; 103 spawnedSwords.Add(spawnedSword); 104 playerMov.Sword = spawnedSword; 105 spawnedSword.gameObject.name = "Sword" + (s + 1); 106 } 107 108 slimePlayer[s].transform.position = SelectSpawnPoint().position; 109 } 110 111 if (hud.activeSelf) 112 { 113 hud.GetComponent<HudAndHealthSetter>().Resetter(); 114 } 115 116 Respawn(); 117 } 118 119 public void Respawn() 120 { 121 ResetUseableSpawns(); 122 123 foreach (GameObject slime in FindExistingPlayers("DeadSlime")) 124 { 125 slime.GetComponent<PlayerMovement>().health = PublicStatHandler.GetInstance().health; 126 winChecker.UnSetGhost(slime); 127 } 128 129 existingPlayers.Clear(); 130 131 existingPlayers.AddRange(FindExistingPlayers("Slime")); 132 existingPlayers.AddRange(FindExistingPlayers("DeadSlime")); 133 134 foreach (GameObject slime in existingPlayers) 135 { 136 slime.GetComponent<PlayerMovement>().health = PublicStatHandler.GetInstance().health; 137 slime.GetComponent<PlayerMovement>().slimePercentage = 0; 138 Debug.Log(slime.name + " " + slime.GetComponent<PlayerMovement>().health); 139 slime.transform.position = SelectSpawnPoint().position; 140 } 141 142 hud.GetComponent<HudAndHealthSetter>().Resetter(); 143 } 144 145 public void ResetUseableSpawns() 146 { 147 usableSpawnPoints.Clear(); 148 for (int i = 0; i < numberOfSpawns; i++) 149 { 150 usableSpawnPoints.Add(i); 151 } 152 Debug.Log("Reset Spawns, Number of spawns at " + usableSpawnPoints.Count); 153 } 154 155 public Transform SelectSpawnPoint() 156 { 157 for (int i = 0; i < usableSpawnPoints.Count; i++) 158 { 159 Debug.Log("Useable spawnpoints: " + usableSpawnPoints[i]); 160 } 161 int randomIndex = Random.Range(0, (usableSpawnPoints.Count)); 162 int randomNumber = (int)usableSpawnPoints[randomIndex]; 163 Debug.Log("Selected Spawn: " + randomNumber); 164 usableSpawnPoints.RemoveAt(randomIndex); 165 Debug.Log(spawnPoints[randomNumber].transform.position); 166 return spawnPoints[randomNumber]; 167 } 168 169 public GameObject[] FindExistingPlayers(string tag) 170 { 171 return GameObject.FindGameObjectsWithTag(tag); 172 } 173 }