using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MEC;

public class PlanetRotator : MonoBehaviour
{
    private Vector3 rotationEulers;
    private CoroutineHandle rotationHandle;
    public bool applyScarler = false;
    public float scalerFactor;

    private void Start()
    {
        rotationHandle = Timing.RunCoroutine(C_RotatePlanet());

        if(applyScarler)
        {
            scalerFactor = Random.Range(1f, 3f);
            transform.localScale = new Vector3(scalerFactor, scalerFactor, scalerFactor);
        }
    }

    private void OnBecameVisible()
    {
        Timing.ResumeCoroutines(rotationHandle);
    }

    private void OnBecameInvisible()
    {
        Timing.PauseCoroutines(rotationHandle);
    }

    private IEnumerator<float> C_RotatePlanet()
    {
        rotationEulers = new Vector3(Random.Range(-3, 4), Random.Range(-3, 4), Random.Range(-3, 4)) * Time.deltaTime;
        while (true)
        {
            transform.Rotate(rotationEulers);

            yield return Timing.WaitForOneFrame;
        }
    }
}
