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