Advertisement
apieceoffruit

Bar

Jul 7th, 2021 (edited)
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System;
  2. using DG.Tweening;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. namespace JasonStorey
  7. {
  8.     public class Bar : MonoBehaviour
  9.     {
  10.         [Header("Dependencies")]
  11.         [SerializeField]
  12.         Image _fill;
  13.         [SerializeField]
  14.         Image _buffer;
  15.         [SerializeField]
  16.         Image _flash;
  17.        
  18.         [Header("Settings")]
  19.         [SerializeField]
  20.         int _max = 100;
  21.         [SerializeField]
  22.         int _value = 20;
  23.  
  24.         public void TakeDamage(int amount)
  25.         {
  26.             float currentPercent = (float)_value / _max;
  27.             _value = Mathf.Max(0,_value - amount);
  28.             float targetPercent = (float)_value / _max;
  29.            
  30.             Fill = targetPercent;
  31.             _buffer.fillAmount = currentPercent;
  32.             Buffer = targetPercent;
  33.             Shake();
  34.         }
  35.  
  36.  
  37.        
  38.         #region Plumbing
  39.  
  40.         void Shake()
  41.         {
  42.             _shaker.Restart();
  43.             _shaker.Play();
  44.         }
  45.  
  46.         [ContextMenu("Replenish")]
  47.         public void Replenish()
  48.         {
  49.             _value = _max;
  50.             DOVirtual
  51.                 .Float(_fill.fillAmount, 1, 1f,x=>_fill.fillAmount = x)
  52.                 .SetEase(Ease.OutSine).OnComplete(Flash);
  53.         }
  54.        
  55.         float Fill
  56.         {
  57.             set => _fill.fillAmount = value;
  58.         }
  59.  
  60.         float Buffer
  61.         {
  62.             set
  63.             {
  64.                
  65.                 DOVirtual
  66.                     .Float(_buffer.fillAmount, value, 0.3f,x=>_buffer.fillAmount = x)
  67.                     .SetEase(Ease.InSine);
  68.             }
  69.         }
  70.  
  71.         public Color FlashColor;
  72.        
  73.         [ContextMenu("Flash")]
  74.         void Flash()
  75.         {
  76.             _flash.color = FlashColor;
  77.             _flash.DOColor(_fill.color,1f).SetEase(Ease.OutSine).OnComplete(()=>_flash.color = Color.clear);
  78.             Shake();
  79.         }
  80.  
  81.         void OnValidate()
  82.         {
  83.             Max = _max;
  84.             Value = _value;
  85.         }
  86.  
  87.         public int Value
  88.         {
  89.             get => _value;
  90.             set => _value = Mathf.Clamp(value, 0, _max);
  91.         }
  92.  
  93.         public int Max
  94.         {
  95.             get => _max;
  96.             set => _max = Mathf.Max(1, value);
  97.         }
  98.  
  99.         public float Percent
  100.         {
  101.             get => _fill.fillAmount;
  102.             set
  103.             {
  104.                 var val = Mathf.Lerp(0, Max, value);
  105.                 _value = (int) val;
  106.                 _fill.fillAmount = _buffer.fillAmount = val / Max;
  107.             }
  108.         }
  109.  
  110.         void Start()
  111.         {  
  112.             _shaker = transform.DOPunchPosition(Vector3.right * 6, 0.3f, 20).SetAutoKill(false);
  113.         }
  114.  
  115.         void OnDestroy()
  116.         {
  117.             _shaker.Kill();
  118.         }
  119.  
  120.         Tweener _shaker;
  121.  
  122.         #endregion
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement