収穫ロジック調整

This commit is contained in:
kimura 2021-07-21 13:00:52 +09:00
parent 9bcf9a9597
commit 2862cb0670
5 changed files with 71 additions and 17 deletions

View File

@ -421,6 +421,7 @@ GameObject:
- component: {fileID: 536970160} - component: {fileID: 536970160}
- component: {fileID: 536970159} - component: {fileID: 536970159}
- component: {fileID: 536970158} - component: {fileID: 536970158}
- component: {fileID: 536970161}
m_Layer: 0 m_Layer: 0
m_Name: Main Camera m_Name: Main Camera
m_TagString: MainCamera m_TagString: MainCamera
@ -493,6 +494,22 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 1 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &536970161
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 536970157}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 56666c5a40171f54783dd416a44f42bf, type: 3}
m_Name:
m_EditorClassIdentifier:
m_EventMask:
serializedVersion: 2
m_Bits: 23
m_MaxRayIntersections: 0
--- !u!1 &619114158 --- !u!1 &619114158
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -415,7 +415,7 @@ BoxCollider2D:
m_Enabled: 1 m_Enabled: 1
m_Density: 1 m_Density: 1
m_Material: {fileID: 0} m_Material: {fileID: 0}
m_IsTrigger: 0 m_IsTrigger: 1
m_UsedByEffector: 0 m_UsedByEffector: 0
m_UsedByComposite: 0 m_UsedByComposite: 0
m_Offset: {x: 0, y: -0.74168634} m_Offset: {x: 0, y: -0.74168634}
@ -496,6 +496,5 @@ MonoBehaviour:
seedlingStage2: {fileID: 875275074190396077} seedlingStage2: {fileID: 875275074190396077}
seedlingStage3: {fileID: 875275073577674106} seedlingStage3: {fileID: 875275073577674106}
seedlingStage4: {fileID: 875275074368017847} seedlingStage4: {fileID: 875275074368017847}
stage4Collider: {fileID: 3560956324713403295}
growingArrowPrefab: {fileID: 4493492865567033234, guid: b3d641214f420f74290147d368b221fa, growingArrowPrefab: {fileID: 4493492865567033234, guid: b3d641214f420f74290147d368b221fa,
type: 3} type: 3}

View File

@ -15,13 +15,18 @@ public class CornField : MonoBehaviour
private readonly List<CornSeedling> plants = new List<CornSeedling>(); private readonly List<CornSeedling> plants = new List<CornSeedling>();
private static readonly float maxPeriod = 60f; private static readonly float maxPeriod = 60f;
private static readonly float minPeriod = 45f; private static readonly float minPeriod = 45f;
private readonly CompositeDisposable compositeDisposable = new CompositeDisposable();
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
compositeDisposable.AddTo(this);
this.UpdateAsObservable() this.UpdateAsObservable()
.Where(_ => Input.GetKeyDown(KeyCode.R)) .Where(_ => Input.GetKeyDown(KeyCode.R))
.Take(1) .Take(1)
.Subscribe(_ => TransitionManager.Instance.LoadScene(GameScenes.CornField)).AddTo(this); .Subscribe(_ => TransitionManager.Instance.LoadScene(GameScenes.CornField)).AddTo(this);
var settings = new DateTime[3] var settings = new DateTime[3]
{ {
DateTime.Now, DateTime.Now,
@ -42,7 +47,17 @@ public class CornField : MonoBehaviour
for (int i = 0; i < plants.Count; i++) for (int i = 0; i < plants.Count; i++)
{ {
var period = 30 + Random.Range(0, 15); var period = 30 + Random.Range(0, 15);
plants[i].SetSeedlingGene(settings[i], period, SeedlingRank.Rank1); var ii = i;
plants[ii].SetSeedlingGene(settings[ii], period, SeedlingRank.Rank1);
plants[ii].Harvested.Subscribe(_ =>
{
// 収穫
Debug.Log($"harvested");
// 新しい苗
period = 30 + Random.Range(0, 15);
settings[ii] = DateTime.Now;
plants[ii].SetSeedlingGene(settings[ii], period, SeedlingRank.Rank1);
}).AddTo(compositeDisposable);
} }
promoteGrowthButton.OnClickAsObservable().Subscribe(_ => promoteGrowthButton.OnClickAsObservable().Subscribe(_ =>
@ -54,7 +69,7 @@ public class CornField : MonoBehaviour
plants[i].PromoteGrowth(settings[i]); plants[i].PromoteGrowth(settings[i]);
}; };
} }
}); }).AddTo(compositeDisposable);
} }
// Update is called once per frame // Update is called once per frame

View File

