using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class VRMiniGameDarts : MonoBehaviour { private void Start() { base.GetComponent().maxAngularVelocity = 12.566371f; base.GetComponent().sleepThreshold = 0.2f; this.m_VRController = VRMiniGameManager.Instance.VRController; VRMiniGameSetup miniGameSetup = UnityEngine.Object.FindObjectOfType(); if (miniGameSetup.Is2DMode) { this.m_StartPivotLocalAngles = Vector3.zero; this.m_Pivot.localPosition = Vector3.zero; this.m_Pivot.localEulerAngles = this.m_StartPivotLocalAngles; VRMiniGameGaugeeManager gaugeManager = miniGameSetup.GaugeManager; gaugeManager.onMouseClickDownEvent = (VRMiniGameGaugeeManager.OnMouseClickEvent)Delegate.Combine(gaugeManager.onMouseClickDownEvent, new VRMiniGameGaugeeManager.OnMouseClickEvent(delegate(VRMiniGameGaugeeManager manager) { if (miniGameSetup.GaugeManager.isPlay) { return; } if (!this.m_IsGrab) { return; } miniGameSetup.GaugeManager.Play(); })); VRMiniGameGaugeeManager gaugeManager2 = miniGameSetup.GaugeManager; gaugeManager2.onMouseClickUpEvent = (VRMiniGameGaugeeManager.OnMouseClickEvent)Delegate.Combine(gaugeManager2.onMouseClickUpEvent, new VRMiniGameGaugeeManager.OnMouseClickEvent(delegate(VRMiniGameGaugeeManager manager) { if (!miniGameSetup.GaugeManager.isPlay) { return; } if (!this.m_IsGrab) { return; } OvrCamera useCamera = miniGameSetup.UseCamera; float num = 0.1f; float num2 = 10f; float gaugeValue = miniGameSetup.GaugeManager.gaugeValue; float d = num + (num2 - num) * gaugeValue * gaugeValue; Vector3 vector = this.m_VRController.transform.position - useCamera.m_goCenterEye.transform.position; vector = (Quaternion.AngleAxis(-10f, useCamera.m_goCenterEye.transform.right) * vector).normalized * d; Vector3[] deviationValue = this.GetDeviationValue(this.m_VelocityArray); vector += this.CalcThrowVec(deviationValue); this.GrabEnd(vector, Vector3.zero); miniGameSetup.GaugeManager.Stop(); })); } else if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.VIVE) { if (this.m_VRController.HandSide) { this.m_Pivot.localPosition = new Vector3(0.05f, -0.05f, -0.03f); this.m_Pivot.localEulerAngles = new Vector3(90f, -90f, 0f); this.m_StartPivotLocalAngles = this.m_Pivot.localEulerAngles; } else { this.m_Pivot.localPosition = new Vector3(-0.05f, -0.01f, -0.03f); this.m_Pivot.localEulerAngles = new Vector3(90f, -90f, 0f); this.m_StartPivotLocalAngles = this.m_Pivot.localEulerAngles; } } else { if (this.m_VRController.HandSide) { this.m_Pivot.localEulerAngles = Vector3.Scale(this.m_Pivot.localEulerAngles, new Vector3(1f, -1f, 1f)); } this.m_StartPivotLocalAngles = this.m_Pivot.localEulerAngles; } } private void Update() { if (VRMiniGameManager.Instance.MiniGameState != VRMiniGameManager.VRMiniGameState.Playing) { return; } if (this.m_IsRespawn) { return; } if (base.transform.position.y < -5f) { if (this.m_IsJudgeScore) { Debug.Log("[ ミニゲーム ]投擲したが、奈落に落ちた。"); this.m_IsJudgeScore = false; } this.Refresh(); } if (this.m_IsJudgeScore) { Rigidbody component = base.GetComponent(); Vector3 velocity = component.velocity; this.m_Pivot.LookAt(base.transform.position + velocity); } if (!this.m_VRController) { return; } if (this.m_IsGrab) { this.UpdateGrabTransform(); if (this.m_VRController.GetPressDown(AVRControllerButtons.BTN.TRIGGER)) { base.GetComponent().useGravity = false; GameObject[] array = GameObject.FindGameObjectsWithTag("MiniGameTarget"); this.m_TargetObject = array[UnityEngine.Random.Range(0, array.Length - 1)]; } if (this.m_VRController.GetPress(AVRControllerButtons.BTN.TRIGGER)) { } if (this.m_VRController.GetPressUp(AVRControllerButtons.BTN.TRIGGER)) { Vector3[] deviationValue = this.GetDeviationValue(this.m_VelocityArray); Vector3 vector = this.CalcThrowVec(deviationValue); vector *= this.m_Power; this.GrabEnd(vector, Vector3.zero); } } } private void Refresh() { if (!this.m_IsDropped) { VRMiniGameManager.VoiceType type = (VRMiniGameManager.VoiceType)Enum.Parse(typeof(VRMiniGameManager.VoiceType), "Remaining" + VRMiniGameManager.Instance.life.ToString()); VRMiniGameManager.Instance.AddQueueVoice(type); } if (VRMiniGameManager.Instance.life <= 0) { VRMiniGameSetup vrminiGameSetup = UnityEngine.Object.FindObjectOfType(); vrminiGameSetup.GaugeManager.isVisible = false; VRMiniGameManager.Instance.Result(delegate { Debug.Log("ダーツを終了。"); }); return; } base.transform.position = this.m_VRController.transform.position; Rigidbody component = base.GetComponent(); component.velocity = Vector3.zero; component.useGravity = false; component.isKinematic = false; this.m_IsGrab = true; this.m_IsDropped = false; this.m_IsRespawn = false; this.m_Pivot.localEulerAngles = this.m_StartPivotLocalAngles; this.m_GrabNum = 0; for (int i = 0; i < this.m_VelocityArray.Length; i++) { this.m_VelocityArray[i] = Vector3.zero; } } private IEnumerator Coroutine_Refresh(float tim) { this.m_IsRespawn = true; yield return new WaitForSeconds(tim); this.Refresh(); yield break; } private void OnCollisionEnter(Collision col) { if (this.m_IsRespawn) { return; } if (this.m_IsDropped) { base.StartCoroutine(this.Coroutine_Refresh(2f)); return; } if (!this.m_IsJudgeScore) { return; } base.StartCoroutine(this.Coroutine_Refresh(2f)); this.m_IsJudgeScore = false; if (!col.gameObject.CompareTag("MiniGameTarget")) { VRMiniGameManager.Instance.AddQueueVoice(VRMiniGameManager.VoiceType.Miss); return; } base.GetComponent().isKinematic = true; Transform transform = col.gameObject.transform; Transform transform2 = this.m_ColliderJudgeScore.transform; Vector3 vector = transform.worldToLocalMatrix.MultiplyPoint(transform2.position); vector.y = 0f; Debug.Log(string.Concat(new object[] { "的とのローカル位置:", vector, " ", vector.magnitude })); float num = Mathf.Atan2(-vector.z, -vector.x) * 57.29578f + 180f; float magnitude = vector.magnitude; Debug.Log("的の中央からの角度:" + num); Debug.Log("的の中央からの距離:" + magnitude); int num2; if (magnitude < 0.02f) { num2 = 50; } else if (magnitude < 0.052f) { num2 = 25; } else { int num3 = (int)((num + 9f) / 18f); int[] array = new int[] { 11, 14, 9, 12, 5, 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8 }; num3 = ((num3 < array.Length) ? num3 : (num3 - array.Length)); num2 = array[num3]; if (magnitude > 0.24f && magnitude <= 0.28f) { num2 *= 3; } else if (magnitude > 0.388f && magnitude <= 0.43f) { num2 *= 2; } if (magnitude > 0.43f) { num2 = 0; } } if (num2 > 0) { if (num2 > 20) { this.m_ParticleL.Play(); } else { this.m_ParticleS.Play(); } VRMiniGameManager.Instance.AddQueueVoice(VRMiniGameManager.VoiceType.Hit); } else { VRMiniGameManager.Instance.AddQueueVoice(VRMiniGameManager.VoiceType.Miss); } GameMain.Instance.SoundMgr.PlaySe("SE_vr0030.ogg", false); VRMiniGameManager.Instance.score += num2; } private void UpdateGrabTransform() { Transform transform = this.m_VRController.transform; Vector3 a = transform.localToWorldMatrix.MultiplyPoint3x4(Vector3.forward * this.m_Magnitude); Vector3 vector = a - base.transform.position; Vector3 zero = Vector3.zero; zero.x = Mathf.DeltaAngle(base.transform.eulerAngles.x, transform.eulerAngles.x); zero.y = Mathf.DeltaAngle(base.transform.eulerAngles.y, transform.eulerAngles.y); zero.z = Mathf.DeltaAngle(base.transform.eulerAngles.z, transform.eulerAngles.z); base.transform.position += vector; base.transform.eulerAngles += zero; base.GetComponent().velocity = Vector3.zero; base.GetComponent().angularVelocity = Vector3.zero; if (vector.magnitude > 1f) { return; } this.UpdateVectorArray(this.m_VelocityArray, vector * 60f); } private void UpdateVectorArray(Vector3[] array, Vector3 vec) { for (int i = array.Length - 1; i > 0; i--) { array[i] = array[i - 1]; } array[0] = vec; } private void GrabEnd(Vector3 Velocity, Vector3 AngularVelocity) { base.transform.position = this.m_VRController.transform.position; base.GetComponent().useGravity = true; this.m_IsGrab = false; this.m_GrabNum--; if (this.m_GrabNum > 0) { return; } if (this.m_IsJudgeScore) { return; } if (this.m_TargetObject) { Vector3 a = this.m_TargetObject.transform.position - base.transform.position; float num = -4.905f; float y = Velocity.y; float num2 = -a.y; float num3 = (-y - Mathf.Sqrt(y * y - 4f * num * num2)) / (2f * num); if (num3 > 0f) { Vector3 b = a / num3; b.y = Velocity.y; float sqrMagnitude = (Velocity - b).sqrMagnitude; Debug.DrawLine(base.transform.position, base.transform.position + Velocity, Color.red, 5f); Debug.DrawLine(base.transform.position, base.transform.position + b, Color.green, 5f); Velocity = Vector3.Lerp(Velocity, b, this.m_DebugPower / sqrMagnitude); Debug.DrawLine(base.transform.position, base.transform.position + Velocity, Color.yellow, 5f); } } base.GetComponent().AddForce(Velocity, ForceMode.VelocityChange); base.GetComponent().useGravity = true; AudioSource component = base.GetComponent(); int num4 = (int)this.m_SoundThreshold.Evaluate(Velocity.magnitude); num4 = ((num4 >= 0) ? num4 : 0); if (num4 == 0) { GameMain.Instance.SoundMgr.PlaySe("SE_vr0091.ogg", false); } else if (num4 == 1) { GameMain.Instance.SoundMgr.PlaySe("SE_vr0092.ogg", false); } else if (num4 == 2) { GameMain.Instance.SoundMgr.PlaySe("SE_vr0093.ogg", false); } else { GameMain.Instance.SoundMgr.PlaySe("SE_vr0094.ogg", false); } if (num4 == 0) { this.m_IsDropped = true; } else { VRMiniGameManager.Instance.life--; this.m_IsJudgeScore = true; } } private Vector3 CalcThrowVec(Vector3[] VecArray) { Vector3 vector = Vector3.zero; for (int i = 0; i < VecArray.Length; i++) { vector += VecArray[i]; } if (VecArray.Length > 0) { vector /= (float)VecArray.Length; } Vector3 result = vector; if (VecArray.Length > 1) { result = result.normalized * VecArray[VecArray.Length - 1].magnitude; } return result; } private Vector3[] GetDeviationValue(Vector3[] array) { List list = new List(); list.Add(array[0]); for (int i = 1; i < array.Length; i++) { if (Vector3.Dot(array[i], array[0]) >= 0f) { list.Add(array[i]); } } Vector3[] array2 = list.ToArray(); float[] array3 = new float[array2.Length]; for (int j = 0; j < array3.Length; j++) { Vector3 vector = array2[j]; array3[j] = Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z); } float num = 0f; for (int k = 0; k < array3.Length; k++) { num += array3[k]; } num /= (float)array3.Length; float[] array4 = new float[array3.Length]; for (int l = 0; l < array4.Length; l++) { float num2 = num - array3[l]; array4[l] = num2 * num2; } float num3 = 0f; for (int m = 0; m < array4.Length; m++) { num3 += array4[m]; } num3 /= (float)array4.Length; float num4 = Mathf.Sqrt(num3); float[] array5 = new float[array4.Length]; for (int n = 0; n < array5.Length; n++) { float num5 = (array3[n] - num) * 10f; num5 /= num4; num5 = 50f + num5; array5[n] = num5; } list.Clear(); int num6 = 0; for (int num7 = 0; num7 < array5.Length; num7++) { if (array5[num7] >= 50f) { list.Add(array2[num7]); num6++; } } return list.ToArray(); } private Vector3[] m_VelocityArray = new Vector3[9]; [SerializeField] [Tooltip("投げた時の力の倍率。")] private float m_Power = 1f; [SerializeField] [Tooltip("掴んだ相手との距離。\nメートル単位で指定。")] private float m_Magnitude = 0.1f; [SerializeField] [Tooltip("スコア判定を行うためのゲームオブジェクト。\nトランスフォームのパラメータがBoxColliderとして働きます。")] private GameObject m_ColliderJudgeScore; [SerializeField] [Range(0f, 1000f)] [Tooltip("投げた時にターゲットへ向かう補正の力。")] private float m_DebugPower; [Space(16f)] [SerializeField] [Tooltip("投げた時の力を、効果音のインデックスに対応させる。\n横軸:投げる力\n縦軸:対応する効果音のインデックス\n効果音は Sound Throwing が使われます。")] private AnimationCurve m_SoundThreshold; private bool m_IsJudgeScore; private int m_GrabNum; private GameObject m_TargetObject; [SerializeField] private ParticleSystem m_ParticleS; [SerializeField] private ParticleSystem m_ParticleL; [SerializeField] private Transform m_Pivot; private Vector3 m_StartPivotLocalAngles; private AVRControllerButtons m_VRController; private bool m_IsGrab = true; private bool m_IsDropped; private bool m_IsRespawn; }