AimHandler_SDK.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace SmartBowSDK
  5. {
  6. /* 瞄准处理器 */
  7. public class AimHandler_SDK : MonoBehaviour
  8. {
  9. public SmartBowHelper smartBowHelper;
  10. private Quaternion _gameRotation = Quaternion.identity;
  11. public Quaternion gameRotation
  12. {
  13. get => _gameRotation;
  14. set
  15. {
  16. _gameRotation = value;
  17. //去掉z轴影响
  18. Vector3 normalVector = _gameRotation * Vector3.forward;
  19. Quaternion normalQuat = Quaternion.LookRotation(normalVector);
  20. //通知外部监听函数
  21. smartBowHelper.InvokeOnRotationUpdate(normalQuat);
  22. }
  23. }
  24. private AxisBaseHandler_SDK m_axisHandler;
  25. void Start()
  26. {
  27. m_axisHandler = new Axis9Handler_SDK(this);
  28. m_axisHandler.Init();
  29. }
  30. public void ReinitAxisHandler()
  31. {
  32. m_axisHandler.Init();
  33. }
  34. Quaternion newRotation = Quaternion.identity;
  35. public void SetNewRotation(o0.Geometry.Quaternion o0Quat)
  36. {
  37. Quaternion quat = o0.Bow.Extension.ToUnityQuaternion(o0Quat);
  38. if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w))
  39. {
  40. Debug.LogError($"九轴Rotation存在Nan值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  41. return;
  42. }
  43. if (float.IsInfinity(quat.x) || float.IsInfinity(quat.y) || float.IsInfinity(quat.z) || float.IsInfinity(quat.w))
  44. {
  45. Debug.LogError($"九轴Rotation存在Infinity值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  46. return;
  47. }
  48. newRotation = quat;
  49. }
  50. public void Update()
  51. {
  52. gameRotation = Quaternion.Lerp(gameRotation, newRotation, Time.deltaTime * 7);
  53. }
  54. public void OnDataReceived(byte[] bytes)
  55. {
  56. if (bytes.Length != 27 && bytes.Length != 39)
  57. {
  58. if (bytes.Length == 2)
  59. {
  60. if (bytes[0] == 0x66 && bytes[1] == 0x31)
  61. {
  62. //短按功能键
  63. smartBowHelper.InvokeOnFunctionKeyPress();
  64. }
  65. else if (bytes[0] == 0x66 && bytes[1] == 0x32)
  66. {
  67. //长按功能键
  68. smartBowHelper.InvokeOnFunctionKeyLongPress();
  69. }
  70. else if (bytes[1] == 10)
  71. {
  72. //电量
  73. smartBowHelper.bluetoothAim.battery = bytes[0];
  74. }
  75. }
  76. else if (bytes[0] == 0x5b)
  77. {
  78. //红外线检测到射箭
  79. smartBowHelper.shootChecker.OnInfraredDataReceived(bytes);
  80. }
  81. else if (bytes[0] == 0x5a)
  82. {
  83. //手枪应答
  84. smartBowHelper.shootChecker.ReplyGun(bytes);
  85. }
  86. else if (bytes[0] == 0x5C)
  87. {
  88. //弹夹变化
  89. smartBowHelper.shootChecker.UpdateTheMagazine(bytes);
  90. }
  91. else if (bytes[0] == 0x60) {
  92. //枪械是否上膛
  93. smartBowHelper.shootChecker.UpdateChamberState(bytes);
  94. }
  95. else if (bytes[0] == 0x5E)
  96. {
  97. //接收到系统数据
  98. smartBowHelper.shootChecker.UpdateDeviceAndSysInfo(bytes);
  99. }
  100. return;
  101. }
  102. m_axisHandler.Update(bytes);
  103. }
  104. public void DoIdentity()
  105. {
  106. try { m_axisHandler.DoIdentity(); } catch (Exception e) { Debug.LogWarning("DoIdentity:" + e.Message); }
  107. gameRotation = newRotation;
  108. }
  109. public void NotifyAxisOnShot() { m_axisHandler.NotifyAxisOnShot(); }
  110. public void StartGyrCalibration() { m_axisHandler.StartGyrCalibration(); }
  111. public void StopGyrCalibration() { m_axisHandler.StopGyrCalibration(); }
  112. public bool IsGyrCalibrating() { return m_axisHandler.IsGyrCalibrating(); }
  113. public float GetGyrProgress() { return m_axisHandler.GetGyrProgress(); }
  114. public bool IsGyrCompleted() { return m_axisHandler.IsGyrCompleted(); }
  115. public void StartMagCalibration() { m_axisHandler.StartMagCalibration(); }
  116. public bool IsMagCompleted() { return m_axisHandler.IsMagCompleted(); }
  117. public void ResumeCalibrateRecord(string record) { m_axisHandler.ResumeCalibrateRecord(record); }
  118. }
  119. }