diff --git a/popcorn/Assets/MyGame/Scenes/Main/TimingGame.cs b/popcorn/Assets/MyGame/Scenes/Main/TimingGame.cs new file mode 100644 index 00000000..94eb4599 --- /dev/null +++ b/popcorn/Assets/MyGame/Scenes/Main/TimingGame.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.SceneManagement; +using UnityEngine.UI; + +public class TimingGame : MonoBehaviour +{ + [SerializeField] private PowerMeter powerMeter; + [SerializeField] private Text powerResultText; + [SerializeField] private PanController panController; + + private Action tapAction; + private bool initialized; + private bool isReady; + private bool isProgress; + private int gameCount; + private int failCount; + private bool isPerfect; + private readonly int maxGameCount = 3; + private readonly int maxFailCount = 2; + + // Update is called once per frame + void Update() + { + if (isProgress) return; + if(Input.GetMouseButtonDown(0)) + { + tapAction.Invoke(); + } + } + + public void Initialize() + { + if (initialized) return; + isReady = false; + GameReset(); + initialized = true; + } + + private void GameReset() + { + if (isReady) return; + powerResultText.gameObject.SetActive(false); + powerMeter.gameObject.SetActive(true); + gameCount = 0; + failCount = 0; + panController.enabled = false; + powerMeter.StopMove(); + isProgress = false; + isPerfect = true; + + isReady = true; + } + + public void GameStart() + { + GameReset(); + isReady = false; + powerMeter.StartMove(); + panController.enabled = true; + + tapAction = () => + { + powerMeter.StopMove(); + isProgress = true; + gameCount++; + var pos = powerMeter.GetCurrentPosisiton(); + Debug.Log($"tap! {pos}"); + // 判定 + CheckQuality(pos); + + this.CallWaitForSeconds(1f, () => + { + powerResultText.gameObject.SetActive(false); + // 失敗 + if (failCount >= maxFailCount) + { + GameOver(); + return; + } + + // 成功 + if (gameCount >= maxGameCount) + { + // 大成功 + if (isPerfect) + { + GameOver(); + return; + } + GameOver(); + return; + } + powerMeter.StartMove(); + isProgress = false; + }); + }; + } + + private void GameOver() + { + tapAction = () => { }; + powerMeter.gameObject.SetActive(false); + panController.enabled = false; + } + + private void CheckQuality(float pos) + { + powerResultText.gameObject.SetActive(true); + //Perfect=0.45~0.5 + //Good=0.3 + if (pos <= 0.05f) + { + powerResultText.text = "パーフェクト"; + return; + } + if (pos <= 0.2) + { + powerResultText.text = "グッド"; + isPerfect = false; + return; + } + powerResultText.text = "ミス!"; + isPerfect = false; + failCount++; + } + + private bool CheckTap(Vector2 position){ + var eventSystem = EventSystem.current; + var eventDataCurrentPosition = new PointerEventData(eventSystem); + eventDataCurrentPosition.position = position; + List raycastResults = new List(); + eventSystem.RaycastAll(eventDataCurrentPosition, raycastResults); + return raycastResults.Count == 0; + } +} diff --git a/popcorn/Assets/MyGame/Scenes/Main/TimingGame.cs.meta b/popcorn/Assets/MyGame/Scenes/Main/TimingGame.cs.meta new file mode 100644 index 00000000..3427700a --- /dev/null +++ b/popcorn/Assets/MyGame/Scenes/Main/TimingGame.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2527953f37754598a05d509fe4b7bfde +timeCreated: 1624611253 \ No newline at end of file