58 lines
2.1 KiB
C#
58 lines
2.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
using UniRx;
|
|
using UnityEngine;
|
|
|
|
public class GameDataObserver : SingletonMonoBehaviour<GameDataObserver>
|
|
{
|
|
[SerializeField] private FloatReactiveProperty checkInterval = new FloatReactiveProperty(.1f);
|
|
private IObservable<int> heartObserver;
|
|
public IObservable<int> Heart => heartObserver;
|
|
|
|
private IObservable<int> sceneCounter;
|
|
public IObservable<int> SceneCounter => sceneCounter;
|
|
|
|
public IObservable<bool> ShopLevelUpdate;
|
|
|
|
private void Start()
|
|
{
|
|
checkInterval.AddTo(this);
|
|
var gameData = GameDataManager.GameData;
|
|
var levelList = SpreadsheetDataManager.Instance.GetBaseDataList<ShopLevelData>(Const.ShopLevelDataSheet);
|
|
heartObserver = checkInterval
|
|
.Select(x =>
|
|
Observable.Interval(TimeSpan.FromSeconds(x))
|
|
.Select(_ => gameData.Heart)
|
|
.DistinctUntilChanged())
|
|
.Switch();
|
|
|
|
var shopLevelObserver = checkInterval
|
|
.Select(x =>
|
|
Observable.Interval(TimeSpan.FromSeconds(x))
|
|
.Select(_ => gameData.ViewedShopLevel)
|
|
.DistinctUntilChanged())
|
|
.Switch();
|
|
|
|
sceneCounter = checkInterval
|
|
.Select(x =>
|
|
Observable.Interval(TimeSpan.FromSeconds(x))
|
|
.Select(_ => UnityEngine.SceneManagement.SceneManager.sceneCount)
|
|
.DistinctUntilChanged()
|
|
.Delay(TimeSpan.FromSeconds(.4f))
|
|
// .Do(c => { Debug.Log($"sceneCount:{c}");})
|
|
)
|
|
.Switch();
|
|
|
|
ShopLevelUpdate = Observable.CombineLatest(heartObserver, shopLevelObserver, sceneCounter,
|
|
(heart, level, sceneCount) =>
|
|
{
|
|
var currentLevel = levelList.LastOrDefault(data => data.heart <= heart)?.shopLevel ?? 1;
|
|
return currentLevel > level && sceneCount == 1;
|
|
})
|
|
.DistinctUntilChanged()
|
|
.Publish(false)
|
|
.RefCount();
|
|
ShopLevelUpdate.Subscribe(x => { Debug.Log($"change:{x}"); });
|
|
}
|
|
}
|