EgonMilanVotrubec

AntnomolyGame

Apr 29th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4.  
  5. namespace Antnomoly
  6. {
  7.     /// <summary>
  8.     /// This is used to send Event Arguments to all of the classes that are listening
  9.     /// to the "StateChange" event when it is fired.
  10.     /// </summary>
  11.     public class StateChangeArgs : EventArgs
  12.     {
  13.         public StateValues State;
  14.     }
  15.  
  16.     public class AntnomolyGame : MonoBehaviour
  17.     {
  18.         public event EventHandler<StateChangeArgs> StateChange;
  19.  
  20.         public Text GameText;
  21.  
  22.         int breath;
  23.         int strength;
  24.         int timer;
  25.  
  26.         private StateValues state;
  27.         public StateValues State
  28.         {
  29.             get { return state; }
  30.             set
  31.             {
  32.                 state = value;
  33.  
  34.                 // This method will now take care of the code based on your current State.
  35.                 StateMethod ( );
  36.  
  37.                 // Fire the StateChange event so that all listeners can then react.
  38.                 OnStateChange ( );
  39.             }
  40.         }
  41.  
  42.  
  43.         void Awake ( )
  44.         {
  45.             // When you call the lower case "state", your referencing the local, private variable.
  46.             // If you referenced the uppercase "State", you'd be calling the State property method,
  47.             // and firing the "StateChange" event.
  48.             state = StateValues.None;
  49.         }
  50.  
  51.  
  52.         /// <summary>
  53.         /// When the state changes, it calls this method. This method is used to keep all of your
  54.         /// game code seperate from your "State" property method, nothing more.
  55.         /// </summary>
  56.         private void StateMethod ( )
  57.         {
  58.             // Do something based on your current state
  59.             switch ( state )
  60.             {
  61.                 case StateValues.beginning:
  62.                     // Do your beginning code...
  63.                     break;
  64.             }
  65.  
  66.         }
  67.  
  68.  
  69.         /// <summary>
  70.         /// This is the Event method that will notify all listeners that the state has been changed.
  71.         /// </summary>
  72.         private void OnStateChange ( )
  73.         {
  74.             StateChange?.Invoke ( this, new StateChangeArgs ( ) { State = this.state } );
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment