SpineAnimationLoader.cs 10 KB

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