Duck.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using DG.Tweening;
  6. namespace DuckHunter
  7. {
  8. public class Duck : MonoBehaviour
  9. {
  10. public static List<Duck> DuckList = new List<Duck>();
  11. RectTransform _arrowRectTransform;
  12. RectTransform _parentRectTransform;
  13. RectTransform _rectTransform;
  14. DragonBones.UnityArmatureComponent _armature;
  15. [NonSerialized] public bool dead = false;
  16. float _direction = 1;
  17. public Action<Duck> onExit;
  18. public Action<Duck> onHitDead;
  19. public Action<Duck> onFallDonwStart;
  20. public Action<Duck> onFallDonwEnd;
  21. public DuckConfig config;
  22. void Start()
  23. {
  24. DuckList.Add(this);
  25. //获取基本组件
  26. _arrowRectTransform = transform.Find("Arrow") as RectTransform;
  27. _parentRectTransform = transform.parent as RectTransform;
  28. _rectTransform = transform as RectTransform;
  29. //初始化皮肤
  30. int useSkinIndex = config.type - 1;
  31. for (int i = 0; i < _rectTransform.childCount; i++)
  32. {
  33. GameObject skin = _rectTransform.GetChild(i).gameObject;
  34. bool needActive = i == useSkinIndex;
  35. skin.SetActive(needActive);
  36. //获取龙骨动画组件
  37. if (needActive) _armature = skin.GetComponent<DragonBones.UnityArmatureComponent>();
  38. }
  39. //初始化角度
  40. _flyAngle = config.flyAngle;
  41. //初始化速度
  42. _flySpeed = config.flySpeed;
  43. //初始化坐标
  44. _rectTransform.anchoredPosition = Vector2.right * config.positionX;
  45. //初始化动作
  46. StartActionCoroutine(Fly());
  47. }
  48. void OnDestroy()
  49. {
  50. DuckList.Remove(this);
  51. }
  52. void Update()
  53. {
  54. }
  55. Sequence _sequenceRotateForever;
  56. string _lastPlayAnimationName;
  57. void PlayAnimation(string animationName = null)
  58. {
  59. if (animationName == _lastPlayAnimationName) return;
  60. _lastPlayAnimationName = animationName;
  61. _armature.animation.Play(animationName, 0);
  62. if (_sequenceRotateForever != null)
  63. {
  64. _sequenceRotateForever.Kill();
  65. _sequenceRotateForever = null;
  66. }
  67. if (animationName == "03")
  68. {
  69. _arrowRectTransform.gameObject.SetActive(true);
  70. _arrowRectTransform.localPosition = new Vector3(-20, -55);
  71. _arrowRectTransform.localEulerAngles = new Vector3(0, 0, 145);
  72. }
  73. else if (animationName == "04")
  74. {
  75. _arrowRectTransform.gameObject.SetActive(true);
  76. _arrowRectTransform.localPosition = new Vector3(0, 35);
  77. _arrowRectTransform.localEulerAngles = new Vector3(0, 0, 0);
  78. _sequenceRotateForever = DOTween.Sequence();
  79. _sequenceRotateForever.Append(_arrowRectTransform.DOLocalRotate(Vector3.up * 180, 0.2f));
  80. _sequenceRotateForever.Append(_arrowRectTransform.DOLocalRotate(Vector3.up * 360, 0.2f));
  81. _sequenceRotateForever.SetLoops(-1);
  82. }
  83. else if (animationName == "05")
  84. {
  85. _arrowRectTransform.gameObject.SetActive(true);
  86. _arrowRectTransform.localPosition = new Vector3(34, -77);
  87. _arrowRectTransform.localEulerAngles = new Vector3(0, 0, 232);
  88. }
  89. else
  90. {
  91. _arrowRectTransform.gameObject.SetActive(false);
  92. };
  93. }
  94. static string Event_HitDead_Finish = "Event_HitDead_Finish";
  95. static string Event_FallDown_Finish = "Event_FallDown_Finish";
  96. static string Event_FlyTouchBound = "Event_FlyTouchBound";
  97. static string Event_FlyAway = "Event_FlyAway";
  98. void OnEvent(string eventName)
  99. {
  100. if (eventName == Event_HitDead_Finish)
  101. {
  102. StartActionCoroutine(FallDown());
  103. }
  104. else if (eventName == Event_FallDown_Finish)
  105. {
  106. onExit?.Invoke(this);
  107. }
  108. else if (eventName == Event_FlyTouchBound)
  109. {
  110. if (_touchBoundCount >= config.touchBoundCount)
  111. {
  112. StartActionCoroutine(FlyAway());
  113. }
  114. else
  115. {
  116. _flyAngle = CalculateTurnbackAngle();
  117. StartActionCoroutine(Fly());
  118. }
  119. }
  120. else if (eventName == Event_FlyAway)
  121. {
  122. Destroy(gameObject);
  123. onExit?.Invoke(this);
  124. }
  125. }
  126. Coroutine _actionCoroutine = null;
  127. void StartActionCoroutine(IEnumerator routine)
  128. {
  129. StopActionCoroutine();
  130. _actionCoroutine = StartCoroutine(routine);
  131. }
  132. void StopActionCoroutine()
  133. {
  134. if (_actionCoroutine != null)
  135. {
  136. StopCoroutine(_actionCoroutine);
  137. _actionCoroutine = null;
  138. }
  139. }
  140. float _flyAngle = 60;
  141. float _flySpeed = 60;
  142. Vector2 _flyVelocity;
  143. /// <summary>
  144. /// 计算飞行速度
  145. /// </summary>
  146. Vector2 CalculateFlyVelocity()
  147. {
  148. return new Vector2(Mathf.Cos(_flyAngle * Mathf.Deg2Rad), Mathf.Sin(_flyAngle * Mathf.Deg2Rad)) * _flySpeed;
  149. }
  150. /// <summary>
  151. /// 计算折返后的角度
  152. /// </summary>
  153. float CalculateTurnbackAngle()
  154. {
  155. Vector2 dc = _BoundChangeVelocityDirections[_touchBoundType];
  156. Vector3 v = _flyVelocity;
  157. Vector3 v_dc = v * dc;
  158. Vector3 c = Vector3.Cross(v, v_dc);
  159. return _flyAngle + 180 + (c.z > 0 ? -1 : 1) * 90;
  160. }
  161. /// <summary>
  162. /// 飞行动作表现
  163. /// </summary>
  164. IEnumerator Fly()
  165. {
  166. _flyVelocity = CalculateFlyVelocity();
  167. if (_flyVelocity.y > 0) _armature.animation.Play("01", 0);
  168. else _armature.animation.Play("02", 0);
  169. Vector2 ap = _rectTransform.anchoredPosition;
  170. UpdateTouchBoundType();
  171. int firstTouchBoundType = _touchBoundType;
  172. float t = 0;
  173. while (_touchBoundType == firstTouchBoundType || _touchBoundType == 0)
  174. {
  175. t += Time.deltaTime;
  176. _rectTransform.anchoredPosition = ap + _flyVelocity * t;
  177. _direction = _flyVelocity.x > 0 ? 1 : -1;
  178. UpdateAnglesByDirection();
  179. UpdateTouchBoundType();
  180. yield return null;
  181. }
  182. CorrectWithinTheBound();
  183. _touchBoundCount++;
  184. OnEvent(Event_FlyTouchBound);
  185. }
  186. /// <summary>
  187. /// 碰到边界后用它来改变速度方向
  188. /// </summary>
  189. static Vector2[] _BoundChangeVelocityDirections =
  190. {
  191. Vector2.one,
  192. new Vector2(-1, 1),
  193. new Vector2(1, -1),
  194. new Vector2(-1, 1),
  195. new Vector2(1, -1),
  196. };
  197. int _touchBoundCount;
  198. int _touchBoundType;
  199. /// <summary>
  200. /// 更新触碰边界的标志
  201. /// </summary>
  202. void UpdateTouchBoundType()
  203. {
  204. Vector2 ap = _rectTransform.anchoredPosition;
  205. Rect mr = _rectTransform.rect;
  206. Rect pr = _parentRectTransform.rect;
  207. Vector2 mrMin = ap + mr.min;
  208. Vector2 mrMax = ap + mr.max;
  209. if (mrMin.x <= 0) _touchBoundType = 1; //left
  210. else if (mrMin.y <= 0) _touchBoundType = 2; //bottom
  211. else if (mrMax.x >= pr.width) _touchBoundType = 3; //right
  212. else if (mrMax.y >= pr.height) _touchBoundType = 4; //top
  213. else _touchBoundType = 0; //within
  214. }
  215. /// <summary>
  216. /// 把自己纠正回边界内,避免下一次边界检测可能会出问题
  217. /// </summary>
  218. void CorrectWithinTheBound()
  219. {
  220. Vector2 ap = _rectTransform.anchoredPosition;
  221. Rect mr = _rectTransform.rect;
  222. Rect pr = _parentRectTransform.rect;
  223. Vector2 mrMin = ap + mr.min;
  224. Vector2 mrMax = ap + mr.max;
  225. if (mrMin.x <= 0) ap.x = -mr.min.x + 0.1f;
  226. if (mrMin.y <= 0) ap.y = -mr.min.y + 0.1f;
  227. if (mrMax.x >= pr.width) ap.x = pr.width - mr.max.x - 0.1f;
  228. if (mrMax.y >= pr.height) ap.y = pr.height - mr.max.y - 0.1f;
  229. _rectTransform.anchoredPosition = ap;
  230. }
  231. void UpdateAnglesByDirection()
  232. {
  233. var ag = _rectTransform.localEulerAngles;
  234. ag.y = _direction < 0 ? -180 : 0;
  235. _rectTransform.localEulerAngles = ag;
  236. }
  237. IEnumerator HitDead()
  238. {
  239. PlayAnimation("03");
  240. UpdateAnglesByDirection();
  241. onHitDead?.Invoke(this);
  242. yield return new WaitForSeconds(0.1f);
  243. AudioManager.Instance.PlayDuckHit(gameObject);
  244. yield return new WaitForSeconds(0.4f);
  245. OnEvent(Event_HitDead_Finish);
  246. }
  247. IEnumerator FallDown()
  248. {
  249. onFallDonwStart?.Invoke(this);
  250. PlayAnimation("04");
  251. UpdateAnglesByDirection();
  252. Vector2 speed = Vector2.zero;
  253. while (_rectTransform.anchoredPosition.y > 0)
  254. {
  255. speed += Vector2.down * 500f * Time.deltaTime;
  256. _rectTransform.anchoredPosition += speed * Time.deltaTime;
  257. yield return null;
  258. }
  259. OnEvent(Event_FallDown_Finish);
  260. onFallDonwEnd?.Invoke(this);
  261. }
  262. bool _hasFlyAway = false;
  263. public Action onStartFlyAway;
  264. IEnumerator FlyAway()
  265. {
  266. _hasFlyAway = true;
  267. onStartFlyAway?.Invoke();
  268. bool intersect = true;
  269. while (intersect)
  270. {
  271. _rectTransform.anchoredPosition += _flyVelocity * Time.deltaTime;
  272. Rect mr = _rectTransform.rect;
  273. mr.position = _rectTransform.anchoredPosition + mr.min;
  274. Rect pr = _parentRectTransform.rect;
  275. pr.position = Vector2.zero;
  276. intersect = Intersects(mr, pr);
  277. yield return null;
  278. }
  279. OnEvent(Event_FlyAway);
  280. }
  281. bool Intersects(Rect a, Rect b)
  282. {
  283. float maxax = a.x + a.width,
  284. maxay = a.y + a.height,
  285. maxbx = b.x + b.width,
  286. maxby = b.y + b.height;
  287. return !(maxax < b.x || maxbx < a.x || maxay < b.y || maxby < a.y);
  288. }
  289. //活着的时候击中才会返回ture
  290. public bool Hit()
  291. {
  292. if (_hasFlyAway) return false;
  293. if (dead) return false;
  294. dead = true;
  295. StartActionCoroutine(HitDead());
  296. return true;
  297. }
  298. public void PlayDeadForDogHand(int direction)
  299. {
  300. _direction = direction;
  301. UpdateAnglesByDirection();
  302. PlayAnimation("05");
  303. }
  304. public void NotifyFlyAway()
  305. {
  306. if (_hasFlyAway) return;
  307. if (dead) return;
  308. _flyVelocity *= 1000f / _flyVelocity.magnitude;
  309. StartActionCoroutine(FlyAway());
  310. }
  311. }
  312. }