ShootCheck.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using UnityEngine;
  3. /* 射箭检测 */
  4. public class ShootCheck : MonoBehaviour
  5. {
  6. public Action<float> onShoot;
  7. private float _lastNotifyTime_OnShoot;
  8. const float arrowWeight = 75;
  9. const float actualArrowWeight = 20;
  10. [NonSerialized] public float shootSpeed;
  11. public static ShootCheck ins;
  12. void Awake()
  13. {
  14. ins = this;
  15. }
  16. [NonSerialized] public byte byteTime1;
  17. [NonSerialized] public byte byteTime2;
  18. /**通过红外线数据进行射击 */
  19. public void ShootByInfrared(byte[] bytes) {
  20. byteTime1 = bytes[2];
  21. byteTime1 = bytes[3];
  22. int id = bytes[1]; //序号
  23. float time1 = bytes[2] * 0.1f; //时区1耗时
  24. float time2 = bytes[3] * 0.1f; //时区2耗时
  25. float totalTime = time1 + time2;
  26. // if (totalTime <= 0) {
  27. // totalTime = 0.3f;
  28. // }
  29. //校验和
  30. int sumCheck = bytes[0] + bytes[1] + bytes[2] + bytes[3];
  31. sumCheck &= 0xff;
  32. //校验和比较结果
  33. bool sumCheckRes = sumCheck == bytes[4];
  34. //弓轨速度
  35. float speed = 0.05f / (totalTime / 1000f);
  36. //通过动能定理求箭的速度(实体箭质量*实体箭速度^2=游戏中箭的质量*游戏中箭的速度^2)
  37. shootSpeed = Mathf.Sqrt(speed * speed * arrowWeight / actualArrowWeight);
  38. //打印
  39. //string logTxt = $"序号{id},时区1:{time1}毫秒,时区2:{time2}毫秒,校验:{sumCheckRes},弓轨速度:{speed}m/s,箭的速度:{shootSpeed}m/s";
  40. //收到射箭数据,就回复硬件,否则n毫秒后硬件会认为丢包进行重传
  41. try {
  42. if (sumCheckRes) BluetoothAim.ins.ReplyInfraredShoot(); //如果数据正确,则回复硬件
  43. } catch (Exception) {}
  44. //调用游戏中的射箭接口
  45. AimHandler.ins._9Axis.axisCSBridge.o09AxisCS.OnShot(100, 100, 100000);
  46. //if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
  47. //SB_EventSystem.ins.ClickMouse();
  48. //} else if (ArmBow.ins) { #undetermined
  49. if (Time.realtimeSinceStartup - _lastNotifyTime_OnShoot > 1.2f)
  50. {
  51. _lastNotifyTime_OnShoot = Time.realtimeSinceStartup;
  52. onShoot?.Invoke(shootSpeed);
  53. }
  54. //} #undetermined
  55. }
  56. }