diff --git a/popcorn/Assets/MyGame/Scripts/HeartMeter.cs b/popcorn/Assets/MyGame/Scripts/HeartMeter.cs index 1700a6e0..37418a60 100644 --- a/popcorn/Assets/MyGame/Scripts/HeartMeter.cs +++ b/popcorn/Assets/MyGame/Scripts/HeartMeter.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using TMPro; using UniRx; +using UniRx.Triggers; using UnityEngine; using UnityEngine.UI; @@ -11,14 +12,17 @@ public class HeartMeter : SingletonMonoBehaviour [SerializeField] private Slider slider; [SerializeField] private TextMeshProUGUI heartLevel; [SerializeField] private GameObject maxObject; + [SerializeField] private float duration = .5f; private Coroutine coroutine; private List shopLevelList = new List(); private int currentHeartCount; private int maxLevel; + private readonly CompositeDisposable compositeDisposable = new CompositeDisposable(); private readonly ReactiveProperty viewHeartCount = new ReactiveProperty(); private readonly ReactiveProperty shopLevel = new ReactiveProperty(); - private CompositeDisposable compositeDisposable = new CompositeDisposable(); + private readonly ReactiveProperty fulledHeart = new ReactiveProperty(); + public IObservable FulledHeart => fulledHeart.DistinctUntilChanged(); private float minHeart; private float maxHeart; @@ -32,7 +36,11 @@ public class HeartMeter : SingletonMonoBehaviour { shopLevel.AddTo(this); viewHeartCount.AddTo(this); + fulledHeart.AddTo(this); compositeDisposable.AddTo(this); +#if UNITY_EDITOR + fulledHeart.Subscribe(x => { Debug.Log($"fulled:{x}"); }); +#endif } public void Initialize(int newShopLevel = 0, int newHeartCount = 0) @@ -57,7 +65,7 @@ public class HeartMeter : SingletonMonoBehaviour SetShopLevel(newShopLevel); } - public void SetHeart(int heartCount) + private void SetHeart(int heartCount) { currentHeartCount = heartCount; viewHeartCount.SetValueAndForceNotify(heartCount); @@ -75,6 +83,7 @@ public class HeartMeter : SingletonMonoBehaviour maxHeart = shopLevelList.FirstOrDefault(data => data.shopLevel == level + 1)?.heart ?? minHeart; if (animate) { + fulledHeart.Value = false; var tmpCount = currentHeartCount - (int)minHeart; SetHeart((int)minHeart); AddHeart(tmpCount); @@ -82,19 +91,27 @@ public class HeartMeter : SingletonMonoBehaviour else { viewHeartCount.SetValueAndForceNotify(currentHeartCount); + fulledHeart.Value = currentHeartCount >= maxHeart && shopLevel.Value < maxLevel; } } public void AddHeart(int value) { this.SafeStopCoroutine(coroutine); currentHeartCount += value; - coroutine = this.CallWaitForSeconds(1f, () => + coroutine = this.CallWaitForSeconds(duration, () => { SetHeart(currentHeartCount); }); - this.CallLerp(1f, f => - { - viewHeartCount.Value = Mathf.Min(currentHeartCount, viewHeartCount.Value + value * f); - }); + var fixValue = Mathf.Min(value, maxHeart); + this.UpdateAsObservable() + .Select(_ => Time.deltaTime / duration) + .Take(TimeSpan.FromSeconds(duration)) + .Subscribe(t => + { + viewHeartCount.Value += fixValue * t; + }, () => + { + fulledHeart.Value = currentHeartCount >= maxHeart && shopLevel.Value < maxLevel; + }).AddTo(this); } } \ No newline at end of file