From 43b66bb0ca5631ed1e90795203cd06b0e0a570d6 Mon Sep 17 00:00:00 2001 From: kimura Date: Fri, 25 Jun 2021 18:36:20 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=9F=E3=83=B3=E3=82=B0?= =?UTF-8?q?=E3=82=B2=E3=83=BC=E3=83=A0=E3=82=AF=E3=83=A9=E3=82=B9=E6=95=B4?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/MyGame/Scenes/Main/TimingGame.cs | 138 ++++++++++++++++++ .../MyGame/Scenes/Main/TimingGame.cs.meta | 3 + 2 files changed, 141 insertions(+) create mode 100644 popcorn/Assets/MyGame/Scenes/Main/TimingGame.cs create mode 100644 popcorn/Assets/MyGame/Scenes/Main/TimingGame.cs.meta 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