dronkowitz

HUD.cs

Mar 1st, 2021 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6.  
  7. public class HUD : MonoBehaviour
  8. {
  9.     public TMP_Text scoreText, timeText, ringText, livesText;
  10.     public GameObject playerprefab;
  11.     public List<Image> icons, faces;
  12.     public List<Character> teamSprites;
  13.     public List<Image> lightsSpeed, lightsFly, lightsPower;
  14.     public Image livesImage;
  15.     TeamComposition curTeam;
  16.     public static float timer;
  17.     public Sprite speedSprite, flySprite, powerSprite;
  18.     public Slider powerUpGauge;
  19.     private int powerUpLevel = 0;
  20.     public GameObject teamBlastPrompt;
  21.     public Image teamBlastFill;
  22.  
  23.     public bool swap;
  24.     public CHARACTERTYPES newType;
  25.     // Start is called before the first frame update
  26.     void Start()
  27.     {
  28.         GameInstance.UpdateData += UpdateRings;
  29.         UpdateHUD();
  30.  
  31.     }
  32.  
  33.     public void Setup(TeamComposition currentTeam)
  34.     {
  35.         curTeam = currentTeam;
  36.         teamSprites.Add(curTeam.speedCharacter);
  37.         teamSprites.Add(curTeam.flyingCharacter);
  38.         teamSprites.Add(curTeam.powerCharacter);
  39.         livesImage.sprite = curTeam.speedCharacter.face;
  40.         SetCharacter(0);
  41.     }
  42.  
  43.     // Update is called once per frame
  44.     void Update()
  45.     {
  46.         timer += Time.deltaTime;
  47.  
  48.  
  49.         UpdateHUD();
  50.     }
  51.  
  52.     public void UpdateHUD()
  53.     {
  54.  
  55.         int ms = Mathf.RoundToInt((timer - Mathf.Floor(timer)) * 100);
  56.         if (ms >= 100) ms -= 100;
  57.         string s = Mathf.Floor(timer % 60).ToString("00");
  58.         string m = Mathf.Floor(timer / 60).ToString("00");
  59.         timeText.text = ($"{m}:{s}:{ms:00}");
  60.  
  61.         scoreText.text = GameInstance.scoreCount.ToString("00000000");
  62.         livesText.text = GameInstance.livesCount.ToString("00");
  63.  
  64.        
  65.         UpdateRings();
  66.         UpdateTeamBlastMeter();
  67.     }
  68.  
  69.     public void UpdateTeamBlastMeter()
  70.     {
  71.         powerUpGauge.value = powerUpLevel;
  72.         if (powerUpLevel == 100)
  73.         {
  74.             teamBlastFill.color = Color.yellow;
  75.             teamBlastPrompt.SetActive(true);
  76.             if (Input.GetKeyDown(KeyCode.Z))
  77.             {
  78.                 FindObjectOfType<TeamBlastVideos>().SpecialAttack();
  79.                 powerUpLevel = 0;
  80.             }
  81.         }
  82.         else
  83.         {
  84.             teamBlastPrompt.SetActive(false);
  85.             teamBlastFill.color = Color.blue;
  86.         }
  87.     }
  88.  
  89.     public void AddPower(int value)
  90.     {
  91.         powerUpLevel = Mathf.Min(100, powerUpLevel + value);
  92.     }
  93.  
  94.     public void UpdateRings()
  95.     {
  96.         ringText.text = GameInstance.currentRings.ToString("000");
  97.     }
  98.  
  99.     public void SetCharacter(CHARACTERTYPES type)
  100.     {
  101.         teamSprites[0] = curTeam.speedCharacter;
  102.         teamSprites[1] = curTeam.flyingCharacter;
  103.         teamSprites[2] = curTeam.powerCharacter;
  104.  
  105.         switch (type)
  106.         {
  107.             case (CHARACTERTYPES.Speed):
  108.                 SetCharacter(0);
  109.                 break;
  110.             case (CHARACTERTYPES.Fly):
  111.                 SetCharacter(-1);
  112.                 break;
  113.             case (CHARACTERTYPES.Power):
  114.                 SetCharacter(2);
  115.                 break;
  116.         }
  117.    
  118.     }
  119.     public void UpdateCharacterLevels()
  120.     {
  121.         GameInstance.speedLevelUp = Mathf.Clamp(GameInstance.speedLevelUp, 0, 3);
  122.         GameInstance.flyLevelUp = Mathf.Clamp(GameInstance.flyLevelUp, 0, 3);
  123.         GameInstance.powerLevelUp = Mathf.Clamp(GameInstance.powerLevelUp, 0, 3);
  124.         int counter = 0;
  125.         while (counter < GameInstance.speedLevelUp)
  126.         {
  127.             lightsSpeed[counter].enabled = true;
  128.             counter += 1;
  129.         }
  130.         counter = 0;
  131.         while (counter < GameInstance.flyLevelUp)
  132.         {
  133.             lightsFly[counter].enabled = true;
  134.             counter += 1;
  135.         } counter = 0;
  136.         while (counter < GameInstance.powerLevelUp)
  137.         {
  138.             lightsPower[counter].enabled = true;
  139.             counter += 1;
  140.         }
  141.     }
  142.  
  143.     public void SetCharacter(int direction)
  144.     {
  145.         if (direction >  0)
  146.         {
  147.             Character s = teamSprites[teamSprites.Count - 1];
  148.             teamSprites.RemoveAt(teamSprites.Count - 1);
  149.             teamSprites.Insert(0, s);
  150.         }
  151.         else if(direction < 0)
  152.         {
  153.             Character s = teamSprites[0];
  154.             teamSprites.RemoveAt(0);
  155.             teamSprites.Add(s);
  156.         }
  157.         livesImage.sprite = teamSprites[0].face;
  158.         for (int i = 0; i < 3; i++)
  159.         {
  160.             faces[i].sprite = teamSprites[i].face;
  161.             icons[i].sprite = teamSprites[i].icon;
  162.         }
  163.     }
  164. }
Add Comment
Please, Sign In to add comment