VRMiniGameWanage.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class VRMiniGameWanage : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. base.GetComponent<Rigidbody>().maxAngularVelocity = 12.566371f;
  10. base.GetComponent<Rigidbody>().sleepThreshold = 0.5f;
  11. this.m_VRController = VRMiniGameManager.Instance.VRController;
  12. GameObject[] array = GameObject.FindGameObjectsWithTag("MiniGameTarget");
  13. this.m_TargetObject = array[UnityEngine.Random.Range(0, array.Length - 1)];
  14. VRMiniGameSetup miniGameSetup = UnityEngine.Object.FindObjectOfType<VRMiniGameSetup>();
  15. if (miniGameSetup.Is2DMode)
  16. {
  17. VRMiniGameGaugeeManager gaugeManager = miniGameSetup.GaugeManager;
  18. gaugeManager.onMouseClickDownEvent = (VRMiniGameGaugeeManager.OnMouseClickEvent)Delegate.Combine(gaugeManager.onMouseClickDownEvent, new VRMiniGameGaugeeManager.OnMouseClickEvent(delegate(VRMiniGameGaugeeManager manager)
  19. {
  20. if (miniGameSetup.GaugeManager.isPlay)
  21. {
  22. return;
  23. }
  24. if (!this.m_IsGrab)
  25. {
  26. return;
  27. }
  28. miniGameSetup.GaugeManager.Play();
  29. }));
  30. VRMiniGameGaugeeManager gaugeManager2 = miniGameSetup.GaugeManager;
  31. gaugeManager2.onMouseClickUpEvent = (VRMiniGameGaugeeManager.OnMouseClickEvent)Delegate.Combine(gaugeManager2.onMouseClickUpEvent, new VRMiniGameGaugeeManager.OnMouseClickEvent(delegate(VRMiniGameGaugeeManager manager)
  32. {
  33. if (!miniGameSetup.GaugeManager.isPlay)
  34. {
  35. return;
  36. }
  37. if (!this.m_IsGrab)
  38. {
  39. return;
  40. }
  41. OvrCamera useCamera = miniGameSetup.UseCamera;
  42. float num = 0.1f;
  43. float num2 = 7f;
  44. float gaugeValue = miniGameSetup.GaugeManager.gaugeValue;
  45. float d = num + (num2 - num) * gaugeValue;
  46. Vector3 vector = this.m_VRController.transform.position - useCamera.m_goCenterEye.transform.position;
  47. vector = (Quaternion.AngleAxis(-30f, useCamera.m_goCenterEye.transform.right) * vector).normalized * d;
  48. Vector3[] deviationValue = this.GetDeviationValue(this.m_VelocityArray);
  49. vector += this.CalcThrowVec(deviationValue);
  50. Vector3[] deviationValue2 = this.GetDeviationValue(this.m_EulerAngleArray);
  51. Vector3 vector2 = this.CalcThrowVec(deviationValue2);
  52. vector2 = Vector3.Scale(vector2, this.transform.up);
  53. this.GrabEnd(vector, vector2);
  54. miniGameSetup.GaugeManager.Stop();
  55. }));
  56. }
  57. else if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.VIVE)
  58. {
  59. if (this.m_VRController.HandSide)
  60. {
  61. this.m_Pivot.localPosition = new Vector3(0f, -0.01f, -0.02f);
  62. }
  63. else
  64. {
  65. this.m_Pivot.localPosition = new Vector3(0f, 0.01f, -0.02f);
  66. }
  67. this.m_Pivot.localEulerAngles = Vector3.zero;
  68. }
  69. else
  70. {
  71. this.m_Pivot.localPosition = Vector3.zero;
  72. }
  73. }
  74. private void Update()
  75. {
  76. this.m_Particle.transform.rotation = Quaternion.AngleAxis(-90f, Vector3.right);
  77. if (VRMiniGameManager.Instance.MiniGameState != VRMiniGameManager.VRMiniGameState.Playing)
  78. {
  79. return;
  80. }
  81. if (this.m_IsRespawn)
  82. {
  83. return;
  84. }
  85. if (base.transform.position.y < -5f)
  86. {
  87. if (this.m_IsJudgeScore)
  88. {
  89. Debug.Log("[ ミニゲーム ]投擲したが、奈落に落ちた。");
  90. this.m_IsJudgeScore = false;
  91. }
  92. this.Refresh();
  93. }
  94. if (this.m_IsJudgeScore)
  95. {
  96. if (base.GetComponent<Rigidbody>().velocity.sqrMagnitude < 1f)
  97. {
  98. Transform transform = this.m_ColliderJudgeScore.transform;
  99. Collider[] array = Physics.OverlapBox(transform.position, transform.lossyScale * 0.5f, transform.rotation);
  100. bool flag = false;
  101. for (int i = 0; i < array.Length; i++)
  102. {
  103. if (array[i].CompareTag("MiniGameTarget"))
  104. {
  105. flag = true;
  106. break;
  107. }
  108. }
  109. if (flag)
  110. {
  111. Debug.Log("[ ミニゲーム ]投擲に成功!!");
  112. VRMiniGameManager.Instance.score++;
  113. VRMiniGameManager.Instance.AddQueueVoice(VRMiniGameManager.VoiceType.Hit);
  114. this.m_IsJudgeScore = false;
  115. this.m_Particle.Play();
  116. base.StartCoroutine(this.Coroutine_Refresh(2f));
  117. }
  118. }
  119. if (base.GetComponent<Rigidbody>().IsSleeping())
  120. {
  121. VRMiniGameManager.Instance.AddQueueVoice(VRMiniGameManager.VoiceType.Miss);
  122. Debug.Log("[ ミニゲーム ]投擲したが、失敗した。");
  123. this.m_IsJudgeScore = false;
  124. base.StartCoroutine(this.Coroutine_Refresh(2f));
  125. }
  126. }
  127. if (this.m_IsDropped && !this.m_IsJudgeScore && base.GetComponent<Rigidbody>().IsSleeping())
  128. {
  129. Debug.Log("[ ミニゲーム ]投擲したが、勢いが弱かった");
  130. this.m_IsJudgeScore = false;
  131. base.StartCoroutine(this.Coroutine_Refresh(2f));
  132. }
  133. if (!this.m_VRController)
  134. {
  135. return;
  136. }
  137. if (this.m_IsGrab)
  138. {
  139. this.UpdateGrabTransform();
  140. if (this.m_VRController.GetPressDown(AVRControllerButtons.BTN.TRIGGER))
  141. {
  142. base.GetComponent<Rigidbody>().useGravity = false;
  143. if (this.m_TargetObject == null)
  144. {
  145. GameObject[] array2 = GameObject.FindGameObjectsWithTag("MiniGameTarget");
  146. this.m_TargetObject = array2[UnityEngine.Random.Range(0, array2.Length - 1)];
  147. }
  148. }
  149. if (this.m_VRController.GetPress(AVRControllerButtons.BTN.TRIGGER))
  150. {
  151. }
  152. if (this.m_VRController.GetPressUp(AVRControllerButtons.BTN.TRIGGER))
  153. {
  154. Vector3[] deviationValue = this.GetDeviationValue(this.m_VelocityArray);
  155. Vector3 vector = this.CalcThrowVec(deviationValue);
  156. vector = Vector3.Scale(vector, base.transform.up);
  157. Vector3[] deviationValue2 = this.GetDeviationValue(this.m_VelocityArray);
  158. Vector3 vector2 = this.CalcThrowVec(deviationValue2);
  159. vector2 *= this.m_Power;
  160. this.GrabEnd(vector2, vector);
  161. }
  162. }
  163. }
  164. private void Refresh()
  165. {
  166. if (!this.m_IsDropped)
  167. {
  168. VRMiniGameManager.VoiceType type = (VRMiniGameManager.VoiceType)Enum.Parse(typeof(VRMiniGameManager.VoiceType), "Remaining" + VRMiniGameManager.Instance.life.ToString());
  169. VRMiniGameManager.Instance.AddQueueVoice(type);
  170. }
  171. if (VRMiniGameManager.Instance.life <= 0)
  172. {
  173. VRMiniGameSetup vrminiGameSetup = UnityEngine.Object.FindObjectOfType<VRMiniGameSetup>();
  174. vrminiGameSetup.GaugeManager.isVisible = false;
  175. VRMiniGameManager.Instance.Result(delegate
  176. {
  177. Debug.Log("輪投げを終了。");
  178. });
  179. return;
  180. }
  181. Rigidbody component = base.GetComponent<Rigidbody>();
  182. component.velocity = Vector3.zero;
  183. component.useGravity = false;
  184. component.isKinematic = false;
  185. this.m_IsGrab = true;
  186. this.m_IsDropped = false;
  187. this.m_IsRespawn = false;
  188. this.m_GrabNum = 0;
  189. for (int i = 0; i < this.m_VelocityArray.Length; i++)
  190. {
  191. this.m_VelocityArray[i] = Vector3.zero;
  192. }
  193. }
  194. private IEnumerator Coroutine_Refresh(float tim)
  195. {
  196. this.m_IsRespawn = true;
  197. yield return new WaitForSeconds(tim);
  198. this.Refresh();
  199. yield break;
  200. }
  201. private void UpdateGrabTransform()
  202. {
  203. Transform transform = this.m_VRController.transform;
  204. Vector3 a = transform.localToWorldMatrix.MultiplyPoint3x4(Vector3.forward * this.m_Magnitude);
  205. Vector3 vector = a - base.transform.position;
  206. Vector3 zero = Vector3.zero;
  207. zero.x = Mathf.DeltaAngle(base.transform.eulerAngles.x, transform.transform.eulerAngles.x);
  208. zero.y = Mathf.DeltaAngle(base.transform.eulerAngles.y, transform.transform.eulerAngles.y);
  209. zero.z = Mathf.DeltaAngle(base.transform.eulerAngles.z, transform.transform.eulerAngles.z);
  210. base.transform.position += vector;
  211. base.transform.eulerAngles += zero;
  212. base.GetComponent<Rigidbody>().velocity = Vector3.zero;
  213. base.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
  214. if (vector.magnitude > 1f)
  215. {
  216. return;
  217. }
  218. this.UpdateVectorArray(this.m_VelocityArray, vector * 60f);
  219. this.UpdateVectorArray(this.m_EulerAngleArray, zero);
  220. }
  221. private void UpdateVectorArray(Vector3[] array, Vector3 vec)
  222. {
  223. for (int i = array.Length - 1; i > 0; i--)
  224. {
  225. array[i] = array[i - 1];
  226. }
  227. array[0] = vec;
  228. }
  229. private void GrabEnd(Vector3 Velocity, Vector3 AngularVelocity)
  230. {
  231. base.GetComponent<Rigidbody>().useGravity = true;
  232. this.m_IsGrab = false;
  233. this.m_GrabNum--;
  234. if (this.m_GrabNum > 0)
  235. {
  236. return;
  237. }
  238. if (this.m_IsJudgeScore)
  239. {
  240. return;
  241. }
  242. if (this.m_TargetObject)
  243. {
  244. Vector3 a = this.m_TargetObject.transform.position - base.transform.position;
  245. float num = -4.905f;
  246. float y = Velocity.y;
  247. float num2 = -a.y;
  248. float num3 = (-y - Mathf.Sqrt(y * y - 4f * num * num2)) / (2f * num);
  249. if (num3 > 0f)
  250. {
  251. Vector3 b = a / num3;
  252. b.y = Velocity.y;
  253. float sqrMagnitude = (Velocity - b).sqrMagnitude;
  254. Debug.DrawLine(base.transform.position, base.transform.position + Velocity, Color.red, 5f);
  255. Debug.DrawLine(base.transform.position, base.transform.position + b, Color.green, 5f);
  256. Velocity = Vector3.Lerp(Velocity, b, this.m_DebugPower / sqrMagnitude);
  257. Debug.DrawLine(base.transform.position, base.transform.position + Velocity, Color.yellow, 5f);
  258. }
  259. }
  260. base.GetComponent<Rigidbody>().AddForce(Velocity, ForceMode.VelocityChange);
  261. base.GetComponent<Rigidbody>().AddTorque(AngularVelocity, ForceMode.VelocityChange);
  262. base.GetComponent<Rigidbody>().useGravity = true;
  263. AudioSource component = base.GetComponent<AudioSource>();
  264. int num4 = (int)this.m_SoundThreshold.Evaluate(Velocity.magnitude);
  265. num4 = ((num4 >= 0) ? num4 : 0);
  266. if (num4 == 0)
  267. {
  268. GameMain.Instance.SoundMgr.PlaySe("SE_vr0091.ogg", false);
  269. }
  270. else if (num4 == 1)
  271. {
  272. GameMain.Instance.SoundMgr.PlaySe("SE_vr0092.ogg", false);
  273. }
  274. else if (num4 == 2)
  275. {
  276. GameMain.Instance.SoundMgr.PlaySe("SE_vr0093.ogg", false);
  277. }
  278. else
  279. {
  280. GameMain.Instance.SoundMgr.PlaySe("SE_vr0094.ogg", false);
  281. }
  282. if (num4 == 0)
  283. {
  284. this.m_IsDropped = true;
  285. }
  286. else
  287. {
  288. VRMiniGameManager.Instance.life--;
  289. this.m_IsJudgeScore = true;
  290. }
  291. }
  292. private Vector3 CalcThrowVec(Vector3[] VecArray)
  293. {
  294. Vector3 vector = Vector3.zero;
  295. for (int i = 0; i < VecArray.Length; i++)
  296. {
  297. vector += VecArray[i];
  298. }
  299. if (VecArray.Length > 0)
  300. {
  301. vector /= (float)VecArray.Length;
  302. }
  303. Vector3 result = vector;
  304. if (VecArray.Length > 1)
  305. {
  306. result = result.normalized * VecArray[VecArray.Length - 1].magnitude;
  307. }
  308. return result;
  309. }
  310. private Vector3[] GetDeviationValue(Vector3[] array)
  311. {
  312. List<Vector3> list = new List<Vector3>();
  313. list.Add(array[0]);
  314. for (int i = 1; i < array.Length; i++)
  315. {
  316. if (Vector3.Dot(array[i], array[0]) >= 0f)
  317. {
  318. list.Add(array[i]);
  319. }
  320. }
  321. Vector3[] array2 = list.ToArray();
  322. float[] array3 = new float[array2.Length];
  323. for (int j = 0; j < array3.Length; j++)
  324. {
  325. Vector3 vector = array2[j];
  326. array3[j] = Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
  327. }
  328. float num = 0f;
  329. for (int k = 0; k < array3.Length; k++)
  330. {
  331. num += array3[k];
  332. }
  333. num /= (float)array3.Length;
  334. float[] array4 = new float[array3.Length];
  335. for (int l = 0; l < array4.Length; l++)
  336. {
  337. float num2 = num - array3[l];
  338. array4[l] = num2 * num2;
  339. }
  340. float num3 = 0f;
  341. for (int m = 0; m < array4.Length; m++)
  342. {
  343. num3 += array4[m];
  344. }
  345. num3 /= (float)array4.Length;
  346. float num4 = Mathf.Sqrt(num3);
  347. float[] array5 = new float[array4.Length];
  348. for (int n = 0; n < array5.Length; n++)
  349. {
  350. float num5 = (array3[n] - num) * 10f;
  351. num5 /= num4;
  352. num5 = 50f + num5;
  353. array5[n] = num5;
  354. }
  355. list.Clear();
  356. int num6 = 0;
  357. for (int num7 = 0; num7 < array5.Length; num7++)
  358. {
  359. if (array5[num7] >= 50f)
  360. {
  361. list.Add(array2[num7]);
  362. num6++;
  363. }
  364. }
  365. return list.ToArray();
  366. }
  367. private Vector3[] m_VelocityArray = new Vector3[9];
  368. private Vector3[] m_EulerAngleArray = new Vector3[9];
  369. [SerializeField]
  370. [Tooltip("投げた時の力の倍率。")]
  371. private float m_Power = 1f;
  372. [SerializeField]
  373. [Tooltip("掴んだ相手との距離。\nメートル単位で指定。")]
  374. private float m_Magnitude = 0.1f;
  375. [SerializeField]
  376. [Tooltip("スコア判定を行うためのゲームオブジェクト。\nトランスフォームのパラメータがBoxColliderとして働きます。")]
  377. private GameObject m_ColliderJudgeScore;
  378. [SerializeField]
  379. [Range(0f, 1000f)]
  380. [Tooltip("投げた時にターゲットへ向かう補正の力。")]
  381. private float m_DebugPower;
  382. [Space(16f)]
  383. [SerializeField]
  384. [Tooltip("投げた時の力を、効果音のインデックスに対応させる。\n横軸:投げる力\n縦軸:対応する効果音のインデックス\n効果音は Sound Throwing が使われます。")]
  385. private AnimationCurve m_SoundThreshold;
  386. private bool m_IsJudgeScore;
  387. private int m_GrabNum;
  388. private GameObject m_TargetObject;
  389. [SerializeField]
  390. private ParticleSystem m_Particle;
  391. [SerializeField]
  392. private Transform m_Pivot;
  393. private AVRControllerButtons m_VRController;
  394. private bool m_IsGrab = true;
  395. private bool m_IsDropped;
  396. private bool m_IsRespawn;
  397. }