VRMiniGameDarts.cs 13 KB

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