using System; using UnityEngine; using Spine.Unity; using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine.Events; using Random = UnityEngine.Random; using Org.BouncyCastle.Pkcs; using HyperspaceGame; public class SpineAnimationLoader : MonoBehaviour { public SkeletonGraphic skeletonAnimation; // 你的SkeletonAnimation组件 public SkeletonDataAsset skeletonDataAsset; public Slider slider; public Image countDownUI; public Image timeUI; public float spawnRangeY; public float spawnRangeX; float duration; public RectTransform rectTransform; public AudioClip brokenClip; public AudioSource source; public GameObject point; UnityAction _destoryCallBack; public Transform scorePoint1; public Transform scorePoint2; public Transform scorePoint3; public Transform scorePoint4; private float radius; private float ScreenWidth; private float ScreenHeight; private bool beShooted = false;//是否已经被打中 private void Start() { ScreenWidth = Screen.width; ScreenHeight = Screen.height; point.SetActive(false); Invoke("CountDown", 1); } public float GetDuration() { return duration; } private float baseScale = 0.8f; public void Init(float size, Order cfg, UnityAction destoryCallBack) { size *= baseScale; Vector3 scale = Vector3.one * size; radius = size * 547 * 0.5f; duration = cfg.ShowTime; slider.maxValue = duration; transform.localScale = scale; _destoryCallBack = destoryCallBack; //音效 if (!UserSettings.ins.openEffect) source.Stop(); } bool isTrue = true; private void Update() { duration -= Time.deltaTime; timeUI.fillAmount = duration / slider.maxValue; if (duration <= 0 && isTrue && !beShooted) { isTrue = false; TiemOver(); } } void CountDown() { countDownUI.gameObject.SetActive(true); } void TiemOver() { beShooted = true;//标记不让再点击了 source.clip = brokenClip; if (UserSettings.ins.openEffect) source.Play(); countDownUI.gameObject.SetActive(false); skeletonAnimation.AnimationState.SetAnimation(1, "animation_02", false); Invoke("Deferred", 0.7f); point.SetActive(false); //GeneratingTarget.gm.scoreSprite.gameObject.SetActive(false); //GeneratingTarget.gm.scoreSprite = null; } void Deferred() { Destroy(gameObject); } public void SetPos(Vector2 randomPos) { transform.localPosition = randomPos; } /// /// 当前靶子半径 /// /// public float Radius() { return radius; } /// /// 随机一个坐标 /// public Vector2 GetRandomPos(PosType posType = PosType.Random, float rateX = 0, float rateY = 0) { float paddingWRate = rateX == 0 ? 0.45f : rateX; float paddingHRate = rateY == 0 ? 0.45f : rateY; var canvasSize = GeneratingTarget.gm.GetCanvasSize(); ScreenWidth = canvasSize.x; ScreenHeight = canvasSize.y; float halfW = ScreenWidth / 2; float halfH = ScreenHeight / 2; float radius = Radius(); var newPos = new Vector2(); switch (posType) { case PosType.Random: newPos.x = Random.Range(-halfW + radius, halfW - radius);//左右 newPos.y = Random.Range(halfH - radius, -halfH + radius);//上下 break; case PosType.Left: newPos.x = Random.Range(-halfW + radius, -halfW + radius + paddingWRate * ScreenWidth); newPos.y = Random.Range(halfH - radius, -halfH + radius); break; case PosType.LeftTop: newPos.x = Random.Range(-halfW + radius, -halfW + radius + paddingWRate * ScreenWidth); newPos.y = Random.Range(halfH - radius, halfH - radius - paddingHRate * ScreenHeight); break; case PosType.LeftDown: newPos.x = Random.Range(-halfW + radius, -halfW + radius + paddingWRate * ScreenWidth); newPos.y = Random.Range(-halfH + radius + paddingHRate * ScreenHeight, -halfH + radius); break; case PosType.Right: newPos.x = Random.Range(halfW - radius - paddingWRate * ScreenWidth, halfW - radius); newPos.y = Random.Range(halfH - radius, -halfH + radius); break; case PosType.RightTop: newPos.x = Random.Range(halfW - radius - paddingWRate * ScreenWidth, halfW - radius); newPos.y = Random.Range(halfH - radius, halfH - radius - paddingHRate * ScreenHeight); break; case PosType.RightDown: newPos.x = Random.Range(halfW - radius - paddingWRate * ScreenWidth, halfW - radius); newPos.y = Random.Range(-halfH + radius + paddingHRate * ScreenHeight, -halfH + radius); break; case PosType.Top: newPos.x = Random.Range(-halfW + radius, halfW - radius); newPos.y = Random.Range(halfH - radius, halfH - radius - paddingHRate * ScreenHeight); break; case PosType.Down: newPos.x = Random.Range(-halfW + radius, halfW - radius); newPos.y = Random.Range(-halfH + radius + paddingHRate * ScreenHeight, -halfH + radius); break; case PosType.Rotate: float tempMax = halfW; if (ScreenWidth > ScreenHeight) tempMax = halfH; tempMax -= radius; newPos.x = Random.Range(-tempMax, tempMax); var maxY = (float)Math.Sqrt(tempMax * tempMax - newPos.x * newPos.x); newPos.y = Random.Range(-maxY, maxY); break; default: break; } return newPos; } private void OnDestroy() { _destoryCallBack?.Invoke(); } /// /// 显示击痕 /// private void ShowShootPoint(Vector3 clickPos) { point.transform.localPosition = clickPos; point.SetActive(true); } /// /// 这是射中靶子计算得分的方法(暂无调用) /// /// public void CalculateScore(Vector3 postion) { if (beShooted) { Debug.LogWarning("重复射击!"); return; } if (GeneratingTarget.gm.Reloading()) { Debug.Log("装弹中"); return; } Debug.Log($"打中的位置 {postion}"); var clickPos = postion; clickPos.z = 0; Debug.Log($"转换成UI位置 {clickPos}"); var canvasSize = GeneratingTarget.gm.GetCanvasSize(); clickPos.x = clickPos.x * (canvasSize.x / (float)Screen.width); clickPos.y = clickPos.y * (canvasSize.y / (float)Screen.height); Vector2 localPoint; var pos = new Vector3(clickPos.x - canvasSize.x * 0.5f, clickPos.y - canvasSize.y * 0.5f, 0); localPoint = new Vector2(pos.x - transform.localPosition.x, pos.y - transform.localPosition.y); localPoint /= transform.localScale; var dis = localPoint.magnitude; // 获取点击位置相对于所在UI元素的坐标 if (RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent(), postion, null, out localPoint)) { if (dis > 216f) return; isTrue = false; int score = 0; //计算得分 if (dis <= 22f) score = 10; else if (dis <= 44f) score = 9; else if (dis <= 66f) score = 8; else if (dis <= 88f) score = 7; else if (dis <= 110f) score = 6; else if (dis <= 130f) score = 5; else if (dis <= 152f) score = 4; else if (dis <= 173f) score = 3; else if (dis <= 195f) score = 2; else if (dis <= 216f) score = 1; //if (true) //{ //打中靶心 beShooted = true; Invoke("TiemOver", 1f); ShowShootPoint(localPoint); //} //else // TiemOver(); if (GeneratingTarget.gm.getAddCountScore) { Debug.Log($"得分 score={score} dis={dis}"); GeneratingTarget.gm.score += score; GeneratingTarget.gm.hitCount++; //统计射箭次数 GeneratingTarget.gm.onShootCount(1); } else { Debug.Log($"不得得分 getAddCountScore{GeneratingTarget.gm.getAddCountScore} score={score} dis={dis}"); } var point = scorePoint1; Vector2 scorePos; int posIdx = 0; if (transform.localPosition.x >= 0f) { if (transform.localPosition.y > 0f) { //右上 point = scorePoint3; scorePos = new Vector2(564f, 20f); posIdx = 3; } else //右下 { point = scorePoint4; scorePos = new Vector2(519f, 85f); posIdx = 4; } } else { if (transform.localPosition.y > 0f)//左上 { point = scorePoint2; scorePos = new Vector2(25f, 21f); posIdx = 2; } else //左下 { point = scorePoint1; scorePos = new Vector2(25f, 21f); posIdx = 1; } } GeneratingTarget.gm.ShowScoreCom(score, point.position, scorePos * transform.localScale, transform, posIdx); } } }