diff --git a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornField.cs b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornField.cs index d4553518..58592e9d 100644 --- a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornField.cs +++ b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornField.cs @@ -29,7 +29,6 @@ public class CornField : MonoBehaviour [SerializeField] private Transform animationTarget; [Space] [SerializeField] private List plantLines = new List(); - private readonly List availableLines = new List(); private static readonly int maxPeriod = 60; private static readonly int minPeriod = 45; private static readonly int harvestedFrameInterval = 3; @@ -59,34 +58,20 @@ public class CornField : MonoBehaviour counterView.Initialize(gameData.cornSeed); SetData(); - var fieldData = SpreadsheetDataManager.Instance.GetBaseDataList(Const.FieldUpgradeDataSheet); - // 畑リセット foreach (var line in plantLines) { line.gameObject.SetActive(false); } - - // セーブデータから畑を復元 - availableLines.Clear(); - foreach (var lineData in gameData.PlantLineDataList) - { - var plantLine = plantLines.First(x => x.LineName == lineData.Type); - plantLine.gameObject.SetActive(true); - plantLine.SetFieldLevel(lineData.Level); - availableLines.Add(plantLine); - // コーン株の進行度初回データ作成 - if (!gameData.SeedlingDataList.Exists(x => x.type == lineData.Type)) - { - gameData.SeedlingDataList.Add(new SeedlingProgressData - { - type = lineData.Type, - Seedlings = Enumerable.Repeat(GenerateSeedlingGene(lineData.Level), plantLine.Seedlings.Count).ToList() - }); - } - } - GameDataManager.SaveGameData(); + SetCornField(); + + upgradeButton.OnClickAsObservable().Subscribe(_ => + { + LocalCacheManager.Save(CornFieldReinforcement.CornFieldResetCallbackTag, new Action(SetCornField)); + TransitionManager.Instance.LoadSceneAdditive(GameScenes.Reinforcement); + }).AddTo(this); + // 収穫カウンター+吸収演出 cornHarvester.FinishHarvested .Scan((list, newList) => (newList.count, list.colliders.Concat(newList.colliders).ToList())) @@ -130,71 +115,101 @@ public class CornField : MonoBehaviour counterView.SetHarvestedCount(x.count); }); }).AddTo(this); - // 株設定 - foreach (var line in availableLines) + } + + public void SetCornField() + { + compositeDisposable.Clear(); + var gameData = GameDataManager.GameData; + var fieldData = SpreadsheetDataManager.Instance.GetBaseDataList(Const.FieldUpgradeDataSheet); + + // コーン株の進行度初回データ作成 + foreach (var lineData in gameData.PlantLineDataList) { - var seedlingDataIndex = gameData.SeedlingDataList.FindIndex(x => x.type == line.LineName); - var lineData = gameData.SeedlingDataList[seedlingDataIndex]; - var i = 0; - foreach (var seedling in line.Seedlings) + // コーン株の進行度初回データ作成 + if (!gameData.SeedlingDataList.Exists(data => data.type == lineData.Type)) { - var index = i; - i++; - seedling.SetSeedlingGene(lineData.Seedlings[index].FirstTime, lineData.Seedlings[index].Period, lineData.Seedlings[index].Level); -#if DEVELOPMENT_BUILD || UNITY_EDITOR - if (UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugFastGrowing", false)) + var plantLine = plantLines.First(line => line.LineName == lineData.Type); + gameData.SeedlingDataList.Add(new SeedlingProgressData { - seedling.SetSeedlingGene(lineData.Seedlings[index].FirstTime, lineData.Seedlings[index].Period / 3, lineData.Seedlings[index].Level); - } -#endif - // 収穫通知 - seedling.Harvested.Subscribe(_ => + type = lineData.Type, + Seedlings = Enumerable.Range(0, plantLine.Seedlings.Count).Select(_ => GenerateSeedlingGene(lineData.Level)).ToList() + }); + } + } + GameDataManager.SaveGameData(); + + + // セーブデータから畑を復元 + foreach (var plantLine in plantLines) + { + if (gameData.PlantLineDataList.FirstOrDefault(data => data.Type == plantLine.LineName) is PlantLineData plantLineData) + { + plantLine.gameObject.SetActive(true); + plantLine.SetFieldLevel(plantLineData.Level); + + for (int i = 0; i < plantLine.Seedlings.Count; i++) { - VibrationManager.Instance.PlayVibrationOnce(); - var harvestCount = fieldData.FirstOrDefault(x => x.Type == CornFieldUpgradeType.FieldCenter && x.level == (int)lineData.Seedlings[index].Level)?.harvested ?? 1; - var harvestedCorn = fieldData.FirstOrDefault(x => x.Type == CornFieldUpgradeType.Machine && x.level == gameData.MachineLevel)?.harvested ?? 20; - gameData.cornSeed += harvestedCorn * harvestCount; - var seedlingTransform = seedling.transform; - - var harvestEffect = Instantiate(harvestEffectPrefab, seedlingTransform); - Destroy(harvestEffect, 1f); - for (int j = 0; j < harvestCount; j++) - { - // 株の位置調整 - var pos = seedlingTransform.position; - if (harvestCount > 1) - { - pos += Vector3.right * (j - 1) * harvestedDistance + Vector3.forward * (j - 1); - } - this.CallWaitForFrame(harvestedFrameInterval * j, () => - { - var harvestAnimation = Instantiate(harvestPrefab, pos, Quaternion.identity, seedlingTransform); - this.CallWaitForSeconds(.5f, () => - { - this.CallLerp(.4f, f => - { - harvestAnimation.transform.position = Vector3.Lerp(pos, harvestInsertPosition.position, f.EaseInQuadratic()); - }, () => - { - // コーン排出 - cornHarvester.AddCount(harvestedCorn); - Destroy(harvestAnimation); - }); - }); - }); - } - // 新しい苗 - var newGene = GenerateSeedlingGene(line.FieldLevel); - seedling.SetSeedlingGene(newGene.FirstTime, newGene.Period, newGene.Level); + var progressData = gameData.SeedlingDataList.First(data => data.type == plantLineData.Type).Seedlings[i]; + var seedling = plantLine.Seedlings[i]; + seedling.SetSeedlingGene(progressData.FirstTime, progressData.Period, progressData.Level); #if DEVELOPMENT_BUILD || UNITY_EDITOR if (UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugFastGrowing", false)) { - seedling.SetSeedlingGene(newGene.FirstTime, newGene.Period / 3, newGene.Level); + seedling.SetSeedlingGene(progressData.FirstTime, progressData.Period / 3, progressData.Level); } #endif - gameData.SeedlingDataList[seedlingDataIndex].Seedlings[index] = newGene; - GameDataManager.SaveGameData(); - }).AddTo(compositeDisposable); + // 収穫通知 + seedling.Harvested.Subscribe(_ => + { + VibrationManager.Instance.PlayVibrationOnce(); + var harvestCount = fieldData.FirstOrDefault(x => x.Type == CornFieldUpgradeType.FieldCenter && x.level == (int)progressData.Level)?.harvested ?? 1; + var harvestedCorn = fieldData.FirstOrDefault(x => x.Type == CornFieldUpgradeType.Machine && x.level == gameData.MachineLevel)?.harvested ?? 20; + gameData.cornSeed += harvestedCorn * harvestCount; + var seedlingTransform = seedling.transform; + + var harvestEffect = Instantiate(harvestEffectPrefab, seedlingTransform); + Destroy(harvestEffect, 1f); + for (int j = 0; j < harvestCount; j++) + { + // 株の位置調整 + var pos = seedlingTransform.position; + if (harvestCount > 1) + { + pos += Vector3.right * (j - 1) * harvestedDistance + Vector3.forward * (j - 1); + } + this.CallWaitForFrame(harvestedFrameInterval * j, () => + { + var harvestAnimation = Instantiate(harvestPrefab, pos, Quaternion.identity, seedlingTransform); + this.CallWaitForSeconds(.5f, () => + { + this.CallLerp(.4f, f => + { + harvestAnimation.transform.position = Vector3.Lerp(pos, harvestInsertPosition.position, f.EaseInQuadratic()); + }, () => + { + // コーン排出 + cornHarvester.AddCount(harvestedCorn); + Destroy(harvestAnimation); + }); + }); + }); + } + // 新しい苗 + var newGene = GenerateSeedlingGene(plantLineData.Level); + seedling.SetSeedlingGene(newGene.FirstTime, newGene.Period, newGene.Level); + #if DEVELOPMENT_BUILD || UNITY_EDITOR + if (UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugFastGrowing", false)) + { + seedling.SetSeedlingGene(newGene.FirstTime, newGene.Period / 3, newGene.Level); + } + #endif + progressData.Level = newGene.Level; + progressData.Period = newGene.Period; + progressData.FirstTime = newGene.FirstTime; + GameDataManager.SaveGameData(); + }).AddTo(compositeDisposable); + } } } @@ -202,28 +217,27 @@ public class CornField : MonoBehaviour promoteGrowthButton.OnClickAsObservable().Subscribe(_ => { VibrationManager.Instance.PlayVibrationOnce(); - foreach (var line in availableLines) + foreach (var plantLine in plantLines) { - var seedlingDataIndex = gameData.SeedlingDataList.FindIndex(x => x.type == line.LineName); - for (int i = 0; i < line.Seedlings.Count; i++) + if (gameData.PlantLineDataList.FirstOrDefault(data => data.Type == plantLine.LineName) is PlantLineData plantLineData) { - if (Random.Range(0, 2) == 0) + for (int i = 0; i < plantLine.Seedlings.Count; i++) { - var tmpData = gameData.SeedlingDataList[seedlingDataIndex].Seedlings[i]; - tmpData.FirstTime = tmpData.FirstTime.AddSeconds(-1); - line.Seedlings[i].PromoteGrowth(tmpData.FirstTime); - gameData.SeedlingDataList[seedlingDataIndex].Seedlings[i] = tmpData; + if (Random.Range(0, 2) == 0) + { + continue; + } + var progressData = gameData.SeedlingDataList.First(data => data.type == plantLineData.Type).Seedlings[i]; + var seedling = plantLine.Seedlings[i]; + progressData.FirstTime = progressData.FirstTime.AddSeconds(-1); + seedling.PromoteGrowth(progressData.FirstTime); } } } + // 設定のセーブ GameDataManager.SaveGameData(); }).AddTo(compositeDisposable); - - upgradeButton.OnClickAsObservable().Subscribe(_ => - { - TransitionManager.Instance.LoadSceneAdditive(GameScenes.Reinforcement); - }).AddTo(this); } private void SetData() diff --git a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornFieldReinforcement.cs b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornFieldReinforcement.cs index 59ff3479..8084336e 100644 --- a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornFieldReinforcement.cs +++ b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornFieldReinforcement.cs @@ -9,6 +9,7 @@ public class CornFieldReinforcement : MonoBehaviour { public static readonly string CornFieldReinforcementDataTypeTag = "CornFieldReinforcementDataType"; public static readonly string CornFieldReinforcementDataTag = "CornFieldReinforcementData"; + public static readonly string CornFieldResetCallbackTag = "CornFieldResetCallback"; [SerializeField] private List reinforcementViews; [SerializeField] private MachineUpgradeView machineUpgradeView; [SerializeField] private Button closeButton; @@ -20,9 +21,10 @@ public class CornFieldReinforcement : MonoBehaviour compositeDisposable.AddTo(this); closeButton.OnClickAsObservable().Subscribe(_ => { - TransitionManager.Instance.UnloadScene(GameScenes.Reinforcement); LocalCacheManager.Remove(CornFieldReinforcementDataTypeTag); LocalCacheManager.Remove(CornFieldReinforcementDataTag); + LocalCacheManager.Remove(CornFieldResetCallbackTag); + TransitionManager.Instance.UnloadScene(GameScenes.Reinforcement); }).AddTo(this); SetView(); } @@ -57,6 +59,7 @@ public class CornFieldReinforcement : MonoBehaviour LocalCacheManager.Save(CornFieldReinforcementDataTag, (lineData, price, new Action(() => { PurchaseField(price, view.LineType, nextRank); SetView(); + LocalCacheManager.Load(CornFieldResetCallbackTag, null)?.Invoke(); }))); TransitionManager.Instance.LoadSceneAdditive(GameScenes.ReinforcementDetail); }).AddTo(compositeDisposable); @@ -74,6 +77,7 @@ public class CornFieldReinforcement : MonoBehaviour LocalCacheManager.Save(CornFieldReinforcementDataTag, (lineData = null, price, new Action(() => { PurchaseField(price, view.LineType, CornFieldRank.Rank1); SetView(); + LocalCacheManager.Load(CornFieldResetCallbackTag, null)?.Invoke(); }))); TransitionManager.Instance.LoadSceneAdditive(GameScenes.ReinforcementDetail); }).AddTo(compositeDisposable); @@ -99,6 +103,7 @@ public class CornFieldReinforcement : MonoBehaviour LocalCacheManager.Save(CornFieldReinforcementDataTag, (gameData.MachineLevel, price, new Action(() => { PurchaseMachine(price); SetView(); + LocalCacheManager.Load(CornFieldResetCallbackTag, null)?.Invoke(); }))); TransitionManager.Instance.LoadSceneAdditive(GameScenes.ReinforcementDetail); }).AddTo(compositeDisposable); diff --git a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornSeedling.cs b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornSeedling.cs index 3f59e63d..04ededdd 100644 --- a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornSeedling.cs +++ b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornSeedling.cs @@ -51,7 +51,7 @@ public class CornSeedling : MonoBehaviour, IPointerEnterHandler { completed = false; compositeDisposable.Clear(); - seedlingStage.SetValueAndForceNotify(SeedlingStage.Stage0); + UpdateStage(); beginTime = dateTime; period = periodTime; @@ -69,6 +69,7 @@ public class CornSeedling : MonoBehaviour, IPointerEnterHandler // 更新 seedlingStage + .SkipLatestValueOnSubscribe() .TakeWhile(_ => !completed) .Subscribe(x => { diff --git a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/PlantLine.cs b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/PlantLine.cs index bba79469..f4f634bd 100644 --- a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/PlantLine.cs +++ b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/PlantLine.cs @@ -16,12 +16,10 @@ public class PlantLine : MonoBehaviour [SerializeField] private List seedlings = new List(); public PlantLineType LineName => lineName; public List Seedlings => seedlings; - public CornFieldRank FieldLevel { get; private set; } private readonly string boardFormat = "x{0}"; public void SetFieldLevel(CornFieldRank rank) { - FieldLevel = rank; switch (rank) { case CornFieldRank.Rank1: