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

public class CloudController : MonoBehaviour
{
    float MIN_X = -715;
    float MAX_X = 715;
    float MIN_Y = -125;
    float MAX_Y = 380;

    float xDelta = 0;
    float yDelta = 0;

    public void SetStartPosition()
    {
        gameObject.transform.localPosition = new Vector3(Random.Range(MIN_X, MAX_X), Random.Range(MIN_Y, MAX_Y), 0);
        ResetXDelta();
        ResetYDelta();
    }

    void Start()
    {

    }

    void ResetXDelta(int sign = 0)
    {
        if (sign == 0)
            sign = (Random.Range(0, 2) == 0 ? -1 : 1);
        xDelta = Random.Range(400, 600) * sign;
    }

    void ResetYDelta(int sign = 0)
    {
        if (sign == 0)
            sign = (Random.Range(0, 2) == 0 ? -1 : 1);
        yDelta = Random.Range(400, 600) * sign;
    }

    void Update()
    {
        var newPos = gameObject.transform.localPosition + new Vector3(xDelta * Time.deltaTime, yDelta * Time.deltaTime, 0);

        if (newPos.x < MIN_X || newPos.x > MAX_X)
        {
            ResetXDelta(xDelta < 0 ? 1 : -1);
            ResetYDelta(yDelta < 0 ? -1 : 1);
        }

        if (newPos.y < MIN_Y || newPos.y > MAX_Y)
        {
            ResetXDelta(xDelta < 0 ? -1 : 1);
            ResetYDelta(yDelta < 0 ? 1 : -1);
        }

        newPos.x = Mathf.Clamp(newPos.x, MIN_X, MAX_X);
        newPos.y = Mathf.Clamp(newPos.y, MIN_Y, MAX_Y);

        gameObject.transform.localPosition = newPos;
    }
}
