using System; using AibisDream.Framework; using AibisDream.Kit; using DG.Tweening; using UnityEngine; namespace AibisDream { public class DeepInClock : MonoBehaviour { #region 索引 private MinHand _minHand; private Transform _secondHand; private Transform _hourHand; private SpriteLooper _cityLooper; public event Action OnClockReached; public event Action OnClockEnd; #endregion #region 参数 [Header("背景切换时长")] public int cityLoopDuration; [Header("开头全程所需时间")] public float rotateSpeed; private bool _isReached; #endregion // 按分钟计算,全程1440分钟 private BindProperty _curTime; public void Enable() { gameObject.SetActive(true); _minHand = transform.Find("fenzhen").GetComponent(); _secondHand = transform.Find("miaozhen"); _hourHand = transform.Find("shizhen"); _cityLooper = transform.Find("City").GetComponent(); _curTime = new BindProperty(); _curTime.Register(UpdateCityLooper); _minHand.OnMinHandReached += StartAutoRotate; _minHand.OnMinHandChanged += UpdateCurTime; _minHand.IsActive = true; } private void UpdateCityLooper(float curTime) { // 时间转为背景循环值 var loopValue = curTime % cityLoopDuration / cityLoopDuration; _cityLooper.SetLoopProgress(loopValue); // 处理时针 var hourNum = curTime / 1440; _hourHand.eulerAngles = new Vector3(0, 0, hourNum * 360); // 处理分针 var minNum = curTime % 60 / 60; _minHand.transform.eulerAngles = new Vector3(0, 0, minNum * 360); if (_curTime.Value > 1200 && !_isReached) { _isReached = true; OnClockReached?.Invoke(); } } private void StartAutoRotate() { DOTween.To(() => _curTime.Value, value => _curTime.Value = value, 1440f, rotateSpeed) .SetEase(Ease.InQuad).OnComplete(OnRotateEnd); } private void UpdateCurTime(float minHandAngle) { _curTime.Value = minHandAngle / 360 * 60; } private void OnRotateEnd() { gameObject.SetActive(false); OnClockEnd?.Invoke(); } } }