﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Arugula.Math;

namespace Arugula.Prototypical
{
    public class RandomAngularBump : MonoBehaviour
    {
        public Vector3 min;
        public Vector3 max;

        public float minDelay;
        public float maxDelay;
        public bool repeat = true;
        public Space space = Space.World;

        float nextBumpTime;
        Rigidbody rb;
        private void Start()
        {
            rb = GetComponent<Rigidbody>();
            if (rb == null)
                this.enabled = false;

            nextBumpTime = Time.time + Random.Range(minDelay, maxDelay);
        }

        private void FixedUpdate()
        {
            if (Time.time > nextBumpTime)
            {
                if (space == Space.World)
                    rb.AddTorque(Mathemagic.RandomVector3(min, max), ForceMode.VelocityChange);
                else
                    rb.AddRelativeTorque(Mathemagic.RandomVector3(min, max), ForceMode.VelocityChange);

                if (repeat)
                    nextBumpTime = Time.time + Random.Range(minDelay, maxDelay);
                else
                    this.enabled = false;
            }
        }
    }

}
