using System; using UnityEngine; using UnityEngine.UI; using ArduinoBluetoothAPI; using BestHTTP.WebSocket; public class ShootCheck : MonoBehaviour { [SerializeField] Text text; CMD cmd = new CMD(); bool locked = false; float maxAcc = 0; public float shootSpeed; public static ShootCheck ins; void Start() { ins = this; BluetoothDispatcher.shoot = OnDataReceived; } public void OnBluetoothReady(BluetoothShoot bluetoothShoot) { bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", "")); } public void OnDataReceived(byte[] bytes) { if (bytes.Length == 2) { DeviceBatteryView.ins.RenderBattery(2, bytes[0]); return; } for (int i = 0; i < (bytes.Length-2)/6; i++) { float acc = ToAcceleratedSpeed(bytes[i * 6 + 5], bytes[i * 6 + 6]); if (ins.check(acc) && ArmBow.ins) { ArmBow.ins.ADS_fire(); } } } float ToAcceleratedSpeed(byte b1, byte b2) { int value = TwoByteToInt(b1, b2); return (float)value / 32768 * LoginMgr.myUserInfo.deviceAccValue; } int TwoByteToInt(byte b1, byte b2) { ushort twoByte = (ushort)(b1 * 256 + b2); short shortNum = (short)twoByte; return (int)shortNum; } bool check(float acc) { DebugLine.show(acc); //这个不需要注释,静态函数内置判断 if (locked) { return false; } if (acc > cmd.getAcc() && acc > maxAcc) { maxAcc = acc; return false; } else if (acc < cmd.getAcc() && maxAcc != 0) { shootSpeed = maxAcc; // Log("最大加速度:" + maxAcc); maxAcc = 0; Dolock(); Invoke("Unlock", 1.8f); return true; } return false; } void Dolock() { locked = true; } void Unlock() { locked = false; } void Log(string text) { if (this.text) { this.text.text = text; } else { Debug.Log(text); } } } [Serializable] class CMD { // public string ax = "y"; // public int a = 6000; // public int r = 2; public string a = "y"; public int a1 = 3; public int a2 = -3; public int r = 2; public float getAcc() { return a1; } }