playMaker

Author Topic: ShowDamageColor  (Read 2793 times)

ransomink

  • Playmaker Newbie
  • *
  • Posts: 44
ShowDamageColor
« on: January 06, 2018, 09:22:08 PM »
Since I made a knockback effect, I figured showing a damage color or flash on that object worked hand-in-hand. Hopefully, someone will find it as useful. I'm not sure if it belongs in the Color or GameObject category though...

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2018. All rights reserved.
// author : ransomink
// Keywords: color, damage
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{

    [ActionCategory(ActionCategory.Color)]
    [Tooltip("Apply a damage color to the GameObject.")]
    public class ShowDamageColor : FsmStateAction
    {
        [RequiredField]
        [UIHint(UIHint.Variable)]
        [Tooltip("Show damage color on this GameObject.")]
        [CheckForComponent(typeof(Renderer))]
        public FsmOwnerDefault gameObject;

        [RequiredField]
        [Tooltip("Color used for damage effect.")]
        public FsmColor damageColor;

        [RequiredField]
        [Tooltip("Duration of color change effect.")]
        [HasFloatSlider(0, 1)]
        public FsmFloat duration;

        [Tooltip("Use unscaled time. Not affected by Time.timeScale (slow motion effects).")]
        public bool realTime;

        /// <summary>
        /// Renderer of the GameObject.
        /// </summary>
        private Renderer _renderer;

        /// <summary>
        /// Default material color of the GameObject.
        /// </summary>
        private Color _defaultColor = Color.clear;

        /// <summary>
        /// Current material color of the GameObject.
        /// </summary>
        private Color _currentColor = Color.black;

        /// <summary>
        /// Duration of the damage color.
        /// </summary>
        private float _colorDuration;

        /// <summary>
        /// Beginning of color change.
        /// </summary>
        private float _startTime;

        /// <summary>
        /// End of color change.
        /// </summary>
        private float _endTime;

        /// <summary>
        /// Change GameObject color?
        /// </summary>
        private bool _damageColor;

        /// <summary>
        /// Get the current time.
        /// </summary>
        private float CurrentTime
        {
            get { return realTime ? Time.unscaledTime : Time.time; }
        }

        public override void Reset()
        {
            gameObject  = null;
            damageColor = Color.red;
            duration    = 0.5f;
            realTime    = false;
        }

        public override void OnEnter()
    {
            var go = Fsm.GetOwnerDefaultTarget( gameObject );

            _renderer      = go.GetComponent<Renderer>();
            _defaultColor  = _renderer.material.color;
            _colorDuration = duration.Value / 2;

            DoDamageColor();
    }

        public override void OnUpdate()
        {
            if ( _damageColor )
            {
                if ( CurrentTime >= _endTime )
                {
                    _damageColor = false;
                    SetTimer( _colorDuration );
                }

                ChangeColor( _damageColor );
            }
            else
            {
                if ( CurrentTime >= _endTime )
                {
                    Finish();
                }

                ChangeColor( _damageColor );
            }
        }

        void DoDamageColor()
        {
            _currentColor = _renderer.material.color;

            // reset color?

            if ( _currentColor != _defaultColor )
            {
                ResetColor();
            }

            SetTimer( _colorDuration );
            _damageColor = true;
        }

        void ResetColor()
        {
            if ( _defaultColor == Color.clear )
            {
                LogError( "Default Color not set on GameObject." );
                return;
            }

            _renderer.material.color = _defaultColor;
        }

        void ChangeColor( bool on )
        {
            if ( on )
            {
                _renderer.material.color = Color.Lerp( _currentColor, damageColor.Value, Mathf.InverseLerp( _startTime, _endTime, CurrentTime ) );
            }
            else
            {
                _renderer.material.color = Color.Lerp( damageColor.Value, _currentColor, Mathf.InverseLerp( _startTime, _endTime, CurrentTime ) );
            }
        }

        void SetTimer( float timer )
        {
            _startTime = CurrentTime;
            _endTime   = _startTime + timer;
        }
    }

}
« Last Edit: January 07, 2018, 06:26:58 PM by ransomink »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: ShowDamageColor
« Reply #1 on: January 07, 2018, 09:13:58 AM »
Hi.
Nice action, i am not sure if it is good to put this on the Ecosystem as it is kinda a specific action.
But if you could export the action and place it as an attachment it would be easier for people to get the action from the attachment instead of copy/paste the code into a c# script

To do this you can select your script and then right-click and select export package, then turn of  'Include dependencies' and save it somewhere.

ransomink

  • Playmaker Newbie
  • *
  • Posts: 44
Re: ShowDamageColor
« Reply #2 on: January 07, 2018, 06:30:17 PM »
Sure, no problem. I created it to work along with the KnockbackGameObject action. OP is updated with the unity package...

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: ShowDamageColor
« Reply #3 on: January 11, 2018, 02:25:05 PM »
Does it work with white?  ;D
Available for Playmaker work

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: ShowDamageColor
« Reply #4 on: January 11, 2018, 05:30:30 PM »
Hi.
Maybe set colors public?  (FsmColor)