using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CapsuleCollider))]
public class DraftZone : MonoBehaviour
{
    public float strength;
    public float radius;
    public Vector3 offset;

    private CapsuleCollider _collider;

    private void Start()
    {
        _collider = (CapsuleCollider)gameObject.collider;
        _collider.isTrigger = true;
        _collider.radius = .5f;
        _collider.height = strength / transform.localScale.y;
        _collider.center = new Vector3(0, strength * .45f / transform.localScale.y, 0);
    }

    public Vector3 GetDraft(Vector3 otherWorldPosition)
    {
        Vector3 relativePosition = otherWorldPosition - transform.position;

        if (relativePosition.y <= 0)
            return Vector3.zero;

        float power = strength - relativePosition.y;
        float sinVal = power / strength * Mathf.PI * .5f;

        return relativePosition.normalized * Mathf.Sin(sinVal) * power * 12.5f;
    }

    private void OnDrawGizmos()
    {
        Random.seed = 42;
        for (int i = 0; i < 5 * strength; i++)
        {
            Vector2 pos = radius * Random.insideUnitCircle;

            Vector3 actualBottomPoint = transform.position + offset + transform.rotation * (new Vector3(pos.x, 0, pos.y));
            Vector3 distancePoint = actualBottomPoint + Random.Range(strength * .4f, strength) * (transform.rotation * Vector3.up);

            Gizmos.DrawLine(actualBottomPoint, distancePoint);
        }
    }
}
