GameDebug.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class GameDebug : MonoBehaviour
  6. {
  7. [SerializeField] InputField speedInput;
  8. [SerializeField] InputField distanceInput;
  9. [SerializeField] Text absoluteAngleText;
  10. [SerializeField] InputField offsetAngleInput;
  11. [SerializeField] Text logText;
  12. bool loaded = false;
  13. public static GameDebug ins;
  14. void Awake()
  15. {
  16. ins = this;
  17. }
  18. void OnDestroy() {
  19. if (ins == this) ins = null;
  20. }
  21. void Start()
  22. {
  23. BowCamera.ins.enabled = false;
  24. speedInput.onEndEdit.AddListener(onSpeedInput);
  25. distanceInput.onEndEdit.AddListener(onDistanceInput);
  26. onSpeedInput(null);
  27. onDistanceInput(null);
  28. loaded = true;
  29. caluculateAbsoluteAngle();
  30. //增加射速卡
  31. }
  32. public void onSpeedInput(string s)
  33. {
  34. float value = 60;
  35. try {
  36. value = float.Parse(speedInput.text);
  37. } catch (System.Exception) {
  38. Log("发射速度解析错误\n已重置为默认值");
  39. }
  40. Arrow.speed = GameMgr.RealSizeToGameSize(value);
  41. caluculateAbsoluteAngle();
  42. }
  43. public void onDistanceInput(string s)
  44. {
  45. float value = 50;
  46. try {
  47. value = float.Parse(distanceInput.text);
  48. } catch (System.Exception) {
  49. Log("靶心距离解析错误\n已重置为默认值");
  50. }
  51. if (value < 10 || value > 70) {
  52. Log("靶心距离超出范围\n超出合法区间");
  53. }
  54. TargetBody target = GameObject.Find("GameArea/TargetObject").GetComponentInChildren<TargetBody>();
  55. target.SetDistance(value);
  56. caluculateAbsoluteAngle();
  57. }
  58. public void caluculateAbsoluteAngle() {
  59. if (!loaded) return;
  60. float mySpeed = Arrow.speed;
  61. if (GameAssistUI.ins) {
  62. mySpeed *= GameAssistUI.ins.shootScaleValue;
  63. }
  64. Vector3 destination = GameObject.Find("GameArea/TargetObject").transform.position;
  65. Vector3 startPoint = BowCamera.ins.transform.position;
  66. float deltaX = Vector2.Distance(
  67. new Vector2(destination.x, destination.z),
  68. new Vector2(startPoint.x, startPoint.z)
  69. );
  70. float deltaY = destination.y - startPoint.y;
  71. float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(mySpeed, 2);
  72. float b = deltaX;
  73. float c = a - deltaY;
  74. bool hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0;
  75. if (hasParabolaAngle) {
  76. float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  77. float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a);
  78. float parabolaAngle = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2)) / Mathf.PI * 180;
  79. absoluteAngleText.text = "绝对角度: " + parabolaAngle + "°";
  80. } else {
  81. absoluteAngleText.text = "绝对角度: " + "无解";
  82. }
  83. }
  84. private void Log(string text) {
  85. logText.text = text;
  86. }
  87. public float GetOffsetAngle() {
  88. float angle = 0;
  89. try {
  90. angle = float.Parse(offsetAngleInput.text);
  91. } catch (System.Exception) {
  92. Log("偏移角度解析错误");
  93. }
  94. return angle;
  95. }
  96. public void ShowRes(Vector3 basePoint, Vector3 hitPoint) {
  97. string text =
  98. "准心瞄点 " + V3ToString(basePoint) + "\n" +
  99. "实际落点 " + V3ToString(hitPoint) + "\n" +
  100. "偏移距离 " + (hitPoint - basePoint).magnitude.ToString("#0.0000") + "m";
  101. Log(text);
  102. }
  103. public string V3ToString(Vector3 v) {
  104. return "(" + v.x.ToString("#0.00") + ", " + v.y.ToString("#0.00") + ", " + v.z.ToString("#0.00") + ")";
  105. }
  106. public void deleteAllArrows() {
  107. Arrow[] arrows = GameObject.FindObjectsOfType<Arrow>();
  108. foreach (var arrow in arrows)
  109. {
  110. if (!arrow.hasDoneNextShoot) continue;
  111. try {
  112. GameObject.Destroy(arrow.gameObject);
  113. } catch (UnityException e) {
  114. Debug.Log("Delete Arrow Error\n" + e.Message);
  115. }
  116. }
  117. }
  118. }