diff --git a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornField.cs b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornField.cs index 92b019ff..83428bc9 100644 --- a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornField.cs +++ b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/CornField.cs @@ -1,12 +1,20 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using UniRx; using UniRx.Triggers; using UnityEngine; using UnityEngine.UI; using Random = UnityEngine.Random; +public enum CornFieldRank +{ + Rank1, + Rank2, + Rank3 +} + public class CornField : MonoBehaviour { [SerializeField] private Button promoteGrowthButton; @@ -15,11 +23,11 @@ public class CornField : MonoBehaviour [SerializeField] private CornHarvester cornHarvester; [Space] [SerializeField] private List plantLines = new List(); - [SerializeField] private List centerPlants = new List(); - private readonly List plants = new List(); - private static readonly float maxPeriod = 60f; - private static readonly float minPeriod = 45f; + private readonly List availableLines = new List(); + private static readonly int maxPeriod = 60; + private static readonly int minPeriod = 45; + private readonly ReactiveProperty harvestedCornCount = new ReactiveProperty(); private readonly CompositeDisposable compositeDisposable = new CompositeDisposable(); // Start is called before the first frame update void Start() @@ -30,67 +38,171 @@ public class CornField : MonoBehaviour .Take(1) .Subscribe(_ => TransitionManager.Instance.LoadScene(GameScenes.CornField)).AddTo(this); - - var settings = new DateTime[3] + // データ + // 解放済みの畑, 畑の質, 収穫機レベル + var plantSetting = new List() { - DateTime.Now, - DateTime.Now, - DateTime.Now, + PlantLineType.Center, + PlantLineType.Bottom, }; - // settings = new DateTime[3] - // { - // DateTime.Now.AddSeconds(-Random.Range(0, 15)), - // DateTime.Now.AddSeconds(-Random.Range(0, 15)), - // DateTime.Now.AddSeconds(-Random.Range(0, 15)), - // }; - - - plants.Clear(); - plants.AddRange(centerPlants); - - for (int i = 0; i < plants.Count; i++) + var fieldLevel = (int)CornFieldRank.Rank3; + var machineLevel = 1; + // コーン株の進行度 + var progressDataList = new List() { - var period = 15 + Random.Range(0, 15); - var ii = i; - plants[ii].SetSeedlingGene(settings[ii], period, SeedlingRank.Rank1); - plants[ii].Harvested.Subscribe(_ => + GenerateSeedlingData(PlantLineType.Center), + }; + + // 畑リセット + availableLines.Clear(); + foreach (var line in plantLines) + { + line.gameObject.SetActive(false); + } + + // セーブデータから畑を復元 + foreach (var line in plantSetting) + { + Debug.Log($"{line}"); + var plantLine = plantLines.First(x => x.LineName == line); + plantLine.gameObject.SetActive(true); + availableLines.Add(plantLine); + // 初回データ作成 + if (!progressDataList.Exists(x => x.type == line)) { - // 収穫 - VibrationManager.Instance.PlayVibrationOnce(); - var harvest = Instantiate(harvestPrefab, plants[ii].transform); - this.CallWaitForSeconds(.5f, () => - { - var beginPos = harvest.transform.position; - this.CallLerp(.4f, f => - { - harvest.transform.position = Vector3.Lerp(beginPos, harvestInsertPosition.position, f.EaseInCubic()); - }, () => - { - var seedCount = 25; - cornHarvester.AddCount(seedCount); - Destroy(harvest); - }); - }); - // 新しい苗 - period = 15 + Random.Range(0, 15); - settings[ii] = DateTime.Now; - plants[ii].SetSeedlingGene(settings[ii], period, SeedlingRank.Rank1); - }).AddTo(compositeDisposable); + progressDataList.Add(GenerateSeedlingData(line)); + } } + // 株設定 + foreach (var line in availableLines) + { + var lineData = progressDataList.First(x => x.type == line.LineName); + var i = 0; + foreach (var seedling in line.Seedlings) + { + var index = i; + i++; + seedling.SetSeedlingGene(lineData.Seedlings[index].firstTime, lineData.Seedlings[index].period, (CornFieldRank)fieldLevel); + seedling.Harvested.Subscribe(_ => + { + // 収穫 + VibrationManager.Instance.PlayVibrationOnce(); + for (int j = 0; j < GetHarvestCount((CornFieldRank)fieldLevel); j++) + { + var harvestedCorn = 20; + harvestedCornCount.Value += harvestedCorn; + var harvestAnimation = Instantiate(harvestPrefab, seedling.transform); + this.CallWaitForSeconds(.5f, () => + { + var beginPos = harvestAnimation.transform.position; + this.CallLerp(.4f, f => + { + harvestAnimation.transform.position = Vector3.Lerp(beginPos, harvestInsertPosition.position, f.EaseInQuadratic()); + }, () => + { + cornHarvester.AddCount(harvestedCorn); + Destroy(harvestAnimation); + }); + }); + } + // 新しい苗 + lineData.Seedlings[index] = GenerateSeedlingGene(); + seedling.SetSeedlingGene(lineData.Seedlings[index].firstTime, lineData.Seedlings[index].period, CornFieldRank.Rank1); + }).AddTo(compositeDisposable); + } + } + + // 時短ボタン promoteGrowthButton.OnClickAsObservable().Subscribe(_ => { VibrationManager.Instance.PlayVibrationOnce(); - for (int i = 0; i < plants.Count; i++) + foreach (var line in availableLines) { - if (Random.Range(0, 2) == 0){ - settings[i] = settings[i].AddSeconds(-1); - plants[i].PromoteGrowth(settings[i]); - }; + var lineData = progressDataList.First(x => x.type == line.LineName); + var i = 0; + foreach (var seedling in line.Seedlings) + { + if (Random.Range(0, 2) == 0) + { + var tmpData = lineData.Seedlings[i]; + tmpData.firstTime = lineData.Seedlings[i].firstTime.AddSeconds(-1); + lineData.Seedlings[i] = tmpData; + seedling.PromoteGrowth(lineData.Seedlings[i].firstTime); + } + } } + // 設定のセーブ }).AddTo(compositeDisposable); } + private SeedlingProgressData GenerateSeedlingData(PlantLineType type) + { + switch (type) + { + case PlantLineType.Top: + return new SeedlingProgressData + { + type = PlantLineType.Top, + Seedlings = new List<(DateTime firstTime, int period)>() + { + GenerateSeedlingGene(), + GenerateSeedlingGene(), + GenerateSeedlingGene(), + GenerateSeedlingGene(), + } + }; + case PlantLineType.Center: + return new SeedlingProgressData + { + type = PlantLineType.Center, + Seedlings = new List<(DateTime firstTime, int period)>() + { + GenerateSeedlingGene(), + GenerateSeedlingGene(), + GenerateSeedlingGene(), + } + }; + break; + case PlantLineType.Bottom: + return new SeedlingProgressData + { + type = PlantLineType.Bottom, + Seedlings = new List<(DateTime firstTime, int period)>() + { + GenerateSeedlingGene(), + GenerateSeedlingGene(), + GenerateSeedlingGene(), + GenerateSeedlingGene(), + } + }; + break; + default: + throw new ArgumentOutOfRangeException(nameof(type), type, null); + } + } + + private (DateTime, int) GenerateSeedlingGene() + { + // return (DateTime.Now, Random.Range(minPeriod, maxPeriod + 1)); + return (DateTime.Now.AddSeconds(-Random.Range(0, 15)), 15 + Random.Range(0, 15)); + } + + private int GetHarvestCount(CornFieldRank rank) + { + switch (rank) + { + case CornFieldRank.Rank1: + return 1; + case CornFieldRank.Rank2: + return 2; + case CornFieldRank.Rank3: + return 3; + default: + throw new ArgumentOutOfRangeException(nameof(rank), rank, null); + } + } + // Update is called once per frame void Update() { diff --git a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/SeedlingProgressData.cs b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/SeedlingProgressData.cs new file mode 100644 index 00000000..8e28ef4e --- /dev/null +++ b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/SeedlingProgressData.cs @@ -0,0 +1,9 @@ +using System; +using System.Collections.Generic; + +[Serializable] +public class SeedlingProgressData +{ + public PlantLineType type; + public List<(DateTime firstTime, int period)> Seedlings; +} \ No newline at end of file diff --git a/popcorn/Assets/MyGame/Scenes/CornField/Scripts/SeedlingProgressData.cs.meta b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/SeedlingProgressData.cs.meta new file mode 100644 index 00000000..a34754fe --- /dev/null +++ b/popcorn/Assets/MyGame/Scenes/CornField/Scripts/SeedlingProgressData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 61c5405c000a4baea2bc5b6ad44ce2b2 +timeCreated: 1627558442 \ No newline at end of file