using UnityEngine;
using System.Collections;

namespace mgaia.tk2d
{
    /// <summary>
    /// Behaviour para possibilitar alterar a escala da sprite através do Animation da Unity
    /// </summary>
    /// <owner>Victor</owner>
    [RequireComponent(typeof(tk2dBaseSprite))]
    [ExecuteInEditMode]
    public class Tk2dSpriteUnityScaleAdapter : MonoBehaviour
    {
        private const string UPDATE_VERTICES_METHOD_NAME = "UpdateVertices";

        private tk2dBaseSprite mBaseSprite;
        private Vector3 mOldScale;
        private Vector3 mInitialScale;

        private void Awake()
        {
            mBaseSprite = GetComponent<tk2dBaseSprite>();
            mInitialScale = mBaseSprite.scale;
        }

        private void OnStartAnimation()
        {
            mBaseSprite.scale = mInitialScale;

            UpdateScale();
        }

        private void Update()
        {
            if (!mOldScale.Equals(mBaseSprite.scale))
            {
                UpdateScale();
            }
        }

        private void UpdateScale()
        {
            mOldScale = mBaseSprite.scale;

            mBaseSprite.SendMessage(UPDATE_VERTICES_METHOD_NAME, SendMessageOptions.RequireReceiver);
        }
    }
}