HudAndHealthSetter.cs (4360B)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 public class HudAndHealthSetter : MonoBehaviour 7 { 8 // Start is called before the first frame update 9 public float harmNumber, hurtNumber, ouchNumber; 10 public List<GameObject> playerHuds; 11 12 public List<GameObject> p1HealthPool, p2HealthPool, p3HealthPool, p4HealthPool; 13 public List<GameObject> activeHeartsP1, activeHeartsP2, activeHeartsP3, activeHeartsP4; 14 15 public GameObject spawnManager; 16 private SpawnManagerV2 spawnManagementScript; 17 18 private int numPlayers; 19 private bool ready = false; 20 21 private bool[] dead; 22 23 void Awake() 24 { 25 numPlayers = PublicStatHandler.GetInstance().amountOfPlayers; 26 Resetter(); 27 Debug.Log(PublicStatHandler.GetInstance().amountOfPlayers); 28 spawnManagementScript = spawnManager.GetComponent<SpawnManagerV2>(); 29 dead = new bool[PublicStatHandler.GetInstance().amountOfPlayers]; 30 } 31 32 // Update is called once per frame 33 void Update() 34 { 35 if (ready) 36 { 37 for (int i = 0; i < numPlayers; i++) 38 { 39 if (spawnManagementScript.spawnedPlayers[i].GetComponent<PlayerMovement>().isAlive) 40 { 41 dead[i] = false; 42 } 43 44 if (spawnManagementScript.spawnedPlayers[i].GetComponent<PlayerMovement>().health < GetHealthList(true, i).Count && !dead[i]) 45 { 46 Debug.Log(dead[i]); 47 GetHealthList(true, i)[GetHealthList(true, i).Count - 1].SetActive(false); 48 GetHealthList(true, i).RemoveAt(GetHealthList(true, i).Count - 1); 49 50 if (!spawnManagementScript.spawnedPlayers[i].GetComponent<PlayerMovement>().isAlive) 51 { 52 dead[i] = true; 53 } 54 } 55 56 float per = spawnManagementScript.spawnedPlayers[i].GetComponent<PlayerMovement>().slimePercentage; 57 if (per < harmNumber) { playerHuds[i].GetComponentInChildren<EmoteChanger>().SetSprite(0); } else 58 if (per < hurtNumber) { playerHuds[i].GetComponentInChildren<EmoteChanger>().SetSprite(1); } else 59 if (per < ouchNumber) { playerHuds[i].GetComponentInChildren<EmoteChanger>().SetSprite(2); } 60 else { playerHuds[i].GetComponentInChildren<EmoteChanger>().SetSprite(3); } 61 } 62 } 63 } 64 65 public void Resetter() 66 { 67 ready = false; 68 69 ResetThis(playerHuds); 70 ResetThis(p1HealthPool); 71 ResetThis(p2HealthPool); 72 ResetThis(p3HealthPool); 73 ResetThis(p4HealthPool); 74 75 for (int i = 0; i < numPlayers; i++) 76 { 77 if (GetHealthList(true, i) != null) 78 { 79 GetHealthList(true, i).Clear(); 80 } 81 } 82 83 SetHealth(); 84 ready = true; 85 } 86 87 public void ResetThis(List<GameObject> theList) 88 { 89 foreach (GameObject slimeHud in theList) 90 { 91 slimeHud.SetActive(false); 92 } 93 } 94 public void SetHealth() 95 { 96 for (int z = 0; z < numPlayers; z++) 97 { 98 playerHuds[z].SetActive(true); 99 playerHuds[z].GetComponentInChildren<EmoteChanger>().SetSprite(0); 100 } 101 102 for (int i = 0; i < PublicStatHandler.GetInstance().health; i++) 103 { 104 for (int j = 0; j < numPlayers; j++) 105 { 106 GetHealthList(false, j)[i].SetActive(true); 107 GetHealthList(true, j).Add(GetHealthList(false, j)[i]); 108 } 109 } 110 111 } 112 113 private List<GameObject> GetHealthList(bool exist, int index) 114 { 115 if (exist) 116 { 117 switch (index) 118 { 119 case 0: return activeHeartsP1; 120 case 1: return activeHeartsP2; 121 case 2: return activeHeartsP3; 122 case 3: return activeHeartsP4; 123 default: return null; 124 } 125 } 126 else 127 { 128 switch (index) 129 { 130 case 0: return p1HealthPool; 131 case 1: return p2HealthPool; 132 case 2: return p3HealthPool; 133 case 3: return p4HealthPool; 134 default: return null; 135 } 136 } 137 } 138 }