Liberi
An exergame built for kids with CP!
GUIStatusMeter.cs
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections;
4 
8 public class GUIStatusMeter : MonoBehaviour
9 {
10  private static GUIStatusMeter _instance;
11 
12  public float EnergyBarEaseFactor;
13  public float HeartScaleEaseFactor;
14  public float HeartMinScale;
15  public float HeartMaxScale;
16 
17  Image _energyBarFill;
18  Transform _heartTransform;
19  Character _character;
20  Animator _heartAnim;
21 
22  int _isTargetHash = Animator.StringToHash("IsTarget");
23  int _isExtremeHash = Animator.StringToHash("IsExtreme");
24 
28  public static void RegisterEnergyProvider(Character provider)
29  {
30  _instance._character = provider;
31  }
32 
33  void Awake()
34  {
35  _instance = this;
36  _energyBarFill = transform.FindChild("energy_bar/fill").GetComponent<Image>();
37  _heartTransform = transform.FindChild("heart");
38  _heartAnim = _heartTransform.GetComponent<Animator>();
39  }
40 
41  void Start()
42  {
43  Heart.TierChanged += OnHeartTierChange;
44  }
45 
46  void OnHeartTierChange(HeartTier newTier, HeartTier prevTier)
47  {
48  _heartAnim.SetBool(_isTargetHash, newTier == HeartTier.Target);
49  _heartAnim.SetBool(_isExtremeHash, newTier == HeartTier.Extreme);
50  }
51 
52  void Update()
53  {
54  if (_character != null)
55  {
56  _energyBarFill.fillAmount = Mathf.Lerp(_energyBarFill.fillAmount, _character.Energy, Time.deltaTime * EnergyBarEaseFactor);
57  _energyBarFill.SetAllDirty();
58  }
59  else
60  _energyBarFill.fillAmount = Mathf.Lerp(_energyBarFill.fillAmount, 0, Time.deltaTime * EnergyBarEaseFactor);
61 
62  _heartTransform.localScale = Vector3.Lerp(_heartTransform.localScale,
63  Vector3.one * Mathf.Clamp(Heart.NormalizedRate, HeartMinScale, HeartMaxScale),
64  Time.deltaTime * HeartScaleEaseFactor);
65  }
66 }
A visual representation of HeartRate and Character.Energy.
Heart class. Allows for heart rate queries and heart rate injection from devices. ...
Definition: Heart.cs:20
static void RegisterEnergyProvider(Character provider)
Register an instance of a Character (usually the player's local avatar's Character component) to act ...
Avatar character management component. In charge of personas, skills and buffs.
Definition: Character.cs:12
static float NormalizedRate
Gets the current heart rate divded by the target heart rate.
Definition: Heart.cs:65
static HeartTierChangedHandler TierChanged
Event fired when the heart rate tier changes.
Definition: Heart.cs:25