SpineAnimationLoader.cs 10 KB

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