TaskItem.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using DG.Tweening;
  7. public class TaskItem : MonoBehaviour
  8. {
  9. public List<GameObject> shouzhilist;
  10. public AudioSource asaudio;
  11. public int type = -1;
  12. public int nextid = 1;
  13. public GameObject t;
  14. public GameObject endArea;
  15. public GameObject dhs;
  16. private void Start()
  17. {
  18. if (dhs != null)
  19. {
  20. playjinai(false);
  21. }
  22. }
  23. public void starttask(TaskManager action)
  24. {
  25. for (int i = 0; i < action.tasklist.Count; i++)
  26. {
  27. if(action.tasklist[i]!=this)
  28. {
  29. action.tasklist[i].gameObject.SetActive(false);
  30. }
  31. }
  32. this.gameObject.SetActive(true);
  33. StartCoroutine(checkend(action));
  34. if(dhs!=null)
  35. {
  36. dhs.SetActive(true);
  37. }
  38. asaudio.Play();
  39. }
  40. void playjinai(bool isshang)
  41. {
  42. return;
  43. if(isshang)
  44. {
  45. dhs.transform.DOLocalMoveY(0.4f, 1f).OnComplete(() => {
  46. playjinai(!isshang);
  47. });
  48. }
  49. else
  50. {
  51. dhs.transform.DOLocalMoveY(0.5f, 1f).OnComplete(() => {
  52. playjinai(!isshang);
  53. });
  54. }
  55. }
  56. private IEnumerator checkend(TaskManager action)
  57. {
  58. yield return null;
  59. switch(type)
  60. {
  61. case 0:
  62. bool isfinish=false;
  63. float tmx2 = 0;
  64. while (!isfinish)
  65. {
  66. yield return null;
  67. if (action.text.text.Contains("Open"))
  68. {
  69. yield return new WaitForSeconds(2f);
  70. if (action.text.text.Contains("Open"))
  71. {
  72. isfinish = true;
  73. }
  74. }
  75. tmx2 = Time.deltaTime + tmx2;
  76. if (tmx2 > 4)
  77. {
  78. tmx2 = 0;
  79. asaudio.Play();
  80. }
  81. }
  82. break;
  83. case 1:
  84. bool isfinish2 = false;
  85. float inithandy = 0;
  86. float linithandy = 0;
  87. float Hinithandy = 0;
  88. float tmx = 0;
  89. while (!isfinish2)
  90. {
  91. if(Mathf.Abs(UdpServer.resR)>40)
  92. {
  93. action.pm.playan("pawL");
  94. isfinish2 = true;
  95. }
  96. yield return null;
  97. if(Vector3.Distance( dhs.transform.position, action.text.transform.position)<=0.2f)
  98. {
  99. //if ((action.text.text.Contains("Grab") || action.text.text.Contains("Pinch")))
  100. // {
  101. if (inithandy == 0)
  102. inithandy = action.text.transform.position.y;
  103. if (Mathf.Abs(inithandy - action.text.transform.position.y) > 0.1f)
  104. {
  105. action.pm.playan("pawL");
  106. inithandy = 0;
  107. isfinish2 = true;
  108. }
  109. // }
  110. }
  111. if (Vector3.Distance(dhs.transform.position, action.ltext.transform.position) <= 0.3f)
  112. {
  113. // if ((action.ltext.text.Contains("Grab") || action.ltext.text.Contains("Pinch")))
  114. // {
  115. if (linithandy == 0)
  116. linithandy = action.ltext.transform.position.y;
  117. if (Mathf.Abs(linithandy - action.ltext.transform.position.y) > 0.1f)
  118. {
  119. action.pm.playan("pawL");
  120. linithandy = 0;
  121. isfinish2 = true;
  122. }
  123. // }
  124. }
  125. if (Vector3.Distance(dhs.transform.position, action.Hand.transform.position) <= 0.3f)
  126. {
  127. // if ((action.ltext.text.Contains("Grab") || action.ltext.text.Contains("Pinch")))
  128. // {
  129. if (action.Hand.transform.localPosition.y >= 0&& Vector3.Distance(dhs.transform.position, action.Hand.transform.position) <= 0.05f)
  130. Hinithandy = action.Hand.transform.localPosition.y;
  131. if (Hinithandy - action.Hand.transform.localPosition.y > 0.1f&& Hinithandy!=0)
  132. {
  133. action.pm.playan("pawL");
  134. Hinithandy = 0;
  135. isfinish2 = true;
  136. }
  137. // }
  138. }
  139. tmx = Time.deltaTime + tmx;
  140. if (tmx>3)
  141. {
  142. tmx = 0;
  143. asaudio.Play();
  144. }
  145. }
  146. action.pm.isplayend = false;
  147. break;
  148. case 2:
  149. yield return new WaitForSeconds(3f);
  150. break;
  151. default:
  152. Vector3 v1p = new Vector3(endArea.transform.position.x, 0, endArea.transform.position.z);
  153. Vector3 v2p = new Vector3(OpenXRCamera.Instance.head.position.x, 0, OpenXRCamera.Instance.head.position.z);
  154. float tmx3 = 0;
  155. while (Vector3.Distance(v1p, v2p) > 0.5f)
  156. {
  157. v1p = new Vector3(endArea.transform.position.x, 0, endArea.transform.position.z);
  158. v2p = new Vector3(OpenXRCamera.Instance.head.position.x, 0, OpenXRCamera.Instance.head.position.z);
  159. yield return null;
  160. tmx3 = Time.deltaTime + tmx3;
  161. if (tmx3 > 4)
  162. {
  163. tmx3 = 0;
  164. asaudio.Play();
  165. }
  166. }
  167. break;
  168. }
  169. action.tasklist[nextid].starttask(action);
  170. }
  171. private void Update()
  172. {
  173. t.transform.position = OpenXRCamera.Instance.head.position;
  174. t.transform.eulerAngles = OpenXRCamera.Instance.head.eulerAngles;
  175. for (int i = 0; i < shouzhilist.Count; i++)
  176. {
  177. shouzhilist[i].transform.localEulerAngles = new Vector3(shouzhilist[i].transform.localEulerAngles.x, shouzhilist[i].transform.localEulerAngles.y, -Mathf.Abs
  178. (UdpServer.resR));
  179. }
  180. }
  181. }