Advertisement
jamieTheCoder

GUIManager

Sep 23rd, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.30 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using MoreMountains.Tools;
  5. using UnityEngine.EventSystems;
  6.  
  7. namespace MoreMountains.CorgiEngine
  8. {  
  9.     /// <summary>
  10.     /// Handles all GUI effects and changes
  11.     /// </summary>
  12.     public class GUIManager : MMSingleton<GUIManager>, MMEventListener<LevelNameEvent>, MMEventListener<ControlsModeEvent>
  13.     {
  14.         [Header("Bindings")]
  15.  
  16.         /// the game object that contains the heads up display (avatar, health, points...)
  17.         [Tooltip("the game object that contains the heads up display (avatar, health, points...)")]
  18.         public GameObject HUD;
  19.         /// the jetpack bar
  20.         [Tooltip("the jetpack bar")]
  21.         public MMProgressBar[] HealthBars;
  22.         /// the jetpack bar
  23.         [Tooltip("the jetpack bar")]
  24.         public MMProgressBar[] JetPackBars;
  25.         [Tooltip("Custom bar")]
  26.         public MMProgressBar[] CustomBars;
  27.         /// the panels and bars used to display current weapon ammo
  28.         [Tooltip("the panels and bars used to display current weapon ammo")]
  29.         public AmmoDisplay[] AmmoDisplays;
  30.         /// the pause screen game object
  31.         [Tooltip("the pause screen game object")]
  32.         public GameObject PauseScreen;
  33.         /// the time splash gameobject
  34.         [Tooltip("the time splash gameobject")]
  35.         public GameObject TimeSplash;
  36.         /// The mobile buttons
  37.         [Tooltip("The mobile buttons")]
  38.         public CanvasGroup Buttons;
  39.         /// The mobile arrows
  40.         [Tooltip("The mobile arrows")]
  41.         public CanvasGroup Arrows;
  42.         /// The mobile movement joystick
  43.         [Tooltip("The mobile movement joystick")]
  44.         public CanvasGroup Joystick;
  45.         /// the points counter
  46.         [Tooltip("the points counter")]
  47.         public Text PointsText;
  48.         /// the level display
  49.         [Tooltip("the level display")]
  50.         public Text LevelText;
  51.  
  52.         [Header("Settings")]
  53.  
  54.         /// the pattern to apply when displaying the score
  55.         [Tooltip("the pattern to apply when displaying the score")]
  56.         public string PointsPattern = "000000";
  57.  
  58.         protected float _initialJoystickAlpha;
  59.         protected float _initialArrowsAlpha;
  60.         protected float _initialButtonsAlpha;
  61.  
  62.         /// <summary>
  63.         /// Initialization
  64.         /// </summary>
  65.         protected override void Awake()
  66.         {
  67.             base.Awake();
  68.  
  69.             if (Joystick != null)
  70.             {
  71.                 _initialJoystickAlpha = Joystick.alpha;
  72.             }
  73.             if (Arrows != null)
  74.             {
  75.                 _initialArrowsAlpha = Arrows.alpha;
  76.             }
  77.             if (Buttons != null)
  78.             {
  79.                 _initialButtonsAlpha = Buttons.alpha;
  80.             }
  81.         }
  82.  
  83.         /// <summary>
  84.         /// Initialization
  85.         /// </summary>
  86.         protected virtual void Start()
  87.         {
  88.             RefreshPoints();
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Sets the HUD active or inactive
  93.         /// </summary>
  94.         /// <param name="state">If set to <c>true</c> turns the HUD active, turns it off otherwise.</param>
  95.         public virtual void SetHUDActive(bool state)
  96.         {
  97.             if (HUD!= null)
  98.             {
  99.                 HUD.SetActive(state);
  100.             }
  101.             if (PointsText!= null)
  102.             {
  103.                 PointsText.enabled = state;
  104.             }
  105.             if (LevelText!= null)
  106.             {
  107.                 LevelText.enabled = state;
  108.             }
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Sets the avatar active or inactive
  113.         /// </summary>
  114.         /// <param name="state">If set to <c>true</c> turns the HUD active, turns it off otherwise.</param>
  115.         public virtual void SetAvatarActive(bool state)
  116.         {
  117.             if (HUD != null)
  118.             {
  119.                 HUD.SetActive(state);
  120.             }
  121.         }
  122.  
  123.         /// <summary>
  124.         /// Called by the input manager, this method turns controls visible or not depending on what's been chosen
  125.         /// </summary>
  126.         /// <param name="state">If set to <c>true</c> state.</param>
  127.         /// <param name="movementControl">Movement control.</param>
  128.         public virtual void SetMobileControlsActive(bool state, InputManager.MovementControls movementControl = InputManager.MovementControls.Joystick)
  129.         {
  130.             if (Joystick != null)
  131.             {
  132.                 Joystick.gameObject.SetActive(state);
  133.                 if (state && movementControl == InputManager.MovementControls.Joystick)
  134.                 {
  135.                     Joystick.alpha = _initialJoystickAlpha;
  136.                 }
  137.                 else
  138.                 {
  139.                     Joystick.alpha = 0;
  140.                     Joystick.gameObject.SetActive(false);
  141.                 }
  142.             }
  143.  
  144.             if (Arrows != null)
  145.             {
  146.                 Arrows.gameObject.SetActive(state);
  147.                 if (state && movementControl == InputManager.MovementControls.Arrows)
  148.                 {
  149.                     Arrows.alpha = _initialArrowsAlpha;
  150.                 }
  151.                 else
  152.                 {
  153.                     Arrows.alpha = 0;
  154.                     Arrows.gameObject.SetActive(false);
  155.                 }
  156.             }
  157.  
  158.             if (Buttons != null)
  159.             {
  160.                 Buttons.gameObject.SetActive(state);
  161.                 if (state)
  162.                 {
  163.                     Buttons.alpha=_initialButtonsAlpha;
  164.                 }
  165.                 else
  166.                 {
  167.                     Buttons.alpha=0;
  168.                     Buttons.gameObject.SetActive (false);
  169.                 }
  170.             }
  171.         }
  172.  
  173.         /// <summary>
  174.         /// Sets the pause.
  175.         /// </summary>
  176.         /// <param name="state">If set to <c>true</c>, sets the pause.</param>
  177.         public virtual void SetPause(bool state)
  178.         {
  179.             if (PauseScreen!= null)
  180.             {
  181.                 PauseScreen.SetActive(state);
  182.                 EventSystem.current.sendNavigationEvents = state;
  183.             }
  184.         }
  185.  
  186.         /// <summary>
  187.         /// Sets the jetpackbar active or not.
  188.         /// </summary>
  189.         /// <param name="state">If set to <c>true</c>, sets the pause.</param>
  190.         public virtual void SetJetpackBar(bool state, string playerID)
  191.         {
  192.             if (JetPackBars == null)
  193.             {
  194.                 return;
  195.             }
  196.  
  197.             foreach (MMProgressBar jetpackBar in JetPackBars)
  198.             {
  199.                 if (jetpackBar != null)
  200.                 {
  201.                     if (jetpackBar.PlayerID == playerID)
  202.                     {
  203.                         jetpackBar.gameObject.SetActive(state);
  204.                     }                  
  205.                 }
  206.             }          
  207.         }
  208.  
  209.         /// <summary>
  210.         /// Sets the ammo displays active or not
  211.         /// </summary>
  212.         /// <param name="state">If set to <c>true</c> state.</param>
  213.         /// <param name="playerID">Player I.</param>
  214.         public virtual void SetAmmoDisplays(bool state, string playerID, int ammoDisplayID)
  215.         {
  216.             if (AmmoDisplays == null)
  217.             {
  218.                 return;
  219.             }
  220.  
  221.             foreach (AmmoDisplay ammoDisplay in AmmoDisplays)
  222.             {
  223.                 if (ammoDisplay != null)
  224.                 {
  225.                     if ((ammoDisplay.PlayerID == playerID) && (ammoDisplay.AmmoDisplayID == ammoDisplayID))
  226.                     {
  227.                         ammoDisplay.gameObject.SetActive(state);
  228.                     }                  
  229.                 }
  230.             }
  231.         }
  232.  
  233.         /// <summary>
  234.         /// Sets the time splash.
  235.         /// </summary>
  236.         /// <param name="state">If set to <c>true</c>, turns the timesplash on.</param>
  237.         public virtual void SetTimeSplash(bool state)
  238.         {
  239.             if (TimeSplash != null)
  240.             {
  241.                 TimeSplash.SetActive(state);
  242.             }
  243.         }
  244.        
  245.         /// <summary>
  246.         /// Sets the text to the game manager's points.
  247.         /// </summary>
  248.         public virtual void RefreshPoints()
  249.         {
  250.             if (PointsText!= null)
  251.             {
  252.                 PointsText.text = GameManager.Instance.Points.ToString(PointsPattern);
  253.             }
  254.         }
  255.  
  256.         /// <summary>
  257.         /// Updates the health bar.
  258.         /// </summary>
  259.         /// <param name="currentHealth">Current health.</param>
  260.         /// <param name="minHealth">Minimum health.</param>
  261.         /// <param name="maxHealth">Max health.</param>
  262.         /// <param name="playerID">Player I.</param>
  263.         public virtual void UpdateHealthBar(float currentHealth,float minHealth,float maxHealth,string playerID)
  264.         {
  265.             if (HealthBars == null) { return; }
  266.             if (HealthBars.Length <= 0) { return; }
  267.  
  268.             foreach (MMProgressBar healthBar in HealthBars)
  269.             {
  270.                 if (healthBar == null) { continue; }
  271.                 if (healthBar.PlayerID == playerID)
  272.                 {
  273.                     healthBar.UpdateBar(currentHealth,minHealth,maxHealth);
  274.                 }
  275.             }
  276.  
  277.         }
  278.  
  279.         /// <summary>
  280.         /// Updates the jetpack bar.
  281.         /// </summary>
  282.         /// <param name="currentFuel">Current fuel.</param>
  283.         /// <param name="minFuel">Minimum fuel.</param>
  284.         /// <param name="maxFuel">Max fuel.</param>
  285.         /// <param name="playerID">Player I.</param>
  286.         public virtual void UpdateJetpackBar(float currentFuel, float minFuel, float maxFuel,string playerID)
  287.         {
  288.             if (JetPackBars == null)
  289.             {
  290.                 return;
  291.             }
  292.  
  293.             foreach (MMProgressBar jetpackBar in JetPackBars)
  294.             {
  295.                 if (jetpackBar == null) { return; }
  296.                 if (jetpackBar.PlayerID == playerID)
  297.                 {
  298.                     jetpackBar.SetBar(currentFuel,minFuel,maxFuel);
  299.                 }    
  300.             }
  301.         }
  302.         public virtual void UpdateCustomBar(float currentProgress, float minProgress, float maxProgress,string playerID)
  303.         {
  304.             if (CustomBars == null)
  305.             {
  306.                 return;
  307.             }
  308.  
  309.             foreach (MMProgressBar CustomBar in JetPackBars)
  310.             {
  311.                 if (CustomBar == null) { return; }
  312.                 if (CustomBar.PlayerID == playerID)
  313.                 {
  314.                     CustomBar.SetBar(currentProgress,minProgress,maxProgress);
  315.                 }    
  316.             }
  317.         }
  318.  
  319.         /// <summary>
  320.         /// Updates the (optional) ammo displays.
  321.         /// </summary>
  322.         /// <param name="magazineBased">If set to <c>true</c> magazine based.</param>
  323.         /// <param name="totalAmmo">Total ammo.</param>
  324.         /// <param name="maxAmmo">Max ammo.</param>
  325.         /// <param name="ammoInMagazine">Ammo in magazine.</param>
  326.         /// <param name="magazineSize">Magazine size.</param>
  327.         /// <param name="playerID">Player I.</param>
  328.         /// <param name="displayTotal">If set to <c>true</c> display total.</param>
  329.         public virtual void UpdateAmmoDisplays(bool magazineBased, int totalAmmo, int maxAmmo, int ammoInMagazine, int magazineSize, string playerID, int ammoDisplayID, bool displayTotal)
  330.         {
  331.             if (AmmoDisplays == null)
  332.             {
  333.                 return;
  334.             }
  335.  
  336.             foreach (AmmoDisplay ammoDisplay in AmmoDisplays)
  337.             {
  338.                 if (ammoDisplay == null) { return; }
  339.                 if ((ammoDisplay.PlayerID == playerID) && (ammoDisplay.AmmoDisplayID == ammoDisplayID))
  340.                 {
  341.                     ammoDisplay.UpdateAmmoDisplays (magazineBased, totalAmmo, maxAmmo, ammoInMagazine, magazineSize, displayTotal);
  342.                 }    
  343.             }
  344.         }
  345.        
  346.         /// <summary>
  347.         /// Sets the level name in the HUD
  348.         /// </summary>
  349.         public virtual void SetLevelName(string name)
  350.         {
  351.             if (LevelText!= null)
  352.             {
  353.                 LevelText.text=name;
  354.             }
  355.         }
  356.  
  357.         /// <summary>
  358.         /// When we catch a level name event, we change our level's name in the GUI
  359.         /// </summary>
  360.         /// <param name="levelNameEvent"></param>
  361.         public virtual void OnMMEvent(LevelNameEvent levelNameEvent)
  362.         {
  363.             SetLevelName(levelNameEvent.LevelName);
  364.         }
  365.  
  366.         public virtual void OnMMEvent(ControlsModeEvent controlsModeEvent)
  367.         {
  368.             SetMobileControlsActive(controlsModeEvent.Status, controlsModeEvent.MovementControl);
  369.         }
  370.  
  371.         /// <summary>
  372.         /// On enable, we start listening to events
  373.         /// </summary>
  374.         protected virtual void OnEnable()
  375.         {
  376.             this.MMEventStartListening<LevelNameEvent>();
  377.             this.MMEventStartListening<ControlsModeEvent>();
  378.         }
  379.  
  380.         /// <summary>
  381.         /// On disable, we stop listening to events
  382.         /// </summary>
  383.         protected virtual void OnDisable()
  384.         {
  385.             this.MMEventStopListening<LevelNameEvent>();
  386.             this.MMEventStopListening<ControlsModeEvent>();
  387.         }
  388.     }
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement