| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using System;
- using UnityEngine;
- using Spine.Unity;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using UnityEngine.Events;
- using Random = UnityEngine.Random;
- 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;
- }
- /// <summary>
- /// 当前靶子半径
- /// </summary>
- /// <returns></returns>
- public float Radius()
- {
- return radius;
- }
- /// <summary>
- /// 随机一个坐标
- /// </summary>
- 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();
- }
- /// <summary>
- /// 显示击痕
- /// </summary>
- private void ShowShootPoint(Vector3 clickPos)
- {
- point.transform.localPosition = clickPos;
- point.SetActive(true);
- }
- /// <summary>
- /// 这是射中靶子计算得分的方法(暂无调用)
- /// </summary>
- /// <param name="postion"></param>
- 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<RectTransform>(), 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);
- }
- }
- }
|