ShootCheck.cs 15 KB

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