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

namespace MPStudio
{
    public class FlyToCoin : MonoBehaviour
    {
        public float flyCost = 1f;
        
        private Vector3 initPos;

        public RectTransform coinRect;

        private Vector3 targetPos;
        
        public Camera uiCamera; 
        
        private void Awake()
        {
            initPos = transform.localPosition;
        }
        
        public Vector3 GetTextWorldPosition()
        {
            RectTransform textRect = coinRect;
        
            // 获取Text在屏幕空间的位置（中心点）
            Vector3 textScreenPos = RectTransformUtility.WorldToScreenPoint(
                uiCamera != null ? uiCamera : Camera.main,
                textRect.position
            );
        
            // 转换为世界坐标
            Vector3 textWorldPos = Camera.main.ScreenToWorldPoint(new Vector3(
                textScreenPos.x,
                textScreenPos.y,
               10 // 确保Z值在相机可见范围内
            ));
        
            return textWorldPos;
        }

        // Start is called before the first frame update
        void Start()
        {
            targetPos =new Vector3(2.9f,2.14f,0) ;//GetTextWorldPosition();
            

          StartCoroutine(  TweenManager.Inst.ToVec3(initPos,targetPos, this.flyCost,(v3) =>
            {
                transform.localPosition = v3;
            }, () =>
            {
              //  print("over");
            }));
        }
    }
}