SpineAnimationLoader.cs 10 KB

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