GameDebug.cs 4.4 KB

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