using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameDebug : MonoBehaviour { [SerializeField] InputField speedInput; [SerializeField] InputField distanceInput; [SerializeField] Text absoluteAngleText; [SerializeField] InputField offsetAngleInput; [SerializeField] Text logText; bool loaded = false; public static GameDebug ins; void Awake() { ins = this; } void OnDestroy() { if (ins == this) ins = null; } void Start() { BowCamera.ins.enabled = false; speedInput.onEndEdit.AddListener(onSpeedInput); distanceInput.onEndEdit.AddListener(onDistanceInput); onSpeedInput(null); onDistanceInput(null); loaded = true; caluculateAbsoluteAngle(); //增加射速卡 PropInfo propInfo = new PropInfo(); propInfo.id = 22; propInfo.count = 1; propInfo.inuse = true; LoginMgr.myUserInfo.bagList.Add(propInfo); } public void onSpeedInput(string s) { float value = 60; try { value = float.Parse(speedInput.text); } catch (System.Exception) { Log("发射速度解析错误\n已重置为默认值"); } Arrow.speed = GameMgr.RealSizeToGameSize(value); caluculateAbsoluteAngle(); } public void onDistanceInput(string s) { float value = 50; try { value = float.Parse(distanceInput.text); } catch (System.Exception) { Log("靶心距离解析错误\n已重置为默认值"); } if (value < 10 || value > 70) { Log("靶心距离超出范围\n超出合法区间"); } TargetBody target = GameObject.Find("GameArea/TargetObject").GetComponentInChildren(); target.SetDistance(value); caluculateAbsoluteAngle(); } public void caluculateAbsoluteAngle() { if (!loaded) return; float mySpeed = Arrow.speed; if (GameAssistUI.ins) { mySpeed *= GameAssistUI.ins.shootScaleValue; } Vector3 destination = GameObject.Find("GameArea/TargetObject").transform.position; Vector3 startPoint = BowCamera.ins.transform.position; float deltaX = Vector2.Distance( new Vector2(destination.x, destination.z), new Vector2(startPoint.x, startPoint.z) ); float deltaY = destination.y - startPoint.y; float a = 0.5f * Physics.gravity.y * Mathf.Pow(deltaX, 2) / Mathf.Pow(mySpeed, 2); float b = deltaX; float c = a - deltaY; bool hasParabolaAngle = Mathf.Pow(b, 2) - 4 * a * c >= 0; if (hasParabolaAngle) { float res1 = (-b + Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a); float res2 = (-b - Mathf.Pow(Mathf.Pow(b, 2) - 4*a*c, 0.5f)) / (2 * a); float parabolaAngle = Mathf.Min(Mathf.Atan(res1), Mathf.Atan(res2)) / Mathf.PI * 180; absoluteAngleText.text = "绝对角度: " + parabolaAngle + "°"; } else { absoluteAngleText.text = "绝对角度: " + "无解"; } } private void Log(string text) { logText.text = text; } public float GetOffsetAngle() { float angle = 0; try { angle = float.Parse(offsetAngleInput.text); } catch (System.Exception) { Log("偏移角度解析错误"); } return angle; } public void ShowRes(Vector3 basePoint, Vector3 hitPoint) { string text = "准心瞄点 " + V3ToString(basePoint) + "\n" + "实际落点 " + V3ToString(hitPoint) + "\n" + "偏移距离 " + (hitPoint - basePoint).magnitude.ToString("#0.0000") + "m"; Log(text); } public string V3ToString(Vector3 v) { return "(" + v.x.ToString("#0.00") + ", " + v.y.ToString("#0.00") + ", " + v.z.ToString("#0.00") + ")"; } public void deleteAllArrows() { Arrow[] arrows = GameObject.FindObjectsOfType(); foreach (var arrow in arrows) { if (!arrow.hasDoneNextShoot) continue; try { GameObject.Destroy(arrow.gameObject); } catch (UnityException e) { Debug.Log("Delete Arrow Error\n" + e.Message); } } } }