ハートがゲージ加算方法変更/ ハートゲージ満タン時に通知するFulledHeartを追加

This commit is contained in:
kimura 2021-11-30 18:08:58 +09:00
parent 63517fe9af
commit 4403ab4598
1 changed files with 24 additions and 7 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using TMPro; using TMPro;
using UniRx; using UniRx;
using UniRx.Triggers;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -11,14 +12,17 @@ public class HeartMeter : SingletonMonoBehaviour<HeartMeter>
[SerializeField] private Slider slider; [SerializeField] private Slider slider;
[SerializeField] private TextMeshProUGUI heartLevel; [SerializeField] private TextMeshProUGUI heartLevel;
[SerializeField] private GameObject maxObject; [SerializeField] private GameObject maxObject;
[SerializeField] private float duration = .5f;
private Coroutine coroutine; private Coroutine coroutine;
private List<ShopLevelData> shopLevelList = new List<ShopLevelData>(); private List<ShopLevelData> shopLevelList = new List<ShopLevelData>();
private int currentHeartCount; private int currentHeartCount;
private int maxLevel; private int maxLevel;
private readonly CompositeDisposable compositeDisposable = new CompositeDisposable();
private readonly ReactiveProperty<float> viewHeartCount = new ReactiveProperty<float>(); private readonly ReactiveProperty<float> viewHeartCount = new ReactiveProperty<float>();
private readonly ReactiveProperty<int> shopLevel = new ReactiveProperty<int>(); private readonly ReactiveProperty<int> shopLevel = new ReactiveProperty<int>();
private CompositeDisposable compositeDisposable = new CompositeDisposable(); private readonly ReactiveProperty<bool> fulledHeart = new ReactiveProperty<bool>();
public IObservable<bool> FulledHeart => fulledHeart.DistinctUntilChanged();
private float minHeart; private float minHeart;
private float maxHeart; private float maxHeart;
@ -32,7 +36,11 @@ public class HeartMeter : SingletonMonoBehaviour<HeartMeter>
{ {
shopLevel.AddTo(this); shopLevel.AddTo(this);
viewHeartCount.AddTo(this); viewHeartCount.AddTo(this);
fulledHeart.AddTo(this);
compositeDisposable.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) public void Initialize(int newShopLevel = 0, int newHeartCount = 0)
@ -57,7 +65,7 @@ public class HeartMeter : SingletonMonoBehaviour<HeartMeter>
SetShopLevel(newShopLevel); SetShopLevel(newShopLevel);
} }
public void SetHeart(int heartCount) private void SetHeart(int heartCount)
{ {
currentHeartCount = heartCount; currentHeartCount = heartCount;
viewHeartCount.SetValueAndForceNotify(heartCount); viewHeartCount.SetValueAndForceNotify(heartCount);
@ -75,6 +83,7 @@ public class HeartMeter : SingletonMonoBehaviour<HeartMeter>
maxHeart = shopLevelList.FirstOrDefault(data => data.shopLevel == level + 1)?.heart ?? minHeart; maxHeart = shopLevelList.FirstOrDefault(data => data.shopLevel == level + 1)?.heart ?? minHeart;
if (animate) if (animate)
{ {
fulledHeart.Value = false;
var tmpCount = currentHeartCount - (int)minHeart; var tmpCount = currentHeartCount - (int)minHeart;
SetHeart((int)minHeart); SetHeart((int)minHeart);
AddHeart(tmpCount); AddHeart(tmpCount);
@ -82,19 +91,27 @@ public class HeartMeter : SingletonMonoBehaviour<HeartMeter>
else else
{ {
viewHeartCount.SetValueAndForceNotify(currentHeartCount); viewHeartCount.SetValueAndForceNotify(currentHeartCount);
fulledHeart.Value = currentHeartCount >= maxHeart && shopLevel.Value < maxLevel;
} }
} }
public void AddHeart(int value) public void AddHeart(int value)
{ {
this.SafeStopCoroutine(coroutine); this.SafeStopCoroutine(coroutine);
currentHeartCount += value; currentHeartCount += value;
coroutine = this.CallWaitForSeconds(1f, () => coroutine = this.CallWaitForSeconds(duration, () =>
{ {
SetHeart(currentHeartCount); SetHeart(currentHeartCount);
}); });
this.CallLerp(1f, f => var fixValue = Mathf.Min(value, maxHeart);
{ this.UpdateAsObservable()
viewHeartCount.Value = Mathf.Min(currentHeartCount, viewHeartCount.Value + value * f); .Select(_ => Time.deltaTime / duration)
}); .Take(TimeSpan.FromSeconds(duration))
.Subscribe(t =>
{
viewHeartCount.Value += fixValue * t;
}, () =>
{
fulledHeart.Value = currentHeartCount >= maxHeart && shopLevel.Value < maxLevel;
}).AddTo(this);
} }
} }