From 4403ab45988689172a84d23a5d90302579ea4b8e Mon Sep 17 00:00:00 2001 From: kimura Date: Tue, 30 Nov 2021 18:08:58 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=8F=E3=83=BC=E3=83=88=E3=81=8C=E3=82=B2?= =?UTF-8?q?=E3=83=BC=E3=82=B8=E5=8A=A0=E7=AE=97=E6=96=B9=E6=B3=95=E5=A4=89?= =?UTF-8?q?=E6=9B=B4/=20=E3=83=8F=E3=83=BC=E3=83=88=E3=82=B2=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E6=BA=80=E3=82=BF=E3=83=B3=E6=99=82=E3=81=AB=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E3=81=99=E3=82=8BFulledHeart=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- popcorn/Assets/MyGame/Scripts/HeartMeter.cs | 31 ++++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) 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