From 7caa6c1729a7e8a5149641e4d2f4a39c9c4b4215 Mon Sep 17 00:00:00 2001 From: kimura Date: Tue, 6 Jul 2021 16:56:27 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=B3=E6=8C=99=E5=8B=95?= =?UTF-8?q?=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MyGame/Scenes/Cooking/Cooking.unity | 6 +- .../MyGame/Scenes/Cooking/Scripts/Corn.cs | 156 ++++++++++++------ .../Scenes/Cooking/Scripts/CornManager.cs | 57 +++++-- 3 files changed, 152 insertions(+), 67 deletions(-) diff --git a/popcorn/Assets/MyGame/Scenes/Cooking/Cooking.unity b/popcorn/Assets/MyGame/Scenes/Cooking/Cooking.unity index 7f41aa0d..5d4a93f2 100644 --- a/popcorn/Assets/MyGame/Scenes/Cooking/Cooking.unity +++ b/popcorn/Assets/MyGame/Scenes/Cooking/Cooking.unity @@ -7879,8 +7879,10 @@ MonoBehaviour: cornPrefab: {fileID: 5499754490129207122, guid: 2b790fd908bd14de4a601c21374fb55b, type: 3} cornSpawnCount: 20 - cornGrowSpeed: 0 - cornHotGrowSpeed: 0 + baseGrowSpeed: 1 + hotGrowSpeed: 1.5 + baseCornPopTime: 10 + cornBurntDuration: 3 --- !u!1 &1928679189 GameObject: m_ObjectHideFlags: 0 diff --git a/popcorn/Assets/MyGame/Scenes/Cooking/Scripts/Corn.cs b/popcorn/Assets/MyGame/Scenes/Cooking/Scripts/Corn.cs index 64d1967d..d1cdc100 100644 --- a/popcorn/Assets/MyGame/Scenes/Cooking/Scripts/Corn.cs +++ b/popcorn/Assets/MyGame/Scenes/Cooking/Scripts/Corn.cs @@ -1,9 +1,19 @@ using System; using System.Collections; using System.Collections.Generic; +using UniRx; +using UniRx.Triggers; using UnityEngine; using Random = UnityEngine.Random; +public enum CornCondition +{ + Seed, + Simple, + Burnt, + Spilled +} + [RequireComponent(typeof(Rigidbody2D))] public class Corn : MonoBehaviour { @@ -11,83 +21,119 @@ public class Corn : MonoBehaviour [SerializeField] private Sprite cornSeedSprite; [SerializeField] private Sprite cornSimpleSprite; [SerializeField] private Sprite cornBurntSprite; - // コーン速度 - [SerializeField] private float cornVelocity; - // コーンがスポーンしてからの時間 - [SerializeField] private float cornLifeTime; - // コーン弾ける時間! + + private Rigidbody2D rigidbody; private float cornPopTime; - // コーンが停まってる時間(焦げる判定 - private float cornStopTime; - private float maxCornStopTime = 5f; - // コーンが動いている総時間 - private float cornMovingTime => cornLifeTime - cornStopTime; - // コーン誕生時間 - private float cornSpawnTime; - private bool isPop; + private float cornBurntTime; + private float cornGrowTime; + private float cornGrowSpeed; + private float maxSpeed = 50f; + private float popSpeed = 20f; + + public IReadOnlyReactiveProperty Condition => condition; + private readonly ReactiveProperty condition = new ReactiveProperty(CornCondition.Seed); - private Rigidbody2D rigidbody2D; + + // 角度調整 private Transform rotationResolverTransform; private Transform spriteTransform; - - // Start is called before the first frame update - void Start() + + private void Awake() { - rigidbody2D = GetComponent(); - spriteTransform = spriteRenderer.transform; - rotationResolverTransform = spriteRenderer.transform.parent.parent; + condition.AddTo(this); + rigidbody = GetComponent(); spriteRenderer.sprite = cornSeedSprite; - cornSpawnTime = Time.time; - var cornPopBasicTime = 5f; - cornPopTime = cornPopBasicTime + Random.Range(0, 5f); - rigidbody2D.position = new Vector2(Random.value, Random.value); + spriteTransform = spriteRenderer.transform; + rotationResolverTransform = spriteTransform.parent.parent; + } + + private void Start() + { + rigidbody.MovePosition(new Vector2(Random.value * 2 - 1, Random.value * 2 - 1)); + + // コーンが飛んでいく + this.ObserveEveryValueChanged(x => x.rigidbody.velocity.magnitude) + .Where(x => x >= maxSpeed) + .FirstOrDefault() + .Subscribe(_ => + { + condition.Value = CornCondition.Spilled; + }).AddTo(this); + + condition.Subscribe(x => + { + switch (x) + { + case CornCondition.Seed: + break; + case CornCondition.Simple: + CornPop(); + break; + case CornCondition.Burnt: + CornBurnt(); + break; + case CornCondition.Spilled: + CornSpilled(); + break; + default: + throw new ArgumentOutOfRangeException(nameof(x), x, null); + } + }).AddTo(this); + + this.UpdateAsObservable().Subscribe(_ => + { + // アニメーション向けに回転をリセット + rotationResolverTransform.rotation = Quaternion.identity; + // コーンの回転を伝搬 + spriteTransform.localRotation = transform.localRotation; + }).AddTo(this); } - private float maxVelocity; private void Update() { - cornVelocity = rigidbody2D.velocity.magnitude; - cornLifeTime = Time.time - cornSpawnTime; - - if (rigidbody2D.velocity.magnitude > maxVelocity) + if (condition.Value == CornCondition.Spilled) { - maxVelocity = Mathf.Max(rigidbody2D.velocity.magnitude, maxVelocity); - Debug.Log(maxVelocity); + return; } - if (maxVelocity >= 50f) + cornGrowTime += Time.deltaTime * cornGrowSpeed; + if (cornGrowTime >= cornBurntTime) { - GetComponent().enabled = false; + condition.Value = CornCondition.Burnt; } - - // アニメーション向けに回転をリセット - rotationResolverTransform.rotation = Quaternion.identity; - // コーンの回転を伝搬 - spriteTransform.localRotation = transform.localRotation; - - if (cornVelocity <= 0.2f) + else if (cornGrowTime >= cornPopTime) { - cornStopTime += Time.deltaTime; - } - - if (cornStopTime >= maxCornStopTime && isPop) - { - spriteRenderer.sprite = cornBurntSprite; - } - if (cornLifeTime >= cornPopTime) - { - CornPop(); + condition.Value = CornCondition.Simple; } } private void CornPop() { - if (isPop) return; - isPop = true; - var popVelocity = 20f; - var popDirection = new Vector2(Random.value * 2 - 1, Random.value * 2 - 1); - rigidbody2D.AddForce(popVelocity * popDirection, ForceMode2D.Impulse); spriteRenderer.sprite = cornSimpleSprite; + rigidbody.AddForce(popSpeed * new Vector2(Random.value * 2 - 1, Random.value * 2 - 1), ForceMode2D.Impulse); + } + + private void CornBurnt() + { + spriteRenderer.sprite = cornBurntSprite; + } + + private void CornSpilled() + { + GetComponent().enabled = false; + // アニメーション消す + } + + public void SetCornProperty(float popTime, float burntDuration) + { + cornPopTime = popTime + Random.Range(0, 2f); + cornBurntTime = cornPopTime + burntDuration + Random.Range(0, 1f); + } + + public void ChangeGrowSpeed(float speed) + { + Debug.Log($"{speed:F}"); + cornGrowSpeed = speed; } } diff --git a/popcorn/Assets/MyGame/Scenes/Cooking/Scripts/CornManager.cs b/popcorn/Assets/MyGame/Scenes/Cooking/Scripts/CornManager.cs index 37525440..5f4f45de 100644 --- a/popcorn/Assets/MyGame/Scenes/Cooking/Scripts/CornManager.cs +++ b/popcorn/Assets/MyGame/Scenes/Cooking/Scripts/CornManager.cs @@ -1,4 +1,5 @@ using System; +using UniRx; using UnityEngine; public class CornManager : MonoBehaviour @@ -6,25 +7,55 @@ public class CornManager : MonoBehaviour [SerializeField] private Transform cornSpawnTarget; [SerializeField] private Corn cornPrefab; [SerializeField] private int cornSpawnCount = 20; - [SerializeField] private float cornGrowSpeed; - [SerializeField] private float cornHotGrowSpeed; - + [SerializeField, ReadOnly] private float baseGrowSpeed = 1f; + [SerializeField] private float hotGrowSpeed; + [SerializeField] private float baseCornPopTime = 5f; + [SerializeField] private float cornBurntDuration = 5f; + private Corn[] cornArray; + private readonly ReactiveProperty cornGrowSpeed = new FloatReactiveProperty(0f); + private bool isHot; + + // クリア判定を通知 + + // コーンの状態を取得する - private void Start() - { - } - - public void Initialize() + private void Awake() { cornArray = new Corn[cornSpawnCount]; } - public void GenerateCorn() + public void RespawnCorn() { for (int i = 0; i < cornSpawnCount; i++) { - cornArray[i] = Instantiate(cornPrefab, cornSpawnTarget); + var corn = Instantiate(cornPrefab, cornSpawnTarget); + cornArray[i] = corn; + corn.SetCornProperty(baseCornPopTime, cornBurntDuration); + + cornGrowSpeed.Subscribe(x => + { + Debug.Log($"corn: {corn.Condition.Value} isHot: {isHot}"); + // コーンが弾けた後はRedの場合のみ焦げ進行する + if (corn.Condition.Value == CornCondition.Simple && !isHot) + { + corn.ChangeGrowSpeed(0f); + return; + } + corn.ChangeGrowSpeed(x); + }).AddTo(this); + + // コーンが弾けたとき、GrowSpeedを再設定する + corn.Condition + .Where(x => x == CornCondition.Simple) + .FirstOrDefault() + .Subscribe(_ => + { + if (!isHot) + { + corn.ChangeGrowSpeed(0f); + } + }).AddTo(this); } } @@ -33,10 +64,16 @@ public class CornManager : MonoBehaviour switch (condition) { case ThermalCondition.Cold: + isHot = false; + cornGrowSpeed.Value = 0f; break; case ThermalCondition.Yellow: + isHot = false; + cornGrowSpeed.Value = baseGrowSpeed; break; case ThermalCondition.Hot: + isHot = true; + cornGrowSpeed.Value = hotGrowSpeed; break; default: throw new ArgumentOutOfRangeException(nameof(condition), condition, null);