SHARE
TWEET

Untitled

a guest Mar 19th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class Node : MonoBehaviour  
  5. {
  6.     public enum NodeState { Full, Partial, Depleted }
  7.     public int NodeSize = 10;
  8.     public int NodeRegenAmount = 1;
  9.     public float RegenTime = 5.0f;
  10.     public delegate void ChangeInNodeState(NodeState state);
  11.     public ChangeInNodeState changeInNodeState;
  12.  
  13.     private NodeState nodeState = NodeState.Full;
  14.     [SerializeField]private int NodeValue = 0;
  15.     private Coroutine regenerateNode;
  16.  
  17.     void Start()
  18.     {
  19.         changeInNodeState = NodeStateHandler;       // Initializes Delegate Listener
  20.         SetNodeValue(NodeSize);                     // Initiates Node
  21.     }
  22.  
  23.     #region Public Methods
  24.     public void SetNodeValue(int value)
  25.     {
  26.         NodeValue = Mathf.Clamp(value, 0, NodeSize);
  27.         UpdateNodeState();
  28.     }
  29.  
  30.     public void AddToNodeValue(int value)
  31.     {
  32.         NodeValue = NodeValue + value;
  33.         NodeValue = Mathf.Clamp(NodeValue, 0, NodeSize);
  34.         UpdateNodeState();
  35.     }
  36.  
  37.     public void SubtractFromNodeValue(int value)
  38.     {
  39.         NodeValue = NodeValue - value;
  40.         NodeValue = Mathf.Clamp(NodeValue, 0, NodeSize);
  41.         UpdateNodeState();
  42.     }
  43.  
  44.     public NodeState GetNodeState() { return nodeState; }
  45.     public int GetNodeValue() { return NodeValue; }
  46.     #endregion
  47.  
  48.     #region Private Methods
  49.     private void UpdateNodeState()
  50.     {
  51.         if (NodeValue == NodeSize)
  52.         {
  53.             nodeState = NodeState.Full;
  54.         }
  55.         else if ((NodeValue > 0) && (NodeValue < NodeSize))
  56.         {
  57.             nodeState = NodeState.Partial;
  58.         }
  59.         else
  60.         {
  61.             nodeState = NodeState.Depleted;
  62.         }
  63.  
  64.         changeInNodeState(nodeState);
  65.     }
  66.  
  67.     private void NodeStateHandler(NodeState state)
  68.     {
  69.         switch (state)
  70.         {
  71.             case NodeState.Full:
  72.                 Debug.Log("Node State Fully Replenished!");
  73.                 break;
  74.             case NodeState.Partial:
  75.                 if (regenerateNode == null) regenerateNode = StartCoroutine(NodeRegenerator(RegenTime));
  76.                 break;
  77.             case NodeState.Depleted:
  78.                 if (regenerateNode == null) regenerateNode = StartCoroutine(NodeRegenerator(RegenTime));
  79.                 break;
  80.         }
  81.     }
  82.  
  83.     private IEnumerator NodeRegenerator(float regenInterval)
  84.     {
  85.         yield return new WaitForSeconds(regenInterval);
  86.         regenerateNode = null;
  87.         SetNodeValue(NodeValue + NodeRegenAmount);
  88.     }
  89.     #endregion
  90.  
  91.     void Update()
  92.     {
  93.         TemporaryControls();
  94.     }
  95.  
  96.     void TemporaryControls()
  97.     {
  98.         if (Input.GetKeyDown(KeyCode.Q)) { SetNodeValue(5); }
  99.         if (Input.GetKeyDown(KeyCode.W)) { SetNodeValue(0); }
  100.     }
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top