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

public class Water : MonoBehaviour
{
    public Transform bottom;
    Vector2 cupBottom;
    float height;
    public float remainHeight;//剩余液面高度，如果太低游戏结束


    // Start is called before the first frame update
    void Start()
    {
        cupBottom = bottom.position;
        height = transform.position.y - cupBottom.y;
        remainHeight = height;
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void MoveDown(float distancePercent)
    {
        float distance = distancePercent * height;
        remainHeight -= distance;
        Vector2 pos = transform.position + new Vector3(0, -distance);
        StartCoroutine(MoveOverTime(pos, 0.5f));

    }



    // 协程：平滑移动物体
    private IEnumerator MoveOverTime(Vector2 target, float time)
    {
        Vector2 startPosition = transform.position;
        float elapsedTime = 0f;

        while (elapsedTime < time)
        {
            transform.position = Vector2.Lerp(startPosition, target, elapsedTime / time);
            elapsedTime += Time.deltaTime;  // 通过时间来递增
            yield return null; // 等待下一帧
        }

        // 确保最终位置精确设置
        transform.position = target;
    }

    internal bool IsTooLow()
    {
        return remainHeight / height <= 0.2;
    }
}
