Merge branch 'develop' of bitbucket.org:usaya/popcorn into develop

This commit is contained in:
koya_15 2021-07-06 17:05:28 +09:00
commit d688994475
4 changed files with 163 additions and 81 deletions

View File

@ -7981,8 +7981,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

View File

@ -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<CornCondition> Condition => condition;
private readonly ReactiveProperty<CornCondition> condition = new ReactiveProperty<CornCondition>(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<Rigidbody2D>();
spriteTransform = spriteRenderer.transform;
rotationResolverTransform = spriteRenderer.transform.parent.parent;
condition.AddTo(this);
rigidbody = GetComponent<Rigidbody2D>();
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<Collider2D>().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<Collider2D>().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;
}
}

View File

@ -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<float> 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);

View File

@ -1,21 +1,18 @@
using System;
using UnityEngine;
namespace MyGame.Scenes.Main
public class Pan : MonoBehaviour
{
public class Pan : MonoBehaviour
{
[SerializeField] private PanController controller;
[SerializeField] private float thrust;
[SerializeField] private PanController controller;
[SerializeField] private float thrust;
private void OnTriggerStay2D(Collider2D other)
{
// Debug.Log(controller.Direction);
// 移動量が少ないときは動かない
// if (controller.Delta.magnitude >= 1f) Debug.Log(controller.Delta.magnitude);
var direct = 1f;
direct = controller.Delta.magnitude < 0.1f ? 0 : 1;
other.attachedRigidbody.AddForce(controller.Delta * thrust * direct, ForceMode2D.Impulse);
}
private void OnTriggerStay2D(Collider2D other)
{
// Debug.Log(controller.Direction);
// 移動量が少ないときは動かない
// if (controller.Delta.magnitude >= 1f) Debug.Log(controller.Delta.magnitude);
var direct = 1f;
direct = controller.Delta.magnitude < 0.1f ? 0 : 1;
other.attachedRigidbody.AddForce(controller.Delta * thrust * direct, ForceMode2D.Impulse);
}
}