using System.Collections;
using UnityEngine;

public class ShipLogic : MonoBehaviour
{
    public float maxFallingTime = 240;
    public float minSaveEscapeTime = 120;
    public float maxSaveEscapeTime = 160;


    private float maxHeight = 30000;
    private float saveHeightBegin;
    private float saveHeightEnd;
    public bool isItSaveWindow;

    private float currentHeight;
    private float speed;

    public float getcurrentAltitude()
    { return currentHeight; }

    void Start()
    {
        currentHeight = maxHeight;

        saveHeightBegin = minSaveEscapeTime / maxFallingTime * maxHeight;
        saveHeightEnd = maxSaveEscapeTime / maxFallingTime * maxHeight;

        speed = maxHeight / maxFallingTime;

        StartCoroutine(FallingDown());
    }

    void Update()
    {

    }

    private bool ChackifitisSave()
    {
        return currentHeight < saveHeightEnd && currentHeight > saveHeightBegin;
    }

    IEnumerator FallingDown()
    {
        while (true)
        {
            currentHeight -= Time.deltaTime * speed;

            isItSaveWindow = ChackifitisSave();

            if (currentHeight < 0)
            {
                currentHeight = 0;
                yield break;
            }
            else
            {
                yield return null;
            }
        }
    }

}
