ShootCheck.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using ArduinoBluetoothAPI;
  7. using DG.Tweening;
  8. using UnityEngine.SceneManagement;
  9. using System.Linq;
  10. using BestHTTP.WebSocket;
  11. using SmartBowSDK;
  12. /* 射箭检测 */
  13. public class ShootCheck : MonoBehaviour
  14. {
  15. [SerializeField] Text text;
  16. public CMD cmd = new CMD();
  17. bool locked = false;
  18. float maxAcc = 0;
  19. Queue<Vector3> keyAccList = new Queue<Vector3>();
  20. Queue<string> keyTimeList = new Queue<string>();
  21. public float shootSpeed;
  22. public static ShootCheck ins;
  23. [SerializeField] InputField ipInputField = default;
  24. public WebSocket webSocket;
  25. void Awake()
  26. {
  27. ins = this;
  28. }
  29. //用户输入时的变化
  30. public void ChangedValue(string value)
  31. {
  32. ipInputField.ActivateInputField();
  33. Debug.Log("输入了" + value);
  34. }
  35. public void EndValue(string value)
  36. {
  37. Debug.Log("最终内容" + value);
  38. }
  39. //socket
  40. public void StartSocket()
  41. {
  42. //socket
  43. string ipStr = ipInputField.text;//ipInputField.GetComponentInChildren<Text>();
  44. string serverIP = ipStr;
  45. // serverIP = "172.16.20.57";
  46. if (serverIP.Length == 0) serverIP = "192.168.101.14";
  47. string address = "ws://" + serverIP + ":8088/Ble/";
  48. webSocket = new WebSocket(new Uri(address));
  49. #if !UNITY_WEBGL
  50. webSocket.StartPingThread = true;
  51. #endif
  52. // Subscribe to the WS events
  53. webSocket.OnOpen += OnOpen;
  54. webSocket.OnMessage += OnMessageRecv;
  55. webSocket.OnBinary += OnBinaryRecv;
  56. webSocket.OnClosed += OnClosed;
  57. webSocket.OnError += OnError;
  58. // Debug.Log("OnOpen: ");
  59. // Start connecting to the server
  60. webSocket.Open();
  61. }
  62. public void Destroy()
  63. {
  64. if (webSocket != null)
  65. {
  66. webSocket.Close();
  67. webSocket = null;
  68. }
  69. }
  70. void OnOpen(WebSocket ws)
  71. {
  72. Debug.Log("OnOpen: ");
  73. webSocket.Send("unity");
  74. }
  75. void OnMessageRecv(WebSocket ws, string message)
  76. {
  77. Debug.LogFormat("OnMessageRecv: msg={0}", message);
  78. }
  79. void OnBinaryRecv(WebSocket ws, byte[] data)
  80. {
  81. Debug.LogFormat("OnBinaryRecv: len={0}", data.Length);
  82. }
  83. void OnClosed(WebSocket ws, UInt16 code, string message)
  84. {
  85. Debug.LogFormat("OnClosed: code={0}, msg={1}", code, message);
  86. webSocket = null;
  87. }
  88. void OnError(WebSocket ws, Exception ex)
  89. {
  90. string errorMsg = string.Empty;
  91. #if !UNITY_WEBGL || UNITY_EDITOR
  92. if (ws.InternalRequest.Response != null)
  93. {
  94. errorMsg = string.Format("Status Code from Server: {0} and Message: {1}", ws.InternalRequest.Response.StatusCode, ws.InternalRequest.Response.Message);
  95. }
  96. #endif
  97. Debug.LogFormat("OnError: error occured: {0}\n", (ex != null ? ex.Message : "Unknown Error " + errorMsg));
  98. webSocket = null;
  99. }
  100. //socket
  101. [SerializeField] InputField ArmBowInputField = default;
  102. public void SetShootBackTime()
  103. {
  104. ArmBow.ins.shootBackTime = int.Parse(ArmBowInputField.text);
  105. }
  106. public void OnBluetoothReady(BluetoothShoot bluetoothShoot)
  107. {
  108. // if (LoginMgr.myUserInfo.arrowAccValue == 16)
  109. // {
  110. // cmd.a = "y";
  111. // }
  112. // else
  113. // {
  114. // cmd.a = "x";
  115. // }
  116. // bluetoothShoot.WriteData(JsonUtility.ToJson(cmd).Replace("\"", ""));
  117. // Sequence sequence = DOTween.Sequence();
  118. // sequence.PrependInterval(1).AppendCallback(delegate() {
  119. // canAdjustNormalOrHightMode = true;
  120. // AdjustNormalOrHightMode();
  121. // });
  122. // sequence.SetUpdate(true);
  123. }
  124. //===普通模式和高速模式的切换===
  125. public bool canAdjustNormalOrHightMode = false;
  126. public int transportMode = 0;
  127. public void AdjustNormalOrHightMode()
  128. {
  129. if (!canAdjustNormalOrHightMode) return;
  130. try {
  131. // string sceneName = SceneManager.GetActiveScene().name;
  132. // if (sceneName == "Game")
  133. // {
  134. // transportMode = 1;
  135. // BluetoothShoot.ins.WriteData("HA");
  136. // }
  137. // else
  138. // {
  139. // transportMode = 0;
  140. // BluetoothShoot.ins.WriteData("NA");
  141. // }
  142. transportMode = 0;
  143. BluetoothShoot.ins.WriteData("NA");
  144. } catch (Exception) { }
  145. }
  146. public void OnDataReceived(byte[] bytes)
  147. {
  148. string str1 = "byte=";
  149. if (webSocket != null)
  150. {
  151. for (int i = 0; i < bytes.Length - 1; i++)
  152. {
  153. str1 += bytes[i];
  154. }
  155. // webSocket.Send(str2);
  156. }
  157. string str2 = "";
  158. if (transportMode == 0)
  159. {
  160. for (int i = 0; i < bytes.Length / 6; i++)
  161. {
  162. float acc = ToAcceleratedSpeed(bytes[i * 6 + 4], bytes[i * 6 + 5]);
  163. string t = "(采样时间:" + (int)bytes[i * 6 + 0] + "分" + (int)bytes[i * 6 + 1] + "秒" + TwoByteToInt(bytes[i * 6 + 2], bytes[i * 6 + 3]) + "毫秒)";
  164. str2 += "加速度:" + acc + t + "\n";
  165. if (ins.check(0, acc, 0, t) && ArmBow.ins)
  166. {
  167. ArmBow.ins.ADS_fire(true);
  168. }
  169. }
  170. }
  171. else if (transportMode == 1)
  172. {
  173. for (int i = 0; i < bytes.Length / 11; i++)
  174. {
  175. float ax = ToAcceleratedSpeed(bytes[i * 11 + 5], bytes[i * 11 + 6]);
  176. float ay = ToAcceleratedSpeed(bytes[i * 11 + 7], bytes[i * 11 + 8]);
  177. float az = ToAcceleratedSpeed(bytes[i * 11 + 9], bytes[i * 11 + 10]);
  178. string t = "(采样时间:" + (int)bytes[i * 11 + 3] + "分" + (int)bytes[i * 11 + 4] + "秒" + TwoByteToInt(bytes[i * 11 + 1], bytes[i * 11 + 2]) + "毫秒)";
  179. str2 += "加速度:" + ay + t + "\n";
  180. if (ins.check(ax, ay, az, t) && ArmBow.ins)
  181. {
  182. ArmBow.ins.ADS_fire(true);
  183. }
  184. }
  185. }
  186. if (webSocket != null)
  187. {
  188. string str3 = str1 + "\n" + str2;
  189. webSocket.Send(str3);
  190. }
  191. }
  192. float ToAcceleratedSpeed(byte b1, byte b2)
  193. {
  194. int value = TwoByteToInt(b1, b2);
  195. // return (float)value / 32768 * LoginMgr.myUserInfo.arrowAccValue;
  196. return (float)value / 32768;
  197. }
  198. int TwoByteToInt(byte b1, byte b2)
  199. {
  200. ushort twoByte = (ushort)(b1 * 256 + b2);
  201. short shortNum = (short)twoByte;
  202. return (int)shortNum;
  203. }
  204. bool check(float ax, float ay, float az, string t)
  205. {
  206. float acc = ay;
  207. DebugLine.show(acc); //这个不需要注释,静态函数内置判断
  208. if (locked)
  209. {
  210. return false;
  211. }
  212. if (acc > cmd.getAcc())
  213. {
  214. if (acc > maxAcc)
  215. {
  216. maxAcc = acc;
  217. }
  218. // if (acc > 15.9f && LoginMgr.myUserInfo.arrowAccValue == 16) {
  219. // double p1 = -1.56729339506415;
  220. // double p2 = 0.0397744840580165;
  221. // double p3 = 4.73453844008481;
  222. // float x = (keyAccList.Count + 1) * 2; //单位毫秒
  223. // double y = 1.0 / (p1+p2*Mathf.Pow(x, 0.5f)*Mathf.Log(x)+p3/Mathf.Pow(x, 0.5f));
  224. // acc = (float) y;
  225. // }
  226. Vector3 keyAcc = new Vector3(ax, acc, az);
  227. keyAccList.Enqueue(keyAcc);
  228. keyTimeList.Enqueue(t);
  229. return false;
  230. }
  231. else if (acc < cmd.getAcc() && maxAcc != 0) {
  232. //积分求初速度
  233. shootSpeed = 0;
  234. float lasKeytAcc = 0;
  235. int keyAccIndex = 0;
  236. float timeInterval = 0.002f;
  237. float totalAx = 0;
  238. float totalAy = 0;
  239. float totalAz = 0;
  240. foreach (var keyAcc in keyAccList)
  241. {
  242. totalAx += Mathf.Abs(keyAcc.x);
  243. totalAy += Mathf.Abs(keyAcc.y);
  244. totalAz += Mathf.Abs(keyAcc.z);
  245. if (keyAccIndex > 0)
  246. {
  247. shootSpeed += keyAcc.y * timeInterval;
  248. shootSpeed -= (keyAcc.y - lasKeytAcc) * timeInterval / 2;
  249. }
  250. else if (keyAccIndex == 0 && keyAccList.Count == 1)
  251. {
  252. shootSpeed = keyAcc.y * timeInterval;
  253. }
  254. lasKeytAcc = keyAcc.y;
  255. keyAccIndex++;
  256. }
  257. //是不是合法射出
  258. bool isLegalShoot = totalAy > totalAx && totalAy > totalAz;
  259. if (isLegalShoot)
  260. {
  261. //加速度acc的单位是g,最后需要乘上
  262. shootSpeed *= 9.80665f;
  263. string strShootSpeed = "弓轨速度: " + shootSpeed + " 帧数: " + keyAccList.Count + "\n";
  264. // shootSpeed = Mathf.Sqrt(shootSpeed * shootSpeed * arrowWeight / LoginMgr.myUserInfo.actualArrowWeight);
  265. strShootSpeed += "箭的速度: " + shootSpeed + "\n";
  266. string str1 = strShootSpeed + "/////////检测到射出的数据////////////:\n";
  267. for (int i = 0; i < keyAccList.Count; i++)
  268. {
  269. float keyAcc = keyAccList.ElementAt(i).y;
  270. string time = keyTimeList.ElementAt(i);
  271. str1 += "加速度:" + keyAcc + time + "\n";
  272. }
  273. Debug.LogWarning(str1);
  274. if (webSocket != null)
  275. {
  276. webSocket.Send(str1 + "/////////检测到射出的数据////////////:\n");
  277. }
  278. }
  279. //本轮计算结束
  280. keyAccList.Clear();
  281. keyTimeList.Clear();
  282. maxAcc = 0;
  283. Dolock();
  284. Invoke("Unlock", 1.8f);
  285. return isLegalShoot;
  286. }
  287. return false;
  288. }
  289. [NonSerialized] public byte byteTime1;
  290. [NonSerialized] public byte byteTime2;
  291. //目前只处理m4
  292. private int _lastHandledId = -1;
  293. private float _lastHandledTime = 0f;
  294. private const float duplicateIgnoreWindow = 0.1f; // 可调:忽略窗口(单位秒)
  295. /**通过红外线数据进行射击 */
  296. public void ShootByInfrared(byte[] bytes) {
  297. int id = bytes[1]; //序号
  298. float now = Time.time;
  299. // 判断是否是重复射击
  300. if (id == _lastHandledId && (now - _lastHandledTime) < duplicateIgnoreWindow)
  301. {
  302. // 相同序号 + 窗口时间内,不处理
  303. return;
  304. }
  305. // 记录本次处理状态
  306. _lastHandledId = id;
  307. _lastHandledTime = now;
  308. byteTime1 = bytes[2];
  309. byteTime1 = bytes[3];
  310. float time1 = bytes[2] * 0.1f; //时区1耗时
  311. float time2 = bytes[3] * 0.1f; //时区2耗时
  312. float totalTime = time1 + time2;
  313. // if (totalTime <= 0) {
  314. // totalTime = 0.3f;
  315. // }
  316. //校验和
  317. int sumCheck = bytes[0] + bytes[1] + bytes[2] + bytes[3];
  318. sumCheck &= 0xff;
  319. //校验和比较结果
  320. bool sumCheckRes = sumCheck == bytes[4];
  321. //弓轨速度
  322. float speed = 0.05f / (totalTime / 1000f);
  323. //通过动能定理求箭的速度(实体箭质量*实体箭速度^2=游戏中箭的质量*游戏中箭的速度^2)
  324. shootSpeed = Mathf.Sqrt(speed * speed * CommonConfig.arrowWeight / UserSettings.ins.actualArrowWeight);
  325. //打印
  326. string logTxt = $"序号{id},时区1:{time1}毫秒,时区2:{time2}毫秒,校验:{sumCheckRes},弓轨速度:{speed}m/s,箭的速度:{shootSpeed}m/s";
  327. if (DebugForDevice.ins) DebugForDevice.ins.LogInfrared(logTxt);
  328. //Debug.Log(logTxt);
  329. //收到射箭数据,就回复硬件,否则n毫秒后硬件会认为丢包进行重传
  330. //try {
  331. // if (sumCheckRes) BluetoothAim.ins.ReplyInfraredShoot(); //如果数据正确,则回复硬件
  332. //} catch (Exception) { }
  333. //打印到nodejs
  334. try {
  335. ShootCheck.ins.webSocket.Send(logTxt);
  336. } catch (Exception) { }
  337. //调用游戏中的射箭接口
  338. AimHandler.ins.NotifyAxisOnShot();
  339. //OnArmBowFire,用于新手提示 GameRuleView
  340. //OnArmBowFire?.Invoke(shootSpeed);
  341. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
  342. SB_EventSystem.ins.ClickMouse();
  343. } else if (ArmBow.ins) {
  344. ArmBow.ins.ADS_fire(true);
  345. } else if (DuckHunter.SmartBowController.Instance) {
  346. //野鸭射击
  347. DuckHunter.SmartBowController.Instance.OnShooting(shootSpeed);
  348. } else if (WildAttack.SmartBowController.Instance)
  349. {
  350. //荒野射击
  351. WildAttack.SmartBowController.Instance.OnShooting(shootSpeed);
  352. } else if (GameController.ins && GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer) != null) {
  353. //本地双人模式下处理1p,2P 在 BluetoothAim 类处理
  354. GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer).ADS_fire(true, shootSpeed);
  355. } else if (GeneratingTarget.gm != null) {
  356. //移动目标
  357. GeneratingTarget.gm.Shooting(true);
  358. }
  359. else {
  360. OnGameShoot?.Invoke(shootSpeed);
  361. }
  362. }
  363. public Action<float> OnGameShoot;
  364. //分离弹夹事件
  365. public Action OnGameUpdateTheMagazine;
  366. //弹夹状态
  367. public BluetoothDeviceStatus bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineLoading;
  368. /// <summary>
  369. /// 0x00 - 弹夹分离 0x01 - 弹夹上膛
  370. /// </summary>
  371. /// <param name="bytes"></param>
  372. public void UpdateTheMagazine(byte[] bytes) {
  373. BluetoothDeviceType temp = bluetoothDeviceType != BluetoothDeviceType.NONE ?
  374. bluetoothDeviceType : BluetoothDeviceType.Pistol1;
  375. if (bytes[1] == 0x00) {
  376. //添加一个同步状态
  377. BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.MagazineSeparation);
  378. Debug.Log("弹夹分离:" + BitConverter.ToString(bytes));
  379. bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineSeparation;
  380. if (GeneratingTarget.gm) {
  381. //Hyperspace
  382. GeneratingTarget.gm.OnSeparation();
  383. }
  384. } else if ( bytes[1] == 0x01){
  385. //添加一个同步状态
  386. BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.MagazineLoading);
  387. //播放上弹夹时候的声音
  388. AudioMgr.ins.PlayBeLoaded();
  389. Debug.Log("弹夹上膛:" + BitConverter.ToString(bytes));
  390. bluetoothDeviceStatus = BluetoothDeviceStatus.MagazineLoading;
  391. if (Billboard.ins)
  392. {
  393. Billboard.ins.bulletManager.ResetBullets(); //game
  394. }
  395. else if (GeneratingTarget.gm)
  396. { //Hyperspace
  397. GeneratingTarget.gm.OnLoading();
  398. }
  399. else
  400. {
  401. OnGameUpdateTheMagazine?.Invoke(); //水果在gamingmanager
  402. }
  403. }
  404. }
  405. //public Action<float> OnArmBowFire;
  406. void Dolock()
  407. {
  408. locked = true;
  409. }
  410. void Unlock()
  411. {
  412. locked = false;
  413. }
  414. //记录当前的蓝牙设备信息
  415. private BluetoothDeviceType bluetoothDeviceType = BluetoothDeviceType.NONE;
  416. //记录连接的设备系统信息
  417. private ConnectPlatform connectPlatform = ConnectPlatform.NONE;
  418. /// <summary>
  419. /// 0x00 未上膛,0x01 已上膛
  420. /// </summary>
  421. /// <param name="bytes"></param>
  422. public void UpdateChamberState(byte[] bytes)
  423. {
  424. Debug.Log("操作上膛状态:" + BitConverter.ToString(bytes));
  425. BluetoothDeviceType temp = bluetoothDeviceType != BluetoothDeviceType.NONE ?
  426. bluetoothDeviceType : BluetoothDeviceType.Pistol1;
  427. if (bytes[1] == 0x00)
  428. {
  429. BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.ChamberEmpty);
  430. }
  431. else if (bytes[1] == 0x01)
  432. {
  433. BluetoothAim.ins.InvokeOnBleDevice(temp, BluetoothDeviceStatus.Chambered);
  434. }
  435. }
  436. /// <summary>
  437. /// 记录当前获得的设备连接数据
  438. /// </summary>
  439. /// <param name="bytes"></param>
  440. public void UpdateDeviceAndSysInfo(byte[] bytes)
  441. {
  442. //Debug.Log("接收到系统数据:" + System.BitConverter.ToString(bytes));
  443. //设备类型
  444. switch (bytes[1])
  445. {
  446. case 0x01:
  447. bluetoothDeviceType = BluetoothDeviceType.HOUYIPro;
  448. break;
  449. case 0x02:
  450. bluetoothDeviceType = BluetoothDeviceType.ARTEMISPro;
  451. break;
  452. case 0x03:
  453. bluetoothDeviceType = BluetoothDeviceType.PistolM9;
  454. break;
  455. case 0x04:
  456. bluetoothDeviceType = BluetoothDeviceType.APOLLO;
  457. break;
  458. case 0x05:
  459. bluetoothDeviceType = BluetoothDeviceType.PistolM17;
  460. break;
  461. case 0x06:
  462. bluetoothDeviceType = BluetoothDeviceType.RifleM416;
  463. break;
  464. }
  465. // 系统类型
  466. switch (bytes[2])
  467. {
  468. case 0x01:
  469. connectPlatform = ConnectPlatform.PHONE;
  470. break;
  471. case 0x02:
  472. connectPlatform = ConnectPlatform.PC;
  473. break;
  474. case 0x03:
  475. connectPlatform = ConnectPlatform.VR;
  476. break;
  477. }
  478. Debug.Log("设备类型:" + bluetoothDeviceType.ToString());
  479. Debug.Log("系统类型:" + connectPlatform.ToString());
  480. //初始化时候回调数据
  481. BluetoothAim.ins.InvokeOnDeviceAndSystemInfoEvent(connectPlatform, bluetoothDeviceType);
  482. }
  483. void Log(string text)
  484. {
  485. if (this.text)
  486. {
  487. this.text.text = text;
  488. } else {
  489. Debug.Log(text);
  490. }
  491. }
  492. }
  493. [Serializable]
  494. public class CMD {
  495. // public string ax = "y";
  496. // public int a = 6000;
  497. // public int r = 2;
  498. public string a = "y";
  499. public int a1 = 3;
  500. public int a2 = -3;
  501. public int r = 2;
  502. public float getAcc() {
  503. return a1;
  504. }
  505. }