﻿using UnityEngine;

namespace pdinklag.CPW.Common
{
    /// <summary>
    /// Abstract base for body part components approximated by a height and radius.
    /// </summary>
    public abstract class BodyPartCylindrical : BodyPartVolumetric, IBodyPart
    {
        public float Height = 0.1f;
        public float Radius = 0.1f;

        public Vector3 Size
        {
            get
            {
                var d = 2.0f * Radius;
                return new(d, Height, d);
            }
        }

        public override Vector3 Top { get { return transform.position + 0.5f * Height * transform.up; } }
        public override Vector3 Bottom { get { return transform.position - 0.5f * Height * transform.up; } }
        public override Vector3 Right { get { return transform.position + Radius * transform.right; } }
        public override Vector3 Left { get { return transform.position - Radius * transform.right; } }
        public override Vector3 Front { get { return transform.position + Radius * transform.forward; } }
        public override Vector3 Back { get { return transform.position - Radius * transform.forward; } }

        public override Collider CreateCollider(GameObject go)
        {
            var c = go.AddComponent<CapsuleCollider>();
            c.height = Height;
            c.radius = Radius;
            return c;
        }

        public override void CopyData(BodyPartComponent other)
        {
            base.CopyData(other);

            var cyl = other as BodyPartCylindrical;
            if (cyl)
            {
                Height = cyl.Height;
                Radius = cyl.Radius;
            }
        }
    }
}
