using System;
using UnityEngine;

namespace pdinklag
{
    /// <summary>
    /// Caches an Animator's bone transforms for quick access using the HumanBodyBones enumeration.
    /// </summary>
    public class AnimatorBones
    {
        private readonly Transform[] _bones = new Transform[(int)HumanBodyBones.LastBone];

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="anim">the animator (must be humanoid)</param>
        public AnimatorBones(Animator anim)
        {
            if (!anim.isHuman) throw new ArgumentException("Animator must be humanoid");

            for (var i = 0; i < (int)HumanBodyBones.LastBone; i++)
            {
                _bones[i] = anim.GetBoneTransform((HumanBodyBones)i);
            }
        }

        /// <summary>
        /// Gets the specified bone transform from the cache.
        /// </summary>
        /// <param name="bone">the bone to get</param>
        /// <returns>the bone transform</returns>
        public Transform this[HumanBodyBones bone] => _bones[(int)bone];
    }
}
