ShootCheck.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. //收到射箭数据,就回复硬件,否则n毫秒后硬件会认为丢包进行重传
  53. //try {
  54. // if (sumCheckRes) BluetoothAim.ins.ReplyInfraredShoot(); //如果数据正确,则回复硬件
  55. //} catch (Exception) { }
  56. //不在home页面的话,当设置coin为0时候,不返回,不为0并且没有时间,才会返回
  57. if (SceneManager.GetActiveScene().name != "Home" && UserSettings.ins.PerRoundCoin != 0 && StandaloneAPI.GetGameTimeCountDown() <= 0) {
  58. //如果射击时候不在home 场景,则返回home 主页
  59. StandaloneAPI.ForceBackHome();
  60. Debug.Log("射击回退主场景!");
  61. return;
  62. }
  63. //OnArmBowFire,用于新手提示 GameRuleView
  64. //OnArmBowFire?.Invoke(shootSpeed);
  65. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
  66. SB_EventSystem.ins.ClickMouse();
  67. } else if (ArmBow.ins) {
  68. ArmBow.ins.ADS_fire(true);
  69. } else if (DuckHunter.SmartBowController.Instance) {
  70. //野鸭射击
  71. DuckHunter.SmartBowController.Instance.OnShooting(shootSpeed);
  72. } else if (WildAttack.SmartBowController.Instance)
  73. {
  74. //荒野射击
  75. WildAttack.SmartBowController.Instance.OnShooting(shootSpeed);
  76. } else if (GameController.ins && GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer) != null) {
  77. //本地双人模式下处理1p,2P 在 BluetoothAim 类处理
  78. GameController.ins.GetArmBowDoublePlayer(PlayerType.FirstPlayer).ADS_fire(true, shootSpeed);
  79. } else if (GeneratingTarget.gm != null) {
  80. //移动目标
  81. GeneratingTarget.gm.Shooting(true);
  82. }
  83. else {
  84. OnGameShoot?.Invoke(shootSpeed);
  85. }
  86. }
  87. public Action<float> OnGameShoot;
  88. //分离弹夹事件
  89. public Action OnGameUpdateTheMagazine;
  90. //弹夹状态
  91. public SmartBowSDK.BluetoothDeviceStatus bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineLoading;
  92. /// <summary>
  93. /// 0x00 - 弹夹分离 0x01 - 弹夹上膛
  94. /// </summary>
  95. /// <param name="bytes"></param>
  96. public void UpdateTheMagazine(byte[] bytes) {
  97. if (bytes[1] == 0x00) {
  98. Debug.Log("弹夹分离:" + System.BitConverter.ToString(bytes));
  99. bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineSeparation;
  100. if (GeneratingTarget.gm) {
  101. //Hyperspace
  102. GeneratingTarget.gm.OnSeparation();
  103. }
  104. } else if ( bytes[1] == 0x01){
  105. //播放上弹夹时候的声音
  106. AudioMgr.ins.PlayBeLoaded();
  107. Debug.Log("弹夹上膛:" + System.BitConverter.ToString(bytes));
  108. bluetoothDeviceStatus = SmartBowSDK.BluetoothDeviceStatus.MagazineLoading;
  109. if (Billboard.ins)
  110. {
  111. Billboard.ins.bulletManager.ResetBullets(); //game
  112. }
  113. else if (GeneratingTarget.gm)
  114. { //Hyperspace
  115. GeneratingTarget.gm.OnLoading();
  116. }
  117. else
  118. {
  119. OnGameUpdateTheMagazine?.Invoke(); //水果在gamingmanager
  120. }
  121. }
  122. }
  123. void Log(string text)
  124. {
  125. if (this.text)
  126. {
  127. this.text.text = text;
  128. } else {
  129. Debug.Log(text);
  130. }
  131. }
  132. }