| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- 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;
- using SmartBowSDK;
- /* 射箭检测 */
- 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;
- //目前只处理m4
- private int _lastHandledId = -1;
- private float _lastHandledTime = 0f;
- private const float duplicateIgnoreWindow = 0.1f; // 可调:忽略窗口(单位秒)
- /**通过红外线数据进行射击 */
- public void ShootByInfrared(byte[] bytes) {
- int id = bytes[1]; //序号
- float now = Time.time;
- // 判断是否是重复射击
- if (id == _lastHandledId && (now - _lastHandledTime) < duplicateIgnoreWindow)
- {
- // 相同序号 + 窗口时间内,不处理
- return;
- }
- // 记录本次处理状态
- _lastHandledId = id;
- _lastHandledTime = now;
- byteTime1 = bytes[2];
- byteTime1 = bytes[3];
- 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<float> OnGameShoot;
- //分离弹夹事件
- public Action OnGameUpdateTheMagazine;
- //弹夹状态
- public BluetoothDeviceStatus bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineLoading;
- /// <summary>
- /// 0x00 - 弹夹分离 0x01 - 弹夹上膛
- /// </summary>
- /// <param name="bytes"></param>
- public void UpdateTheMagazine(byte[] bytes) {
- BluetoothDeviceType temp = bluetoothDeviceType != BluetoothDeviceType.NONE ?
- bluetoothDeviceType : BluetoothDeviceType.Pistol1;
- if (bytes[1] == 0x00) {
- //添加一个同步状态
- BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.MagazineSeparation);
- Debug.Log("弹夹分离:" + BitConverter.ToString(bytes));
- bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineSeparation;
- if (GeneratingTarget.gm) {
- //Hyperspace
- GeneratingTarget.gm.OnSeparation();
- }
- } else if ( bytes[1] == 0x01){
- //添加一个同步状态
- BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.MagazineLoading);
- //播放上弹夹时候的声音
- AudioMgr.ins.PlayBeLoaded();
- Debug.Log("弹夹上膛:" + BitConverter.ToString(bytes));
- bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineLoading;
- if (Billboard.ins)
- {
- Billboard.ins.bulletManager.ResetBullets(); //game
- }
- else if (GeneratingTarget.gm)
- { //Hyperspace
- GeneratingTarget.gm.OnLoading();
- }
- else
- {
- OnGameUpdateTheMagazine?.Invoke(); //水果在gamingmanager
- }
- }
- }
- //记录当前的蓝牙设备信息
- private BluetoothDeviceType bluetoothDeviceType = BluetoothDeviceType.NONE;
- //记录连接的设备系统信息
- private ConnectPlatform connectPlatform = ConnectPlatform.NONE;
- /// <summary>
- /// 0x00 未上膛,0x01 已上膛
- /// </summary>
- /// <param name="bytes"></param>
- public void UpdateChamberState(byte[] bytes)
- {
- Debug.Log("操作上膛状态:" + BitConverter.ToString(bytes));
- BluetoothDeviceType temp = bluetoothDeviceType != BluetoothDeviceType.NONE ?
- bluetoothDeviceType : BluetoothDeviceType.Pistol1;
- if (bytes[1] == 0x00)
- {
- BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.ChamberEmpty);
- }
- else if (bytes[1] == 0x01)
- {
- BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.Chambered);
- }
- }
- /// <summary>
- /// 记录当前获得的设备连接数据
- /// </summary>
- /// <param name="bytes"></param>
- public void UpdateDeviceAndSysInfo(byte[] bytes)
- {
- //Debug.Log("接收到系统数据:" + System.BitConverter.ToString(bytes));
- //设备类型
- switch (bytes[1])
- {
- case 0x01:
- bluetoothDeviceType = BluetoothDeviceType.HOUYIPro;
- break;
- case 0x02:
- bluetoothDeviceType = BluetoothDeviceType.ARTEMISPro;
- break;
- case 0x03:
- bluetoothDeviceType = BluetoothDeviceType.PistolM9;
- break;
- case 0x04:
- bluetoothDeviceType = BluetoothDeviceType.APOLLO;
- break;
- case 0x05:
- bluetoothDeviceType = BluetoothDeviceType.PistolM17;
- break;
- case 0x06:
- bluetoothDeviceType = BluetoothDeviceType.RifleM416;
- break;
- }
- // 系统类型
- switch (bytes[2])
- {
- case 0x01:
- connectPlatform = ConnectPlatform.PHONE;
- break;
- case 0x02:
- connectPlatform = ConnectPlatform.PC;
- break;
- case 0x03:
- connectPlatform = ConnectPlatform.VR;
- break;
- }
- Debug.Log("设备类型:" + bluetoothDeviceType.ToString());
- Debug.Log("系统类型:" + connectPlatform.ToString());
- //初始化时候回调数据
- BluetoothAim.ins.InvokeOnDeviceAndSystemInfoEvent(connectPlatform, bluetoothDeviceType);
- }
- void Log(string text)
- {
- if (this.text)
- {
- this.text.text = text;
- } else {
- Debug.Log(text);
- }
- }
- }
|