@ -1,6 +1,7 @@
using System; using System;
using UniRx; using UniRx;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems;
public enum SeedlingStage public enum SeedlingStage
{ {
@ -18,7 +19,7 @@ public enum SeedlingRank
Rank3 Rank3
} }
public class CornSeedling : MonoBehaviour public class CornSeedling : MonoBehaviour, IPointerEnterHandler
{ {
[SerializeField] private CornSeedlingView seedlingView; [SerializeField] private CornSeedlingView seedlingView;
private static readonly float stage0End = 0.25f; private static readonly float stage0End = 0.25f;
@ -31,11 +32,14 @@ public class CornSeedling : MonoBehaviour
private SeedlingRank seedlingRank; // 苗の良さ(1~3 private SeedlingRank seedlingRank; // 苗の良さ(1~3
private bool completed; private bool completed;
public IObservable<Unit> Harvested => harvested;
private readonly Subject<Unit> harvested = new Subject<Unit>();
private readonly ReactiveProperty<SeedlingStage> seedlingStage = new ReactiveProperty<SeedlingStage>(); private readonly ReactiveProperty<SeedlingStage> seedlingStage = new ReactiveProperty<SeedlingStage>();
private readonly CompositeDisposable compositeDisposable = new CompositeDisposable(); private readonly CompositeDisposable compositeDisposable = new CompositeDisposable();
private void Start() private void Start()
{ {
harvested.AddTo(this);
seedlingStage.AddTo(this); seedlingStage.AddTo(this);
compositeDisposable.AddTo(this); compositeDisposable.AddTo(this);
} }
@ -67,10 +71,18 @@ public class CornSeedling : MonoBehaviour
seedlingRank = rank; seedlingRank = rank;
// resume対応 // resume対応
// 途中からの場合アニメーションを勧めておく処理 // 苗の状態復元
seedlingView.SetView(GetCurrentStage()); seedlingView.SetView(GetCurrentStage());
// 成長ロジック // 成長進捗度更新
Observable.Interval(TimeSpan.FromSeconds(1f))
.TakeWhile(_ => !completed)
.Subscribe(x =>
{
UpdateStage();
}).AddTo(compositeDisposable);
// 更新
seedlingStage seedlingStage
.TakeWhile(_ => !completed) .TakeWhile(_ => !completed)
.Subscribe(x => .Subscribe(x =>
@ -81,14 +93,6 @@ public class CornSeedling : MonoBehaviour
completed = true; completed = true;
} }
}).AddTo(compositeDisposable); }).AddTo(compositeDisposable);
// 成長進捗度
Observable.Interval(TimeSpan.FromSeconds(1f))
.TakeWhile(_ => !completed)
.Subscribe(x =>
{
UpdateStage();
}).AddTo(compositeDisposable);
} }
private void UpdateStage() private void UpdateStage()
@ -118,4 +122,9 @@ public class CornSeedling : MonoBehaviour
} }
return SeedlingStage.Stage0; return SeedlingStage.Stage0;
} }
public void OnPointerEnter(PointerEventData eventData)
{
harvested.OnNext(Unit.Default);
}
} }

View File

@ -1,5 +1,8 @@
using System; using System;
using System.Runtime.CompilerServices;
using UniRx;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems;
public class CornSeedlingView : MonoBehaviour public class CornSeedlingView : MonoBehaviour
{ {
@ -7,7 +10,6 @@ public class CornSeedlingView : MonoBehaviour
[SerializeField] private GameObject seedlingStage2; [SerializeField] private GameObject seedlingStage2;
[SerializeField] private GameObject seedlingStage3; [SerializeField] private GameObject seedlingStage3;
[SerializeField] private GameObject seedlingStage4; [SerializeField] private GameObject seedlingStage4;
[SerializeField] private Collider2D stage4Collider;
[SerializeField] private GameObject growingArrowPrefab; [SerializeField] private GameObject growingArrowPrefab;
private readonly int nextStageTriggerHash = Animator.StringToHash("NextStageTrigger"); private readonly int nextStageTriggerHash = Animator.StringToHash("NextStageTrigger");
@ -59,6 +61,10 @@ public class CornSeedlingView : MonoBehaviour
{ {
seedlingStage2.SetActive(true); seedlingStage2.SetActive(true);
}); });
this.CallWaitForSeconds(.33f, () =>
{
seedlingStage1.SetActive(false);
});
break; break;
case SeedlingStage.Stage3: case SeedlingStage.Stage3:
seedlingStage2.GetComponent<Animator>().SetTrigger(nextStageTriggerHash); seedlingStage2.GetComponent<Animator>().SetTrigger(nextStageTriggerHash);
@ -66,6 +72,10 @@ public class CornSeedlingView : MonoBehaviour
{ {
seedlingStage3.SetActive(true); seedlingStage3.SetActive(true);
}); });
this.CallWaitForSeconds(.33f, () =>
{
seedlingStage2.SetActive(false);
});
break; break;
case SeedlingStage.Stage4: case SeedlingStage.Stage4:
seedlingStage3.GetComponent<Animator>().SetTrigger(nextStageTriggerHash); seedlingStage3.GetComponent<Animator>().SetTrigger(nextStageTriggerHash);
@ -73,6 +83,10 @@ public class CornSeedlingView : MonoBehaviour
{ {
seedlingStage4.SetActive(true); seedlingStage4.SetActive(true);
}); });
this.CallWaitForSeconds(.33f, () =>
{
seedlingStage3.SetActive(false);
});
break; break;
default: default:
throw new ArgumentOutOfRangeException(nameof(stage), stage, null); throw new ArgumentOutOfRangeException(nameof(stage), stage, null);