ハートがゲージ加算方法変更/ ハートゲージ満タン時に通知する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 TMPro;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.UI;
@ -11,14 +12,17 @@ public class HeartMeter : SingletonMonoBehaviour<HeartMeter>
[SerializeField] private Slider slider;
[SerializeField] private TextMeshProUGUI heartLevel;
[SerializeField] private GameObject maxObject;
[SerializeField] private float duration = .5f;
private Coroutine coroutine;
private List<ShopLevelData> shopLevelList = new List<ShopLevelData>();
private int currentHeartCount;
private int maxLevel;
private readonly CompositeDisposable compositeDisposable = new CompositeDisposable();
private readonly ReactiveProperty<float> viewHeartCount = new ReactiveProperty<float>();
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 maxHeart;
@ -32,7 +36,11 @@ public class HeartMeter : SingletonMonoBehaviour<HeartMeter>
{
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<HeartMeter>
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<HeartMeter>
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<HeartMeter>
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);
}
}