Duck.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. bool hitBottom = false;
  210. Collider2D[] hitColliders = Physics2D.OverlapPointAll(transform.position);
  211. foreach (var item in hitColliders)
  212. {
  213. if (item.name.StartsWith("BottomCollider"))
  214. {
  215. hitBottom = true;
  216. break;
  217. }
  218. }
  219. if (hitBottom) _touchBoundType = 2;
  220. else if (mrMin.x <= 0) _touchBoundType = 1; //left
  221. else if (mrMin.y <= 0) _touchBoundType = 2; //bottom
  222. else if (mrMax.x >= pr.width) _touchBoundType = 3; //right
  223. else if (mrMax.y >= pr.height) _touchBoundType = 4; //top
  224. else _touchBoundType = 0; //within
  225. }
  226. /// <summary>
  227. /// 把自己纠正回边界内,避免下一次边界检测可能会出问题
  228. /// </summary>
  229. void CorrectWithinTheBound()
  230. {
  231. Vector2 ap = _rectTransform.anchoredPosition;
  232. Rect mr = _rectTransform.rect;
  233. Rect pr = _parentRectTransform.rect;
  234. Vector2 mrMin = ap + mr.min;
  235. Vector2 mrMax = ap + mr.max;
  236. if (mrMin.x <= 0) ap.x = -mr.min.x + 0.1f;
  237. if (mrMin.y <= 0) ap.y = -mr.min.y + 0.1f;
  238. if (mrMax.x >= pr.width) ap.x = pr.width - mr.max.x - 0.1f;
  239. if (mrMax.y >= pr.height) ap.y = pr.height - mr.max.y - 0.1f;
  240. _rectTransform.anchoredPosition = ap;
  241. }
  242. void UpdateAnglesByDirection()
  243. {
  244. var ag = _rectTransform.localEulerAngles;
  245. ag.y = _direction < 0 ? -180 : 0;
  246. _rectTransform.localEulerAngles = ag;
  247. }
  248. IEnumerator HitDead()
  249. {
  250. PlayAnimation("03");
  251. UpdateAnglesByDirection();
  252. onHitDead?.Invoke(this);
  253. yield return new WaitForSeconds(0.1f);
  254. AudioManager.Instance.PlayDuckHit(gameObject);
  255. yield return new WaitForSeconds(0.4f);
  256. OnEvent(Event_HitDead_Finish);
  257. }
  258. IEnumerator FallDown()
  259. {
  260. onFallDonwStart?.Invoke(this);
  261. PlayAnimation("04");
  262. UpdateAnglesByDirection();
  263. Vector2 speed = Vector2.zero;
  264. while (_rectTransform.anchoredPosition.y > 0)
  265. {
  266. speed += Vector2.down * 500f * Time.deltaTime;
  267. _rectTransform.anchoredPosition += speed * Time.deltaTime;
  268. yield return null;
  269. }
  270. OnEvent(Event_FallDown_Finish);
  271. onFallDonwEnd?.Invoke(this);
  272. }
  273. bool _hasFlyAway = false;
  274. public Action onStartFlyAway;
  275. IEnumerator FlyAway()
  276. {
  277. _hasFlyAway = true;
  278. onStartFlyAway?.Invoke();
  279. bool intersect = true;
  280. while (intersect)
  281. {
  282. _rectTransform.anchoredPosition += _flyVelocity * Time.deltaTime;
  283. Rect mr = _rectTransform.rect;
  284. mr.position = _rectTransform.anchoredPosition + mr.min;
  285. Rect pr = _parentRectTransform.rect;
  286. pr.position = Vector2.zero;
  287. intersect = Intersects(mr, pr);
  288. yield return null;
  289. }
  290. OnEvent(Event_FlyAway);
  291. }
  292. bool Intersects(Rect a, Rect b)
  293. {
  294. float maxax = a.x + a.width,
  295. maxay = a.y + a.height,
  296. maxbx = b.x + b.width,
  297. maxby = b.y + b.height;
  298. return !(maxax < b.x || maxbx < a.x || maxay < b.y || maxby < a.y);
  299. }
  300. //活着的时候击中才会返回ture
  301. public bool Hit()
  302. {
  303. if (_hasFlyAway) return false;
  304. if (dead) return false;
  305. dead = true;
  306. StartActionCoroutine(HitDead());
  307. return true;
  308. }
  309. public void PlayDeadForDogHand(int direction)
  310. {
  311. _direction = direction;
  312. UpdateAnglesByDirection();
  313. PlayAnimation("05");
  314. }
  315. public void NotifyFlyAway()
  316. {
  317. if (_hasFlyAway) return;
  318. if (dead) return;
  319. _flyVelocity *= 1000f / _flyVelocity.magnitude;
  320. StartActionCoroutine(FlyAway());
  321. }
  322. }
  323. }