arthurdiniz0_outer-math/OuterMath Unity Project/Assets/PlayMaker/Actions/Animation/SetAnimationWeight.cs
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
	[ActionCategory(ActionCategory.Animation)]
	[Tooltip("Sets the Blend Weight of an Animation. Check Every Frame to update the weight continuously, e.g., if you're manipulating a variable that controls the weight.")]
	public class SetAnimationWeight : BaseAnimationAction
	{
		[RequiredField]
		[CheckForComponent(typeof(Animation))]
        [Tooltip("The Game Object playing the animation.")]
		public FsmOwnerDefault gameObject;
		[RequiredField]
        [Tooltip("The name of the animation.")]
        [UIHint(UIHint.Animation)]
		public FsmString animName;
        [Tooltip("The weight to set the animation to.")]
        public FsmFloat weight = 1f;
        [Tooltip("Perform this action every frame. Useful if Weight is a variable.")]
        public bool everyFrame;

		public override void Reset()
		{
			gameObject = null;
			animName = null;
			weight = 1f;
			everyFrame = false;
		}

		public override void OnEnter()
		{
			DoSetAnimationWeight(gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value);

		    if (!everyFrame)
		    {
		        Finish();
		    }		
		}

		public override void OnUpdate()
		{
			DoSetAnimationWeight(gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value);
		}

	    private void DoSetAnimationWeight(GameObject go)
		{
		    if (!UpdateCache(go))
		    {
		        return;
		    }

			var anim = animation[animName.Value];
			if (anim == null)
			{
				LogWarning("Missing animation: " + animName.Value);
				return;
			}

			anim.weight = weight.Value;
		}
	}
}