AimHandler.cs 6.1 KB

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