GameDebug.cs 4.3 KB

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