﻿using UnityEngine;

namespace Dissonance.Integrations.DarkRift2.Demo
{
    // ReSharper disable once InconsistentNaming
    public class DR2RemotePlayer : MonoBehaviour
    {
        public ushort ID { get; set; }

        public Vector3? TargetPosition { get; set; }
        public Quaternion? TargetRotation { get; set; }

        void Update ()
        {
            var trans = transform;

            if (TargetPosition.HasValue)
            {
                if (Vector3.SqrMagnitude(TargetPosition.Value - trans.position) < 0.25f)
                {
                    //They're very close, jump to the target position
                    trans.position = TargetPosition.Value;
                    TargetPosition = null;
                }
                else
                {
                    //They're not close, lerp smoothly to target position
                    trans.position = Vector3.Lerp(trans.position, TargetPosition.Value, 0.1f);
                }
            }

            if (TargetRotation.HasValue)
            {
                if (Mathf.Abs(Quaternion.Angle(TargetRotation.Value, trans.rotation)) < 3)
                {
                    //They're very close, jump to the target rotation
                    trans.rotation = TargetRotation.Value;
                    TargetRotation = null;
                }
                else
                {
                    //They're not close, lerp smoothly to target rotation
                    trans.rotation = Quaternion.Lerp(trans.rotation, TargetRotation.Value, 0.1f);
                }
            }
        }
    }
}
