SceneEvent.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using UnityEngine;
  2. using System.Collections;
  3. public enum SceneEventType
  4. {
  5. Unmovable = 1, //没有移动路线
  6. Movable = 2, //需要布置移动路径的事件类型
  7. }
  8. public enum SceneMoveType
  9. {
  10. PingPong = 1,
  11. Circle = 2, //循环
  12. }
  13. public class SceneEvent : MonoBehaviour
  14. {
  15. [System.Serializable]
  16. public struct SceneEventMovePath
  17. {
  18. public Transform startPoint; //起始点
  19. public Transform endPoint; //结束点
  20. public float moveSpeed; //移动速度
  21. public float pauseTime; //停留时间
  22. }
  23. public int EventId; //事件ID
  24. public SceneEventType EventType = SceneEventType.Unmovable; //事件类型
  25. public SceneMoveType MoveType = SceneMoveType.PingPong;
  26. public Transform EventTarget = null; //事件对象
  27. public SceneEventMovePath[] MovePaths = null; //事件移动路径
  28. public float showTime = 10;
  29. public int[] canUsePoints;
  30. float beginShowTime = 0;
  31. int curPathIdx = 0;
  32. SceneEventMovePath curMovePath;
  33. bool startMove = false;
  34. bool reverse = false;
  35. bool pause = false;
  36. float beginPauseTime = 0;
  37. bool start2End = true;
  38. Animator targetAnimator;
  39. private int mEventPointPos = -1;
  40. public int EventPointPos
  41. {
  42. get { return mEventPointPos; }
  43. set { mEventPointPos = value; }
  44. }
  45. public bool IsArrange
  46. {
  47. get { return mEventPointPos >= 0; }
  48. }
  49. // Use this for initialization
  50. void Start()
  51. {
  52. if(EventTarget!=null)
  53. targetAnimator = EventTarget.GetComponent<Animator>();
  54. }
  55. public void Show()
  56. {
  57. this.enabled = true;
  58. if (EventTarget == null) return;
  59. EventTarget.gameObject.SetActive(true);
  60. beginShowTime = Time.realtimeSinceStartup;
  61. }
  62. public void Hide()
  63. {
  64. this.enabled = false;
  65. if (EventTarget == null) return;
  66. EventTarget.gameObject.SetActive(false);
  67. }
  68. // Update is called once per frame
  69. void Update()
  70. {
  71. if (EventTarget == null) return;
  72. if (EventType != SceneEventType.Movable)
  73. {
  74. if((Time.realtimeSinceStartup - beginShowTime ) >= showTime)
  75. {
  76. Hide();
  77. }
  78. return;
  79. }
  80. if (MovePaths == null || MovePaths.Length == 0) return;
  81. if(curPathIdx < MovePaths.Length)
  82. {
  83. if (startMove)
  84. {
  85. MoveOnPath(Time.deltaTime);
  86. }
  87. else
  88. {
  89. MoveNextPath();
  90. startMove = true;
  91. }
  92. }
  93. }
  94. void MoveOnPath(float deltaTime)
  95. {
  96. if(pause)
  97. {
  98. if((Time.realtimeSinceStartup - beginPauseTime) >= curMovePath.pauseTime)
  99. {
  100. MoveNextPath();
  101. }
  102. return;
  103. }
  104. if (start2End)
  105. {
  106. Vector3 pos = Vector3.MoveTowards(EventTarget.position, curMovePath.endPoint.position, curMovePath.moveSpeed * deltaTime);
  107. EventTarget.position = pos;
  108. if (EventTarget.position.FEqual(curMovePath.endPoint.position))
  109. {
  110. pause = true;
  111. beginPauseTime = Time.realtimeSinceStartup;
  112. }
  113. }
  114. else
  115. {
  116. Vector3 pos = Vector3.MoveTowards(EventTarget.position, curMovePath.startPoint.position, curMovePath.moveSpeed * deltaTime);
  117. EventTarget.position = pos;
  118. if (EventTarget.position.FEqual(curMovePath.startPoint.position))
  119. {
  120. pause = true;
  121. beginPauseTime = Time.realtimeSinceStartup;
  122. }
  123. }
  124. if (pause)
  125. {
  126. if (targetAnimator != null && targetAnimator.GetCurrentAnimatorStateInfo(0).IsName("f_idle"))
  127. {
  128. targetAnimator.Play("f_idle");
  129. }
  130. }
  131. }
  132. void MoveNextPath()
  133. {
  134. if(targetAnimator!=null && targetAnimator.GetCurrentAnimatorStateInfo(0).IsName("f_run"))
  135. {
  136. targetAnimator.Play("f_run");
  137. }
  138. pause = false;
  139. curMovePath = MovePaths[curPathIdx];
  140. if (reverse)
  141. {
  142. start2End = false;
  143. EventTarget.position = curMovePath.endPoint.position;
  144. curPathIdx--;
  145. if(curPathIdx < 0)
  146. {
  147. curPathIdx = 0;
  148. reverse = false;
  149. }
  150. if (EventTarget != null)
  151. {
  152. EventTarget.LookAt(curMovePath.startPoint.position);
  153. }
  154. }
  155. else
  156. {
  157. EventTarget.position = curMovePath.startPoint.position;
  158. start2End = true;
  159. curPathIdx++;
  160. if(curPathIdx >= MovePaths.Length)
  161. {
  162. if(MoveType == SceneMoveType.PingPong)
  163. {
  164. curPathIdx--;
  165. reverse = true;
  166. }
  167. else
  168. {
  169. curPathIdx = 0;
  170. reverse = false;
  171. }
  172. }
  173. if (EventTarget != null)
  174. {
  175. EventTarget.LookAt(curMovePath.endPoint.position);
  176. }
  177. }
  178. }
  179. }