RunnersController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.AI;
  7. using UnityEngine.UI;
  8. public class RunnersController : MonoBehaviour
  9. {
  10. [SerializeField]
  11. public NavMeshAgent Agent;
  12. [SerializeField]
  13. private int IDPlayer;
  14. [SerializeField]
  15. public Transform _target;
  16. public TextMeshPro NamePlayerText;
  17. public bool IsPlayer;
  18. public bool IsFly;
  19. //public bool IsJump;
  20. public bool IsSpeedUp;
  21. public GameObject BaloonObject;
  22. public SwipeMove SwipeMove;
  23. public Animator Anim;
  24. public Vector3 StartPos;
  25. private float TimerFly = 3;
  26. private float DefaultTimerFly = 3;
  27. private Rigidbody Rig;
  28. private float _gravity = 9.8f;
  29. public void SetTarget(Transform tr)
  30. {
  31. _target = tr;
  32. }
  33. public void SetPlayer(string text)
  34. {
  35. SwipeMove = GetComponent<SwipeMove>();
  36. SwipeMove.enabled = true;
  37. UIManager.Instance.CompleteCameraController.Target = this.transform;
  38. NamePlayerText.text = text;
  39. NamePlayerText.gameObject.SetActive(true);
  40. //SwipeMove.enabled = true;
  41. }
  42. public void SetRollerAnim()
  43. {
  44. Anim.SetTrigger("SitRoller");
  45. }
  46. public void Start()
  47. {
  48. Agent.enabled = true;
  49. SwipeMove = GetComponent<SwipeMove>();
  50. //Destination();
  51. Agent.enabled = false;
  52. NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
  53. NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
  54. StartPos = transform.position;
  55. Rig = GetComponent<Rigidbody>();
  56. }
  57. public void SetFlyToTowerAnim()
  58. {
  59. Anim.SetBool("FlyInTower", true);
  60. }
  61. public void EnableFlyToTowerAnim()
  62. {
  63. Anim.SetBool("FlyInTower", false);
  64. }
  65. public void ResetPos()
  66. {
  67. transform.position = StartPos;
  68. }
  69. public void FlyPlayer()
  70. {
  71. TimerFly = DefaultTimerFly;
  72. Anim.SetBool("Move", false);
  73. Anim.SetBool("Fly", true);
  74. IsFly = true;
  75. }
  76. public void Idle()
  77. {
  78. Anim.SetBool("Move", false);
  79. Anim.SetBool("Fly", false);
  80. Anim.SetBool("Kiss", false);
  81. Anim.SetBool("Fall", false);
  82. }
  83. public void Move()
  84. {
  85. //IsFly = true;
  86. //IsMove = false;
  87. //IsKiss = false;
  88. //IsJump = false;
  89. if(Core.Instance.IsGameMode != Core.StateModeGame.Kiss)
  90. {
  91. Anim.SetBool("Kiss", false);
  92. Anim.SetBool("Move", true);
  93. Anim.SetBool("Fly", false);
  94. Anim.SetBool("Fall", false);
  95. }
  96. else
  97. {
  98. Anim.SetBool("Kiss", true);
  99. Anim.SetBool("Move", false);
  100. Anim.SetBool("Fly", false);
  101. Anim.SetBool("Fall", false);
  102. }
  103. }
  104. public void Kiss()
  105. {
  106. //IsFly = true;
  107. //IsMove = false;
  108. //IsKiss = false;
  109. //IsJump = false;
  110. Anim.SetBool("Move", false);
  111. Anim.SetBool("Fly", false);
  112. Anim.SetBool("Kiss", true);
  113. Anim.SetBool("Fall", false);
  114. //Anim.PlayInFixedTime("Kiss", 1, 2f);
  115. }
  116. public void FallPlayer()
  117. {
  118. Anim.SetBool("Move", false);
  119. Anim.SetBool("Fly", false);
  120. Anim.SetBool("Kiss", false);
  121. Anim.SetBool("Fall", true);
  122. Agent.speed = 0;
  123. }
  124. private void OnGameOver(Notification note)
  125. {
  126. //throw new NotImplementedException();
  127. }
  128. private void OnDestroy()
  129. {
  130. NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
  131. NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver);
  132. }
  133. public void Stumble()
  134. {
  135. Anim.SetTrigger("Stumble");
  136. }
  137. private void OnGameStart(Notification note)
  138. {
  139. Agent.enabled = true;
  140. Anim.SetBool("Move", true);
  141. Destination();
  142. }
  143. public void Destination()
  144. {
  145. if (Agent.enabled)
  146. {
  147. try
  148. {
  149. Agent.destination = _target.position;
  150. }
  151. catch
  152. {
  153. }
  154. }
  155. }
  156. private void Update()
  157. {
  158. if(Core.Instance.LevelEnd)
  159. {
  160. Idle();
  161. return;
  162. }
  163. if(Anim.GetBool("Fall"))
  164. {
  165. Agent.enabled = false;
  166. transform.Translate(transform.up * -_gravity * Time.deltaTime);
  167. return;
  168. }
  169. if(IsFly)
  170. {
  171. TimerFly -= Time.deltaTime;
  172. if(TimerFly <= 0)
  173. {
  174. //TimerFly = DefaultTimerFly;
  175. IsFly = false;
  176. Move();
  177. }
  178. }
  179. if(Core.Instance.IsGameMode == Core.StateModeGame.IsGameMode)
  180. {
  181. //switch(SwipeMove.DirSwipe)
  182. // {
  183. // case SwipeMove.DirectionSwipe.Left:
  184. // Anim.SetFloat("Blend", Mathf.Lerp(Anim.GetFloat("Blend"), 0, 10 * Time.deltaTime));
  185. // break;
  186. // case SwipeMove.DirectionSwipe.Right:
  187. // Anim.SetFloat("Blend", Mathf.Lerp(Anim.GetFloat("Blend"), 1, 10 * Time.deltaTime));
  188. // break;
  189. // }
  190. Anim.SetFloat("Blend", Mathf.Lerp(Anim.GetFloat("Blend"), 0.5f, 1 * Time.deltaTime));
  191. //if (Vector3.Dot(vel, transform.right) > 0.01f)
  192. //{
  193. //}
  194. //else if (Vector3.Dot(vel, transform.right) < 0.01f)
  195. //{
  196. // Anim.SetFloat("Blend", Mathf.Lerp(Anim.GetFloat("Blend"), 0, 10 * Time.deltaTime));
  197. //}
  198. //else
  199. {
  200. Anim.SetFloat("Blend", 0.5f);
  201. }
  202. if(Agent)
  203. {
  204. Agent.SetDestination(_target.position);
  205. Agent.autoTraverseOffMeshLink = true;
  206. }
  207. }
  208. else
  209. {
  210. Agent.autoTraverseOffMeshLink = false;
  211. }
  212. }
  213. public void ResetAnimation()
  214. {
  215. ResetAllAnimation();
  216. Destination();
  217. }
  218. public void ResetAllAnimation()
  219. {
  220. Anim.SetBool("Move", false);
  221. Anim.SetBool("Fly", false);
  222. Anim.SetBool("Kiss", false);
  223. Anim.SetBool("Fall", false);
  224. Anim.SetBool("FlyInTower", false);
  225. }
  226. }