using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using ArduinoBluetoothAPI; using DG.Tweening; using UnityEngine.SceneManagement; using System.Linq; using BestHTTP.WebSocket; /* 射箭检测 */ public class ShootCheck : MonoBehaviour { [SerializeField] Text text; public float shootSpeed; public static ShootCheck ins; void Awake() { ins = this; } [SerializeField] InputField ArmBowInputField = default; public void SetShootBackTime() { ArmBow.ins.shootBackTime = int.Parse(ArmBowInputField.text); } [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 * CommonConfig.arrowWeight / UserSettings.ins.actualArrowWeight); //打印 string logTxt = $"序号{id},时区1:{time1}毫秒,时区2:{time2}毫秒,校验:{sumCheckRes},弓轨速度:{speed}m/s,箭的速度:{shootSpeed}m/s"; if (DebugForDevice.ins) DebugForDevice.ins.LogInfrared(logTxt); Debug.Log(logTxt); if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) { SB_EventSystem.ins.ClickMouse(); } else if (ArmBow.ins) { ArmBow.ins.ADS_fire(true); } else if (DuckHunter.SmartBowController.Instance) { //野鸭射击 DuckHunter.SmartBowController.Instance.OnShooting(shootSpeed); } else if (WildAttack.SmartBowController.Instance) { //荒野射击 WildAttack.SmartBowController.Instance.OnShooting(shootSpeed); } else if (GameController.ins && GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer) != null) { //本地双人模式下处理1p,2P 在 BluetoothAim 类处理 GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer).ADS_fire(true, shootSpeed); } else if (GeneratingTarget.gm != null) { //移动目标 GeneratingTarget.gm.Shooting(true); } else { OnGameShoot?.Invoke(shootSpeed); } } public Action OnGameShoot; //分离弹夹事件 public Action OnGameUpdateTheMagazine; //弹夹状态 public SmartBowSDK.BluetoothDeviceStatus bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineLoading; /// /// 0x00 - 弹夹分离 0x01 - 弹夹上膛 /// /// public void UpdateTheMagazine(byte[] bytes) { if (bytes[1] == 0x00) { Debug.Log("弹夹分离:" + System.BitConverter.ToString(bytes)); bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineSeparation; if (GeneratingTarget.gm) { //Hyperspace GeneratingTarget.gm.OnSeparation(); } } else if ( bytes[1] == 0x01){ //播放上弹夹时候的声音 AudioMgr.ins.PlayBeLoaded(); Debug.Log("弹夹上膛:" + System.BitConverter.ToString(bytes)); bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineLoading; if (Billboard.ins) { Billboard.ins.bulletManager.ResetBullets(); //game } else if (GeneratingTarget.gm) { //Hyperspace GeneratingTarget.gm.OnLoading(); } else { OnGameUpdateTheMagazine?.Invoke(); //水果在gamingmanager } } } void Log(string text) { if (this.text) { this.text.text = text; } else { Debug.Log(text); } } }