AimHandler.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. /* 瞄准处理器 */
  5. public class AimHandler : MonoBehaviour
  6. {
  7. Transform m_controlObj {
  8. get {
  9. if (CameraToLook.ins) {
  10. return CameraToLook.ins.transform;
  11. }
  12. return null;
  13. }
  14. }
  15. public int DeviceType
  16. {
  17. get
  18. {
  19. if (m_axisHandler.GetType() == typeof(Axis9Handler)) return 9;
  20. if (m_axisHandler.GetType() == typeof(Axis663Handler)) return 663;
  21. return 0;
  22. }
  23. }
  24. private AxisBaseHandler m_axisHandler;
  25. //陀螺仪校准进度记录
  26. [NonSerialized] public int gyrCalibrateCompleteCount = 0;
  27. [NonSerialized] public int gyrCalibrateTotalCount = 2000;
  28. public static AimHandler ins;
  29. void Start()
  30. {
  31. ins = this;
  32. BluetoothDispatcher.aim = OnDataReceived;
  33. // m_axisHandler = new Axis9NopackHandler(this); //九轴旧
  34. m_axisHandler = new Axis9Handler(this); //九轴新
  35. // m_axisHandler = new Axis663Handler(this); //双陀螺仪
  36. m_axisHandler.Init();
  37. // StartCoroutine(TestRecord());
  38. }
  39. public void ReinitAxisHandler()
  40. {
  41. Debug.Log("ReinitAxisHandler");
  42. m_axisHandler.Init();
  43. }
  44. // System.Collections.IEnumerator TestRecord() {
  45. // while (LoginMgr.myUserInfo.id == 0) yield return null;
  46. // UserComp.Instance.saveMac();
  47. // }
  48. [NonSerialized] private Quaternion newRotation = Quaternion.identity;
  49. public void SetNewRotation(Quaternion quat) {
  50. this.newRotation = quat;
  51. }
  52. public void SetNewRotation(o0.Geometry.Quaternion o0Quat) {
  53. Quaternion quat = o0.Bow.Extension.ToUnityQuaternion(o0Quat);
  54. if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w)) {
  55. Debug.LogError($"九轴Rotation存在Nan值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  56. return;
  57. }
  58. if (float.IsInfinity(quat.x) || float.IsInfinity(quat.y) || float.IsInfinity(quat.z) || float.IsInfinity(quat.w)) {
  59. Debug.LogError($"九轴Rotation存在Infinity值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  60. return;
  61. }
  62. this.newRotation = quat;
  63. }
  64. [NonSerialized] public bool lerpForRotation = true;
  65. [NonSerialized] public float lerpTimeRate = 7;
  66. public void Update()
  67. {
  68. if (m_controlObj && !m_ban9AxisCalculate)
  69. {
  70. if (lerpForRotation)
  71. m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate);
  72. else
  73. m_controlObj.localRotation = newRotation;
  74. }
  75. if (IsMagCompleted())
  76. {
  77. if (!m_magCompleted)
  78. {
  79. StartCoroutine(SaveMag());
  80. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_success"));
  81. }
  82. m_magCompleted = true;
  83. } else {
  84. m_magCompleted = false;
  85. }
  86. }
  87. public void OnDataReceived(byte[] bytes)
  88. {
  89. // Debug.Log("瞄准模块数据长度" + bytes.Length);
  90. if (bytes.Length != 27 && bytes.Length != 39)
  91. {
  92. if (bytes.Length == 2) {
  93. if (bytes[0] == 0x66 && bytes[1] == 0x31) {
  94. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
  95. //视角回正
  96. DoIdentity();
  97. //鼠标居中自然会居中
  98. } else {
  99. AutoResetView.DoIdentity();
  100. }
  101. } else if (bytes[0] == 0x66 && bytes[1] == 0x32) {
  102. if (SB_EventSystem.ins) {
  103. // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
  104. //唤起/隐藏虚拟鼠标
  105. SB_EventSystem.ins.AwakenSimulateMouse();
  106. }
  107. } else if (bytes[1] == 10) {
  108. //显示电量
  109. DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
  110. }
  111. } else if (bytes[0] == 0x5b) {
  112. //红外射击检测
  113. ShootCheck.ins.ShootByInfrared(bytes);
  114. }
  115. return;
  116. }
  117. m_axisHandler.Update(bytes);
  118. if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles);
  119. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(newRotation);
  120. }
  121. private bool m_magCompleted;
  122. public void CorrectMagCompleted(bool value) { m_magCompleted = value; }
  123. private bool m_ban9AxisCalculate = false;
  124. public void Ban9AxisCalculate(bool ban)
  125. {
  126. m_ban9AxisCalculate = ban;
  127. if (!ban)
  128. {
  129. SetMsOldDefault();
  130. if (m_controlObj) m_controlObj.localRotation = newRotation;
  131. }
  132. }
  133. public void SetMsOldDefault()
  134. {
  135. if (m_axisHandler.GetType() == typeof(Axis9NopackHandler))
  136. (m_axisHandler as Axis9NopackHandler).msOld = default;
  137. }
  138. public void DoIdentity()
  139. {
  140. m_axisHandler.DoIdentity();
  141. if (m_controlObj) m_controlObj.localRotation = newRotation;
  142. }
  143. public void NotifyAxisOnShot() { m_axisHandler.NotifyAxisOnShot(); }
  144. public void CalibrateGyr(bool calibration) { m_axisHandler.CalibrateGyr(calibration); }
  145. public void InitGyr(string record) { m_axisHandler.InitGyr(record); }
  146. public void InitMag(string record) { m_axisHandler.InitMag(record); }
  147. public void ResetGyr() { m_axisHandler.ResetGyr(); }
  148. public void ResetMag() { m_axisHandler.ResetMag(); }
  149. public bool IsGyrCompleted() { return m_axisHandler.IsGyrCompleted(); }
  150. public bool IsMagCompleted() { return m_axisHandler.IsMagCompleted(); }
  151. public IEnumerator SaveGyr() { return m_axisHandler.SaveGyr(); }
  152. public IEnumerator SaveMag() { return m_axisHandler.SaveMag(); }
  153. public void ResumeCalibrateRecord(string record) { m_axisHandler.ResumeCalibrateRecord(record); }
  154. }