using System; using UnityEngine; /* 射箭检测 */ public class ShootCheck : MonoBehaviour { public Action onShoot; private float _lastNotifyTime_OnShoot; const float arrowWeight = 75; const float actualArrowWeight = 20; [NonSerialized] public float shootSpeed; public static ShootCheck ins; void Awake() { ins = this; } [NonSerialized] public byte byteTime1; [NonSerialized] public byte byteTime2; /**通过红外线数据进行射击 */ public void ShootByInfrared(byte[] bytes) { byteTime1 = bytes[2]; byteTime1 = bytes[3]; int id = bytes[1]; //序号 float time1 = bytes[2] * 0.1f; //时区1耗时 float time2 = bytes[3] * 0.1f; //时区2耗时 float totalTime = time1 + time2; // if (totalTime <= 0) { // totalTime = 0.3f; // } //校验和 int sumCheck = bytes[0] + bytes[1] + bytes[2] + bytes[3]; sumCheck &= 0xff; //校验和比较结果 bool sumCheckRes = sumCheck == bytes[4]; //弓轨速度 float speed = 0.05f / (totalTime / 1000f); //通过动能定理求箭的速度(实体箭质量*实体箭速度^2=游戏中箭的质量*游戏中箭的速度^2) shootSpeed = Mathf.Sqrt(speed * speed * arrowWeight / actualArrowWeight); //打印 //string logTxt = $"序号{id},时区1:{time1}毫秒,时区2:{time2}毫秒,校验:{sumCheckRes},弓轨速度:{speed}m/s,箭的速度:{shootSpeed}m/s"; //收到射箭数据,就回复硬件,否则n毫秒后硬件会认为丢包进行重传 try { if (sumCheckRes) BluetoothAim.ins.ReplyInfraredShoot(); //如果数据正确,则回复硬件 } catch (Exception) {} //调用游戏中的射箭接口 AimHandler.ins._9Axis.axisCSBridge.o09AxisCS.OnShot(100, 100, 100000); //if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) { //SB_EventSystem.ins.ClickMouse(); //} else if (ArmBow.ins) { #undetermined if (Time.realtimeSinceStartup - _lastNotifyTime_OnShoot > 1.2f) { _lastNotifyTime_OnShoot = Time.realtimeSinceStartup; onShoot?.Invoke(shootSpeed); } //} #undetermined } }