Duck.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. //不是射箭状态不处理箭
  63. if (GlobalData.MyDeviceMode != DeviceMode.Archery) return;
  64. if (_sequenceRotateForever != null)
  65. {
  66. _sequenceRotateForever.Kill();
  67. _sequenceRotateForever = null;
  68. }
  69. if (animationName == "03")
  70. {
  71. _arrowRectTransform.gameObject.SetActive(true);
  72. _arrowRectTransform.localPosition = new Vector3(-20, -55);
  73. _arrowRectTransform.localEulerAngles = new Vector3(0, 0, 145);
  74. }
  75. else if (animationName == "04")
  76. {
  77. _arrowRectTransform.gameObject.SetActive(true);
  78. _arrowRectTransform.localPosition = new Vector3(0, 35);
  79. _arrowRectTransform.localEulerAngles = new Vector3(0, 0, 0);
  80. _sequenceRotateForever = DOTween.Sequence();
  81. _sequenceRotateForever.Append(_arrowRectTransform.DOLocalRotate(Vector3.up * 180, 0.2f));
  82. _sequenceRotateForever.Append(_arrowRectTransform.DOLocalRotate(Vector3.up * 360, 0.2f));
  83. _sequenceRotateForever.SetLoops(-1);
  84. }
  85. else if (animationName == "05")
  86. {
  87. _arrowRectTransform.gameObject.SetActive(true);
  88. _arrowRectTransform.localPosition = new Vector3(34, -77);
  89. _arrowRectTransform.localEulerAngles = new Vector3(0, 0, 232);
  90. }
  91. else
  92. {
  93. _arrowRectTransform.gameObject.SetActive(false);
  94. };
  95. }
  96. static string Event_HitDead_Finish = "Event_HitDead_Finish";
  97. static string Event_FallDown_Finish = "Event_FallDown_Finish";
  98. static string Event_FlyTouchBound = "Event_FlyTouchBound";
  99. static string Event_FlyAway = "Event_FlyAway";
  100. void OnEvent(string eventName)
  101. {
  102. if (eventName == Event_HitDead_Finish)
  103. {
  104. StartActionCoroutine(FallDown());
  105. }
  106. else if (eventName == Event_FallDown_Finish)
  107. {
  108. onExit?.Invoke(this);
  109. }
  110. else if (eventName == Event_FlyTouchBound)
  111. {
  112. if (_touchBoundCount >= config.touchBoundCount)
  113. {
  114. StartActionCoroutine(FlyAway());
  115. }
  116. else
  117. {
  118. _flyAngle = CalculateTurnbackAngle();
  119. StartActionCoroutine(Fly());
  120. }
  121. }
  122. else if (eventName == Event_FlyAway)
  123. {
  124. Destroy(gameObject);
  125. onExit?.Invoke(this);
  126. }
  127. }
  128. Coroutine _actionCoroutine = null;
  129. void StartActionCoroutine(IEnumerator routine)
  130. {
  131. StopActionCoroutine();
  132. _actionCoroutine = StartCoroutine(routine);
  133. }
  134. void StopActionCoroutine()
  135. {
  136. if (_actionCoroutine != null)
  137. {
  138. StopCoroutine(_actionCoroutine);
  139. _actionCoroutine = null;
  140. }
  141. }
  142. float _flyAngle = 60;
  143. float _flySpeed = 60;
  144. Vector2 _flyVelocity;
  145. /// <summary>
  146. /// 计算飞行速度
  147. /// </summary>
  148. Vector2 CalculateFlyVelocity()
  149. {
  150. return new Vector2(Mathf.Cos(_flyAngle * Mathf.Deg2Rad), Mathf.Sin(_flyAngle * Mathf.Deg2Rad)) * _flySpeed;
  151. }
  152. /// <summary>
  153. /// 计算折返后的角度
  154. /// </summary>
  155. float CalculateTurnbackAngle()
  156. {
  157. Vector2 dc = _BoundChangeVelocityDirections[_touchBoundType];
  158. Vector3 v = _flyVelocity;
  159. Vector3 v_dc = v * dc;
  160. Vector3 c = Vector3.Cross(v, v_dc);
  161. return _flyAngle + 180 + (c.z > 0 ? -1 : 1) * 90;
  162. }
  163. /// <summary>
  164. /// 飞行动作表现
  165. /// </summary>
  166. IEnumerator Fly()
  167. {
  168. _flyVelocity = CalculateFlyVelocity();
  169. if (_flyVelocity.y > 0) _armature.animation.Play("01", 0);
  170. else _armature.animation.Play("02", 0);
  171. Vector2 ap = _rectTransform.anchoredPosition;
  172. UpdateTouchBoundType();
  173. int firstTouchBoundType = _touchBoundType;
  174. float t = 0;
  175. while (_touchBoundType == firstTouchBoundType || _touchBoundType == 0)
  176. {
  177. t += Time.deltaTime;
  178. _rectTransform.anchoredPosition = ap + _flyVelocity * t;
  179. _direction = _flyVelocity.x > 0 ? 1 : -1;
  180. UpdateAnglesByDirection();
  181. UpdateTouchBoundType();
  182. yield return null;
  183. }
  184. CorrectWithinTheBound();
  185. _touchBoundCount++;
  186. OnEvent(Event_FlyTouchBound);
  187. }
  188. /// <summary>
  189. /// 碰到边界后用它来改变速度方向
  190. /// </summary>
  191. static Vector2[] _BoundChangeVelocityDirections =
  192. {
  193. Vector2.one,
  194. new Vector2(-1, 1),
  195. new Vector2(1, -1),
  196. new Vector2(-1, 1),
  197. new Vector2(1, -1),
  198. };
  199. int _touchBoundCount;
  200. int _touchBoundType;
  201. /// <summary>
  202. /// 更新触碰边界的标志
  203. /// </summary>
  204. void UpdateTouchBoundType()
  205. {
  206. Vector2 ap = _rectTransform.anchoredPosition;
  207. Rect mr = _rectTransform.rect;
  208. Rect pr = _parentRectTransform.rect;
  209. Vector2 mrMin = ap + mr.min;
  210. Vector2 mrMax = ap + mr.max;
  211. bool hitBottom = false;
  212. Collider2D[] hitColliders = Physics2D.OverlapPointAll(transform.position);
  213. foreach (var item in hitColliders)
  214. {
  215. if (item.name.StartsWith("BottomCollider"))
  216. {
  217. hitBottom = true;
  218. break;
  219. }
  220. }
  221. if (hitBottom) _touchBoundType = 2;
  222. else if (mrMin.x <= 0) _touchBoundType = 1; //left
  223. else if (mrMin.y <= 0) _touchBoundType = 2; //bottom
  224. else if (mrMax.x >= pr.width) _touchBoundType = 3; //right
  225. else if (mrMax.y >= pr.height) _touchBoundType = 4; //top
  226. else _touchBoundType = 0; //within
  227. }
  228. /// <summary>
  229. /// 把自己纠正回边界内,避免下一次边界检测可能会出问题
  230. /// </summary>
  231. void CorrectWithinTheBound()
  232. {
  233. Vector2 ap = _rectTransform.anchoredPosition;
  234. Rect mr = _rectTransform.rect;
  235. Rect pr = _parentRectTransform.rect;
  236. Vector2 mrMin = ap + mr.min;
  237. Vector2 mrMax = ap + mr.max;
  238. if (mrMin.x <= 0) ap.x = -mr.min.x + 0.1f;
  239. if (mrMin.y <= 0) ap.y = -mr.min.y + 0.1f;
  240. if (mrMax.x >= pr.width) ap.x = pr.width - mr.max.x - 0.1f;
  241. if (mrMax.y >= pr.height) ap.y = pr.height - mr.max.y - 0.1f;
  242. _rectTransform.anchoredPosition = ap;
  243. }
  244. void UpdateAnglesByDirection()
  245. {
  246. var ag = _rectTransform.localEulerAngles;
  247. ag.y = _direction < 0 ? -180 : 0;
  248. _rectTransform.localEulerAngles = ag;
  249. }
  250. IEnumerator HitDead()
  251. {
  252. PlayAnimation("03");
  253. UpdateAnglesByDirection();
  254. onHitDead?.Invoke(this);
  255. yield return new WaitForSeconds(0.1f);
  256. AudioManager.Instance.PlayDuckHit(gameObject);
  257. yield return new WaitForSeconds(0.4f);
  258. OnEvent(Event_HitDead_Finish);
  259. }
  260. IEnumerator FallDown()
  261. {
  262. onFallDonwStart?.Invoke(this);
  263. PlayAnimation("04");
  264. UpdateAnglesByDirection();
  265. Vector2 speed = Vector2.zero;
  266. while (_rectTransform.anchoredPosition.y > 0)
  267. {
  268. speed += Vector2.down * 500f * Time.deltaTime;
  269. _rectTransform.anchoredPosition += speed * Time.deltaTime;
  270. yield return null;
  271. }
  272. OnEvent(Event_FallDown_Finish);
  273. onFallDonwEnd?.Invoke(this);
  274. }
  275. bool _hasFlyAway = false;
  276. public Action onStartFlyAway;
  277. IEnumerator FlyAway()
  278. {
  279. _hasFlyAway = true;
  280. onStartFlyAway?.Invoke();
  281. bool intersect = true;
  282. while (intersect)
  283. {
  284. _rectTransform.anchoredPosition += _flyVelocity * Time.deltaTime;
  285. Rect mr = _rectTransform.rect;
  286. mr.position = _rectTransform.anchoredPosition + mr.min;
  287. Rect pr = _parentRectTransform.rect;
  288. pr.position = Vector2.zero;
  289. intersect = Intersects(mr, pr);
  290. yield return null;
  291. }
  292. OnEvent(Event_FlyAway);
  293. }
  294. bool Intersects(Rect a, Rect b)
  295. {
  296. float maxax = a.x + a.width,
  297. maxay = a.y + a.height,
  298. maxbx = b.x + b.width,
  299. maxby = b.y + b.height;
  300. return !(maxax < b.x || maxbx < a.x || maxay < b.y || maxby < a.y);
  301. }
  302. //活着的时候击中才会返回ture
  303. public bool Hit()
  304. {
  305. if (_hasFlyAway) return false;
  306. if (dead) return false;
  307. dead = true;
  308. StartActionCoroutine(HitDead());
  309. return true;
  310. }
  311. public void PlayDeadForDogHand(int direction)
  312. {
  313. _direction = direction;
  314. UpdateAnglesByDirection();
  315. PlayAnimation("05");
  316. }
  317. public void NotifyFlyAway()
  318. {
  319. if (_hasFlyAway) return;
  320. if (dead) return;
  321. _flyVelocity *= 1000f / _flyVelocity.magnitude;
  322. StartActionCoroutine(FlyAway());
  323. }
  324. }
  325. }