TargetBodyNew.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. /* 组件-射击目标-靶子 */
  3. public class TargetBodyNew : MonoBehaviour
  4. {
  5. float distance = 10f;
  6. public int playerIndex;
  7. public static TargetBodyNew ins;
  8. void Start()
  9. {
  10. ins = this;
  11. SetDistance(distance);
  12. }
  13. void OnDestroy()
  14. {
  15. if (ins == this) ins = null;
  16. }
  17. public void Hit(ArrowNew2 arrow, Vector3 hitPosition, int attackerPlayerIndex)
  18. {
  19. transform.Find("PointSignLastHit")?.GetComponent<PointSignLastHitNew>()?.Show(hitPosition);
  20. arrow.Hit();
  21. AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(gameObject));
  22. bool hitTarget = false;
  23. float maxSize = Vector3.Distance(transform.Find("CenterPoint").position, transform.Find("SidePoint").position);
  24. float radius = measureRadius(hitPosition);
  25. float score = Mathf.Clamp(10f - radius / maxSize * 10f, 0, 9.9f) + 1;
  26. if (score >= 1)
  27. {
  28. score = (float)System.Math.Round(score, CommonConfig.ringsPrecision);
  29. if (playerIndex != attackerPlayerIndex) score = 0;
  30. GameController.ins.HitTarget(score, attackerPlayerIndex);
  31. //AudioMgr.ins.PlayCheer(true);
  32. hitTarget = true;
  33. }
  34. if (!hitTarget) GameController.ins.HitTarget(0);
  35. arrow.hitType = hitTarget ? ArrowNew2.HitType.TargetInRing : ArrowNew2.HitType.TargetOutRing;
  36. }
  37. float measureRadius(Vector3 position)
  38. {
  39. return Vector3.Distance(transform.Find("CenterPoint").position, position);
  40. }
  41. public void SetDistance(float value)
  42. {
  43. distance = value;
  44. Vector3 v3 = transform.parent.localPosition;
  45. v3.z = GameMgr.RealSizeToGameSize(value);
  46. transform.parent.localPosition = v3;
  47. }
  48. public float GetDistance()
  49. {
  50. return distance;
  51. }
  52. }