TargetArrow.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. using UnityEngine.EventSystems;
  7. using System;
  8. using DG.Tweening;
  9. public class TargetArrow : MonoBehaviour
  10. {
  11. public RectTransform RootRect;
  12. public RectTransform TipRect;
  13. public RectTransform CanvasRect;
  14. bool endShowTargetArrow = false;
  15. bool isDrag = false;
  16. //being dragged
  17. public void SetTargetArrow(Vector3 InitialPosition, Vector3 TargetPosition)
  18. {
  19. isDrag = true;
  20. this.gameObject.SetActive(true);
  21. GetComponent<RectTransform>().localPosition = InitialPosition;
  22. Vector3 DirectionVector = TargetPosition - InitialPosition;
  23. float shita = 0;
  24. if (Mathf.Abs(DirectionVector.y) != 0)
  25. {
  26. // Specify rotation angle
  27. shita = Mathf.Atan2(DirectionVector.x, DirectionVector.y) * 180 / Mathf.PI;
  28. }
  29. if (Mathf.Abs(DirectionVector.y) < 3)
  30. {
  31. if (DirectionVector.x > 0)
  32. {
  33. shita = 90;
  34. }
  35. else
  36. {
  37. shita = 270;
  38. }
  39. }
  40. shita *= -1;
  41. RootRect.localRotation = Quaternion.Euler(new Vector3(0, 0, shita));
  42. //Get maximum length
  43. float MaxLength = DirectionVector.magnitude;
  44. RootRect.sizeDelta = new Vector2(RootRect.sizeDelta.x, MaxLength);
  45. TipRect.localPosition = new Vector3(0, RootRect.sizeDelta.y - 13, 0);
  46. }
  47. public void OffTargetArrow()
  48. {
  49. this.gameObject.SetActive(false);
  50. }
  51. public Coroutine OnTargetArrow(Vector3 InitialPosition, Vector3 targetPosition, FieldPermanentCard StartFieldUnitCard, FieldPermanentCard EndFieldUnitCard)
  52. {
  53. this.gameObject.SetActive(true);
  54. this.StartFieldUnitCard = StartFieldUnitCard;
  55. this.EndFieldUnitCard = EndFieldUnitCard;
  56. return StartCoroutine(OnTargetArrowCoroutine(InitialPosition, targetPosition));
  57. }
  58. //Temporary target display
  59. public IEnumerator OnTargetArrowCoroutine(Vector3 InitialPosition, Vector3 TargetPosition)
  60. {
  61. ContinuousController.instance.PlaySE(GManager.instance.TargetArrowSE);
  62. isDrag = false;
  63. GetComponent<RectTransform>().localPosition = InitialPosition;
  64. Vector3 DirectionVector = TargetPosition - InitialPosition;
  65. float shita = 0;
  66. if (Mathf.Abs(DirectionVector.y) != 0)
  67. {
  68. //Specify rotation angle
  69. shita = Mathf.Atan2(DirectionVector.x, DirectionVector.y) * 180 / Mathf.PI;
  70. }
  71. if (Mathf.Abs(DirectionVector.y) < 3)
  72. {
  73. if (DirectionVector.x > 0)
  74. {
  75. shita = 90;
  76. }
  77. else
  78. {
  79. shita = 270;
  80. }
  81. }
  82. shita *= -1;
  83. RootRect.localRotation = Quaternion.Euler(new Vector3(0, 0, shita));
  84. //Get maximum length
  85. float MaxLength = DirectionVector.magnitude;
  86. //Time interval to display
  87. float extendTime = 0.17f;
  88. //Number of times to display
  89. int ShowCount = 2;
  90. for (int i = 0; i < ShowCount; i++)
  91. {
  92. RootRect.sizeDelta = new Vector2(RootRect.sizeDelta.x, 0);
  93. TipRect.localPosition = new Vector3(0, RootRect.sizeDelta.y - 13, 0);
  94. bool end = false;
  95. var sequence = DOTween.Sequence();
  96. sequence
  97. .Append(DOTween.To(() => RootRect.sizeDelta, (x) => RootRect.sizeDelta = x, new Vector2(RootRect.sizeDelta.x, MaxLength), extendTime / ShowCount))
  98. .AppendCallback(() => { end = true; sequence.Kill(); });
  99. sequence.Play();
  100. while (!end)
  101. {
  102. TipRect.localPosition = new Vector3(0, RootRect.sizeDelta.y - 13, 0);
  103. yield return null;
  104. }
  105. RootRect.sizeDelta = new Vector2(RootRect.sizeDelta.x, MaxLength);
  106. TipRect.localPosition = new Vector3(0, RootRect.sizeDelta.y - 13, 0);
  107. float _waitTime = 0.07f;
  108. yield return new WaitForSeconds(_waitTime);
  109. }
  110. float waitTime = 0.07f;
  111. yield return new WaitForSeconds(waitTime);
  112. endShowTargetArrow = true;
  113. StartCoroutine(ChangePositionTargetAwwor());
  114. }
  115. FieldPermanentCard StartFieldUnitCard;
  116. FieldPermanentCard EndFieldUnitCard;
  117. public bool Destroyed { get; set; }
  118. IEnumerator ChangePositionTargetAwwor()
  119. {
  120. while (true)
  121. {
  122. if (Destroyed)
  123. {
  124. yield break;
  125. }
  126. yield return new WaitForSeconds(Time.deltaTime);
  127. if (Destroyed)
  128. {
  129. yield break;
  130. }
  131. bool end = true;
  132. if (this != null)
  133. {
  134. if (this.gameObject != null)
  135. {
  136. if (!isDrag)
  137. {
  138. if (endShowTargetArrow)
  139. {
  140. if (StartFieldUnitCard != null && EndFieldUnitCard != null)
  141. {
  142. if (StartFieldUnitCard.ThisPermanent != null)
  143. {
  144. if (StartFieldUnitCard.ThisPermanent.TopCard != null)
  145. {
  146. if (EndFieldUnitCard.ThisPermanent != null)
  147. {
  148. if (EndFieldUnitCard.ThisPermanent.TopCard != null)
  149. {
  150. end = false;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. if (end)
  161. {
  162. yield break;
  163. }
  164. else
  165. {
  166. this.gameObject.SetActive(true);
  167. GetComponent<RectTransform>().localPosition = StartFieldUnitCard.GetLocalCanvasPosition() + StartFieldUnitCard.ThisPermanent.TopCard.Owner.playerUIObjectParent.localPosition;
  168. Vector3 DirectionVector = (EndFieldUnitCard.GetLocalCanvasPosition() + EndFieldUnitCard.ThisPermanent.TopCard.Owner.playerUIObjectParent.localPosition) - (StartFieldUnitCard.GetLocalCanvasPosition() + StartFieldUnitCard.ThisPermanent.TopCard.Owner.playerUIObjectParent.localPosition);
  169. float shita = 0;
  170. if (Mathf.Abs(DirectionVector.y) != 0)
  171. {
  172. //Specify rotation angle
  173. shita = Mathf.Atan2(DirectionVector.x, DirectionVector.y) * 180 / Mathf.PI;
  174. }
  175. if (Mathf.Abs(DirectionVector.y) < 3)
  176. {
  177. if (DirectionVector.x > 0)
  178. {
  179. shita = 90;
  180. }
  181. else
  182. {
  183. shita = 270;
  184. }
  185. }
  186. shita *= -1;
  187. RootRect.localRotation = Quaternion.Euler(new Vector3(0, 0, shita));
  188. //Get maximum length
  189. float MaxLength = DirectionVector.magnitude;
  190. RootRect.sizeDelta = new Vector2(RootRect.sizeDelta.x, MaxLength);
  191. TipRect.localPosition = new Vector3(0, RootRect.sizeDelta.y - 13, 0);
  192. }
  193. }
  194. }
  195. }