/// <summary>
        /// Handle changes to the health attribute
        /// </summary>
        /// <param name="rAttribute"></param>
        /// <param name="rOldValue"></param>
        protected virtual void OnAttributeValueChanged(BasicAttribute rAttribute, object rOldValue)
        {
            BasicAttributeFloat lAttributeFloat = rAttribute as BasicAttributeFloat;

            if (lAttributeFloat == null ||
                lAttributeFloat.ID.ToUpperInvariant() != _HealthKey.ToUpperInvariant())
            {
                return;
            }

            if (_ShowDebugInfo)
            {
                Debug.Log(string.Format("[{0}] {1} value changed: {2} [{3}]", GetType().Name,
                                        lAttributeFloat._ID, lAttributeFloat.GetValue <float>(), (float)rOldValue));
            }

            if (_UseEasing)
            {
                StartCoroutine(ChangeSliderValue(lAttributeFloat.Value, _HealthBar));
            }
            else
            {
                _HealthBar.value = lAttributeFloat.Value;
            }
        }
 /// <summary>
 /// Set up the Slider's min, max, and current values
 /// </summary>
 /// <param name="rAttributeFloat"></param>
 /// <param name="rSlider"></param>
 protected virtual void SetupSlider(BasicAttributeFloat rAttributeFloat, Slider rSlider)
 {
     //Debug.Log(string.Format("[SetupSlider] MinValue: {0}  MaxValue: {1}", rAttributeFloat.MinValue, rAttributeFloat.MaxValue));
     // If no Max Value specified, use the Value entered for the attribute
     rSlider.maxValue = Math.Abs(rAttributeFloat.MaxValue - float.MaxValue) < 0.01 ? rAttributeFloat.Value : rAttributeFloat.MaxValue;
     // If no Min Value specified, use 0
     rSlider.minValue = Math.Abs(rAttributeFloat.MinValue - float.MinValue) < 0.01 ? 0 : rAttributeFloat.MinValue;
     rSlider.value    = rAttributeFloat.Value;
 }
Example #3
0
        /// <summary>
        /// Create the BasicAttributes and add the Health attribute
        /// </summary>
        /// <param name="rMotionController"></param>
        /// <param name="rHealth">Health value to set; if the Health attribute already exist, then its value
        /// is not overwritten</param>
        /// <param name="rMana"></param>
        /// <param name="rStamina"></param>
        /// <returns></returns>
        public static BasicAttributes CreateBasicAttributes(MotionController rMotionController, float rHealth, float rMana = 0, float rStamina = 0)
        {
            BasicAttributes lAttributes = rMotionController.GetOrAddComponent <BasicAttributes>();

            if (!lAttributes.AttributeExists("Health"))
            {
                lAttributes.SetAttributeValue <float>("Health", rHealth);
                BasicAttributeFloat lHealth = lAttributes.GetAttribute("Health") as BasicAttributeFloat;
                if (lHealth != null)
                {
                    lHealth.MinValue = 0;
                    lHealth.MaxValue = rHealth;
                }
            }

            if (rMana > 0 && !lAttributes.AttributeExists("Mana"))
            {
                lAttributes.SetAttributeValue <float>("Mana", rMana);
                BasicAttributeFloat lMana = lAttributes.GetAttribute("Mana") as BasicAttributeFloat;
                if (lMana != null)
                {
                    lMana.MinValue = 0;
                    lMana.MaxValue = rMana;
                }
            }

            if (rStamina > 0 && !lAttributes.AttributeExists("Stamina"))
            {
                lAttributes.SetAttributeValue <float>("Stamina", rStamina);
                BasicAttributeFloat lStamina = lAttributes.GetAttribute("Stamina") as BasicAttributeFloat;
                if (lStamina != null)
                {
                    lStamina.MinValue = 0;
                    lStamina.MaxValue = rMana;
                }
            }

            lAttributes.OnBeforeSerialize();

            return(lAttributes);
        }
        protected virtual void Start()
        {
            // If no Slider was assigned or found, don't continue initializing
            if (_HealthBar == null)
            {
                return;
            }

            // If this HUD is for the player, attempt to find the player
            if (_UsePlayer)
            {
                var player = GameObject.FindGameObjectWithTag("Player");
                if (player != null)
                {
                    _BasicAttributes = player.GetComponent <BasicAttributes>();
                }
            }
            if (_BasicAttributes == null)
            {
                return;
            }

            BasicAttributeFloat lHealthAttribute = _BasicAttributes.GetAttribute(_HealthKey) as BasicAttributeFloat;

            if (lHealthAttribute == null)
            {
                return;
            }

            // Set the slider min and max values
            SetupSlider(lHealthAttribute, _HealthBar);

            // Subscribe to change notifications
            _BasicAttributes.OnAttributeValueChangedEvent += OnAttributeValueChanged;

            mIsInitialized = true;

            SetVisibility(CheckVisibilityState());
        }