SpineAnimationLoader.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using System;
  2. using UnityEngine;
  3. using Spine.Unity;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.Events;
  7. using Random = UnityEngine.Random;
  8. using Org.BouncyCastle.Pkcs;
  9. using HyperspaceGame;
  10. public class SpineAnimationLoader : MonoBehaviour
  11. {
  12. public SkeletonGraphic skeletonAnimation; // 你的SkeletonAnimation组件
  13. public SkeletonDataAsset skeletonDataAsset;
  14. public Slider slider;
  15. public Image countDownUI;
  16. public Image timeUI;
  17. public float spawnRangeY;
  18. public float spawnRangeX;
  19. float duration;
  20. public RectTransform rectTransform;
  21. public AudioClip brokenClip;
  22. public AudioSource source;
  23. public GameObject point;
  24. UnityAction _destoryCallBack;
  25. public Transform scorePoint1;
  26. public Transform scorePoint2;
  27. public Transform scorePoint3;
  28. public Transform scorePoint4;
  29. private float radius;
  30. private float ScreenWidth;
  31. private float ScreenHeight;
  32. private bool beShooted = false;//是否已经被打中
  33. private void Start()
  34. {
  35. ScreenWidth = Screen.width;
  36. ScreenHeight = Screen.height;
  37. point.SetActive(false);
  38. Invoke("CountDown", 1);
  39. }
  40. public float GetDuration()
  41. {
  42. return duration;
  43. }
  44. private float baseScale = 0.8f;
  45. public void Init(float size, Order cfg, UnityAction destoryCallBack)
  46. {
  47. size *= baseScale;
  48. Vector3 scale = Vector3.one * size;
  49. radius = size * 547 * 0.5f;
  50. duration = cfg.ShowTime;
  51. slider.maxValue = duration;
  52. transform.localScale = scale;
  53. _destoryCallBack = destoryCallBack;
  54. }
  55. bool isTrue = true;
  56. private void Update()
  57. {
  58. duration -= Time.deltaTime;
  59. timeUI.fillAmount = duration / slider.maxValue;
  60. if (duration <= 0 && isTrue && !beShooted)
  61. {
  62. isTrue = false;
  63. TiemOver();
  64. }
  65. }
  66. void CountDown()
  67. {
  68. countDownUI.gameObject.SetActive(true);
  69. }
  70. void TiemOver()
  71. {
  72. beShooted = true;//标记不让再点击了
  73. source.clip = brokenClip;
  74. source.Play();
  75. countDownUI.gameObject.SetActive(false);
  76. skeletonAnimation.AnimationState.SetAnimation(1, "animation_02", false);
  77. Invoke("Deferred", 0.7f);
  78. point.SetActive(false);
  79. //GeneratingTarget.gm.scoreSprite.gameObject.SetActive(false);
  80. //GeneratingTarget.gm.scoreSprite = null;
  81. }
  82. void Deferred()
  83. {
  84. Destroy(gameObject);
  85. }
  86. public void SetPos(Vector2 randomPos)
  87. {
  88. transform.localPosition = randomPos;
  89. }
  90. /// <summary>
  91. /// 当前靶子半径
  92. /// </summary>
  93. /// <returns></returns>
  94. public float Radius()
  95. {
  96. return radius;
  97. }
  98. /// <summary>
  99. /// 随机一个坐标
  100. /// </summary>
  101. public Vector2 GetRandomPos(PosType posType = PosType.Random, float rate = 0)
  102. {
  103. float paddingWRate = rate == 0 ? 0.45f : rate;
  104. float paddingHRate = rate == 0 ? 0.45f : rate;
  105. var canvasSize = GeneratingTarget.gm.GetCanvasSize();
  106. ScreenWidth = canvasSize.x;
  107. ScreenHeight = canvasSize.y;
  108. float halfW = ScreenWidth / 2;
  109. float halfH = ScreenHeight / 2;
  110. float radius = Radius();
  111. var newPos = new Vector2();
  112. switch (posType)
  113. {
  114. case PosType.Random:
  115. newPos.x = Random.Range(-halfW + radius, halfW - radius);//左右
  116. newPos.y = Random.Range(halfH - radius, -halfH + radius);//上下
  117. break;
  118. case PosType.Left:
  119. newPos.x = Random.Range(-halfW + radius, -halfW + radius + paddingWRate * ScreenWidth);
  120. newPos.y = Random.Range(halfH - radius, -halfH + radius);
  121. break;
  122. case PosType.LeftTop:
  123. newPos.x = Random.Range(-halfW + radius, -halfW + radius + paddingWRate * ScreenWidth);
  124. newPos.y = Random.Range(halfH - radius, halfH - radius - paddingHRate * ScreenHeight);
  125. break;
  126. case PosType.LeftDown:
  127. newPos.x = Random.Range(-halfW + radius, -halfW + radius + paddingWRate * ScreenWidth);
  128. newPos.y = Random.Range(-halfH + radius + paddingHRate * ScreenHeight, -halfH + radius);
  129. break;
  130. case PosType.Right:
  131. newPos.x = Random.Range(halfW - radius - paddingWRate * ScreenWidth, halfW - radius);
  132. newPos.y = Random.Range(halfH - radius, -halfH + radius);
  133. break;
  134. case PosType.RightTop:
  135. newPos.x = Random.Range(halfW - radius - paddingWRate * ScreenWidth, halfW - radius);
  136. newPos.y = Random.Range(halfH - radius, halfH - radius - paddingHRate * ScreenHeight);
  137. break;
  138. case PosType.RightDown:
  139. newPos.x = Random.Range(halfW - radius - paddingWRate * ScreenWidth, halfW - radius);
  140. newPos.y = Random.Range(-halfH + radius + paddingHRate * ScreenHeight, -halfH + radius);
  141. break;
  142. case PosType.Top:
  143. newPos.x = Random.Range(-halfW + radius, halfW - radius);
  144. newPos.y = Random.Range(halfH - radius, halfH - radius - paddingHRate * ScreenHeight);
  145. break;
  146. case PosType.Down:
  147. newPos.x = Random.Range(-halfW + radius, halfW - radius);
  148. newPos.y = Random.Range(-halfH + radius + paddingHRate * ScreenHeight, -halfH + radius);
  149. break;
  150. case PosType.Rotate:
  151. float tempMax = halfW;
  152. if (ScreenWidth > ScreenHeight)
  153. tempMax = halfH;
  154. tempMax -= radius;
  155. newPos.x = Random.Range(-tempMax, tempMax);
  156. var maxY = (float)Math.Sqrt(tempMax * tempMax - newPos.x * newPos.x);
  157. newPos.y = Random.Range(-maxY, maxY);
  158. break;
  159. default:
  160. break;
  161. }
  162. return newPos;
  163. }
  164. private void OnDestroy()
  165. {
  166. _destoryCallBack?.Invoke();
  167. }
  168. /// <summary>
  169. /// 显示击痕
  170. /// </summary>
  171. private void ShowShootPoint(Vector3 clickPos)
  172. {
  173. point.transform.localPosition = clickPos;
  174. point.SetActive(true);
  175. }
  176. /// <summary>
  177. /// 这是射中靶子计算得分的方法(暂无调用)
  178. /// </summary>
  179. /// <param name="postion"></param>
  180. public void CalculateScore(Vector3 postion)
  181. {
  182. if (beShooted)
  183. {
  184. Debug.LogWarning("重复射击!");
  185. return;
  186. }
  187. if (GeneratingTarget.gm.Reloading())
  188. {
  189. Debug.Log("装弹中");
  190. return;
  191. }
  192. Debug.Log($"打中的位置 {postion}");
  193. var clickPos = postion;
  194. clickPos.z = 0;
  195. Debug.Log($"转换成UI位置 {clickPos}");
  196. var canvasSize = GeneratingTarget.gm.GetCanvasSize();
  197. clickPos.x = clickPos.x * (canvasSize.x / (float)Screen.width);
  198. clickPos.y = clickPos.y * (canvasSize.y / (float)Screen.height);
  199. Vector2 localPoint;
  200. var pos = new Vector3(clickPos.x - canvasSize.x * 0.5f, clickPos.y - canvasSize.y * 0.5f, 0);
  201. localPoint = new Vector2(pos.x - transform.localPosition.x, pos.y - transform.localPosition.y);
  202. localPoint /= transform.localScale;
  203. var dis = localPoint.magnitude;
  204. // 获取点击位置相对于所在UI元素的坐标
  205. if (RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RectTransform>(), postion, null, out localPoint))
  206. {
  207. if (dis > 216f)
  208. return;
  209. isTrue = false;
  210. int score = 0;
  211. //计算得分
  212. if (dis <= 22f)
  213. score = 10;
  214. else if (dis <= 44f)
  215. score = 9;
  216. else if (dis <= 66f)
  217. score = 8;
  218. else if (dis <= 88f)
  219. score = 7;
  220. else if (dis <= 110f)
  221. score = 6;
  222. else if (dis <= 130f)
  223. score = 5;
  224. else if (dis <= 152f)
  225. score = 4;
  226. else if (dis <= 173f)
  227. score = 3;
  228. else if (dis <= 195f)
  229. score = 2;
  230. else if (dis <= 216f)
  231. score = 1;
  232. //if (true)
  233. //{
  234. //打中靶心
  235. beShooted = true;
  236. Invoke("TiemOver", 1f);
  237. ShowShootPoint(localPoint);
  238. //}
  239. //else
  240. // TiemOver();
  241. if (GeneratingTarget.gm.getAddCountScore)
  242. {
  243. Debug.Log($"得分 score={score} dis={dis}");
  244. GeneratingTarget.gm.score += score;
  245. GeneratingTarget.gm.hitCount++;
  246. //统计射箭次数
  247. GeneratingTarget.gm.onShootCount(1);
  248. }
  249. else {
  250. Debug.Log($"不得得分 getAddCountScore{GeneratingTarget.gm.getAddCountScore} score={score} dis={dis}");
  251. }
  252. var point = scorePoint1;
  253. Vector2 scorePos;
  254. int posIdx = 0;
  255. if (transform.localPosition.x >= 0f)
  256. {
  257. if (transform.localPosition.y > 0f)
  258. {
  259. //右上
  260. point = scorePoint3;
  261. scorePos = new Vector2(564f, 20f);
  262. posIdx = 3;
  263. }
  264. else
  265. //右下
  266. {
  267. point = scorePoint4;
  268. scorePos = new Vector2(519f, 85f);
  269. posIdx = 4;
  270. }
  271. }
  272. else
  273. {
  274. if (transform.localPosition.y > 0f)//左上
  275. {
  276. point = scorePoint2;
  277. scorePos = new Vector2(25f, 21f);
  278. posIdx = 2;
  279. }
  280. else
  281. //左下
  282. {
  283. point = scorePoint1;
  284. scorePos = new Vector2(25f, 21f);
  285. posIdx = 1;
  286. }
  287. }
  288. GeneratingTarget.gm.ShowScoreCom(score, point.position, scorePos * transform.localScale, transform, posIdx);
  289. }
  290. }
  291. }