ShootCheck.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 float shootSpeed;
  16. public static ShootCheck ins;
  17. void Awake()
  18. {
  19. ins = this;
  20. }
  21. [SerializeField] InputField ArmBowInputField = default;
  22. public void SetShootBackTime()
  23. {
  24. ArmBow.ins.shootBackTime = int.Parse(ArmBowInputField.text);
  25. }
  26. [NonSerialized] public byte byteTime1;
  27. [NonSerialized] public byte byteTime2;
  28. /**通过红外线数据进行射击 */
  29. public void ShootByInfrared(byte[] bytes) {
  30. byteTime1 = bytes[2];
  31. byteTime1 = bytes[3];
  32. int id = bytes[1]; //序号
  33. float time1 = bytes[2] * 0.1f; //时区1耗时
  34. float time2 = bytes[3] * 0.1f; //时区2耗时
  35. float totalTime = time1 + time2;
  36. // if (totalTime <= 0) {
  37. // totalTime = 0.3f;
  38. // }
  39. //校验和
  40. int sumCheck = bytes[0] + bytes[1] + bytes[2] + bytes[3];
  41. sumCheck &= 0xff;
  42. //校验和比较结果
  43. bool sumCheckRes = sumCheck == bytes[4];
  44. //弓轨速度
  45. float speed = 0.05f / (totalTime / 1000f);
  46. //通过动能定理求箭的速度(实体箭质量*实体箭速度^2=游戏中箭的质量*游戏中箭的速度^2)
  47. shootSpeed = Mathf.Sqrt(speed * speed * CommonConfig.arrowWeight / UserSettings.ins.actualArrowWeight);
  48. //打印
  49. string logTxt = $"序号{id},时区1:{time1}毫秒,时区2:{time2}毫秒,校验:{sumCheckRes},弓轨速度:{speed}m/s,箭的速度:{shootSpeed}m/s";
  50. if (DebugForDevice.ins) DebugForDevice.ins.LogInfrared(logTxt);
  51. Debug.Log(logTxt);
  52. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
  53. SB_EventSystem.ins.ClickMouse();
  54. } else if (ArmBow.ins) {
  55. ArmBow.ins.ADS_fire(true);
  56. } else if (DuckHunter.SmartBowController.Instance) {
  57. //野鸭射击
  58. DuckHunter.SmartBowController.Instance.OnShooting(shootSpeed);
  59. } else if (WildAttack.SmartBowController.Instance)
  60. {
  61. //荒野射击
  62. WildAttack.SmartBowController.Instance.OnShooting(shootSpeed);
  63. } else if (GameController.ins && GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer) != null) {
  64. //本地双人模式下处理1p,2P 在 BluetoothAim 类处理
  65. GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer).ADS_fire(true, shootSpeed);
  66. } else if (GeneratingTarget.gm != null) {
  67. //移动目标
  68. GeneratingTarget.gm.Shooting(true);
  69. }
  70. else {
  71. OnGameShoot?.Invoke(shootSpeed);
  72. }
  73. }
  74. public Action<float> OnGameShoot;
  75. //分离弹夹事件
  76. public Action OnGameUpdateTheMagazine;
  77. //弹夹状态
  78. public SmartBowSDK.BluetoothDeviceStatus bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineLoading;
  79. /// <summary>
  80. /// 0x00 - 弹夹分离 0x01 - 弹夹上膛
  81. /// </summary>
  82. /// <param name="bytes"></param>
  83. public void UpdateTheMagazine(byte[] bytes) {
  84. if (bytes[1] == 0x00) {
  85. Debug.Log("弹夹分离:" + System.BitConverter.ToString(bytes));
  86. bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineSeparation;
  87. if (GeneratingTarget.gm) {
  88. //Hyperspace
  89. GeneratingTarget.gm.OnSeparation();
  90. }
  91. } else if ( bytes[1] == 0x01){
  92. //播放上弹夹时候的声音
  93. AudioMgr.ins.PlayBeLoaded();
  94. Debug.Log("弹夹上膛:" + System.BitConverter.ToString(bytes));
  95. bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineLoading;
  96. if (Billboard.ins)
  97. {
  98. Billboard.ins.bulletManager.ResetBullets(); //game
  99. }
  100. else if (GeneratingTarget.gm)
  101. { //Hyperspace
  102. GeneratingTarget.gm.OnLoading();
  103. }
  104. else
  105. {
  106. OnGameUpdateTheMagazine?.Invoke(); //水果在gamingmanager
  107. }
  108. }
  109. }
  110. void Log(string text)
  111. {
  112. if (this.text)
  113. {
  114. this.text.text = text;
  115. } else {
  116. Debug.Log(text);
  117. }
  118. }
  119. }