using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class FastSlowAnimationANDZoomIN : MonoBehaviour
{
    public Camera cam;
    public Camera workerCamera;
    //Animator anim;

    public float maxHeight = 2f;      // 最大上升高度（米）
                                      // float scaleRation = 0.5f;//默认缩放系数
    public float upTime = 1.5f;// 上升到最高点所需时间（秒）

    private Dictionary<Transform, Vector3> childrenStartPos = new Dictionary<Transform, Vector3>();
    // Start is called before the first frame update
    void Start()
    {
        cam = Camera.main;
        // workerCamera = GameObject.Find("WorkerCamera").GetComponent<Camera>();zhe行代码为什么会引起 bug？目前还不清楚

        foreach (Transform child in transform)
        {
            childrenStartPos[child] = child.position;

        }
    }

    // Update is called once per frame
    void LateUpdate()
    {
        if (cam.gameObject.activeInHierarchy == true)
        {
            //自由视角下的动画逻辑
            foreach (Transform child in transform)//每个孩子就是一个世界空间的大画布
            {
                //通过遍历每个子物体,每个物体的大小是独立计算的，不能取最小值
                float dist = Vector3.Distance(child.position, cam.transform.position);
                float scale = dist / 60f;
                scale = Mathf.Clamp(scale, 0.001f, 10f);

                maxHeight = 0.05f + (dist - 1f) * 0.9f / 49f;
                maxHeight = Mathf.Clamp(maxHeight, 0.05f, 2f);

                float yOffset = Mathf.PingPong(Time.time * (maxHeight / upTime), maxHeight);

                child.position = childrenStartPos[child] + new Vector3(0, yOffset, 0);
                child.localScale = Vector3.one * scale;
                child.LookAt(child.position + cam.transform.forward);//看相机
                                                                     //transform.LookAt(transform.position + target.forward);
            }
        }
        else
        {
            //切换到工人视角下所放和浮动在这里
            foreach (Transform child in transform)//每个孩子就是一个世界空间的大画布
            {


                float yOffset = Mathf.PingPong(Time.time * (0.3f / upTime), 0.3f);

                child.position = childrenStartPos[child] + new Vector3(0, yOffset, 0);
                child.localScale = Vector3.one * 0.3f;
                child.LookAt(child.position + workerCamera.transform.forward);//看相机
                                                                              //transform.LookAt(transform.position + target.forward);
            }
        }
    }
}