タイミングゲームクラス整理
This commit is contained in:
parent
966b689a08
commit
43b66bb0ca
|
|
@ -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<RaycastResult> raycastResults = new List<RaycastResult>();
|
||||
eventSystem.RaycastAll(eventDataCurrentPosition, raycastResults);
|
||||
return raycastResults.Count == 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2527953f37754598a05d509fe4b7bfde
|
||||
timeCreated: 1624611253
|
||||
Loading…
Reference in New Issue