AimHandler.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public enum AimDeviceType {
  6. NONE = -1,
  7. HOUYI = 0,
  8. HOUYI2 = 1,
  9. ARTEMIS = 2,
  10. }
  11. [Serializable]//需要在转换为json格式的类的上方添加序列化
  12. public class AimDeviceInfo
  13. {
  14. public int id;
  15. public int type = -1;//类型 AimDeviceType
  16. public int pos; //位置
  17. public AimDeviceInfo(int _id) {
  18. this.id = _id;
  19. }
  20. }
  21. [Serializable]
  22. public class AimDeviceInfos
  23. {
  24. public List<AimDeviceInfo> arry;
  25. }
  26. /* 瞄准处理器 */
  27. public class AimHandler : MonoBehaviour
  28. {
  29. CameraToLook m_controlObj {
  30. get {
  31. return CameraToLook.ins;
  32. }
  33. }
  34. //记录一个设备
  35. public AimDeviceInfo aimDeviceInfo = null;
  36. public AimDeviceInfos aimDeviceInfos = new AimDeviceInfos();
  37. public Action aimDeviceInfoChangeEvent;
  38. public int DeviceType
  39. {
  40. get
  41. {
  42. return 0;
  43. }
  44. }
  45. //陀螺仪校准进度记录
  46. [NonSerialized] public int gyrCalibrateCompleteCount = 0;
  47. [NonSerialized] public int gyrCalibrateTotalCount = 2000;
  48. public static AimHandler ins;
  49. public bool bInitOne = false;
  50. void Start()
  51. {
  52. ins = this;
  53. BluetoothDispatcher.aim = OnDataReceived;
  54. // StartCoroutine(TestRecord());
  55. }
  56. public void OnGetAimDeviceInfos() {
  57. aimDeviceInfoChangeEvent?.Invoke();
  58. }
  59. public void OnSaveAimDeviceInfos()
  60. {
  61. aimDeviceInfoChangeEvent?.Invoke();
  62. }
  63. //是否存在AimDeviceInfo,存在更新,不存在创建;
  64. public void onCreateAimDeviceInfoById(int _id)
  65. {
  66. OnGetAimDeviceInfos();
  67. //deviceIndex 区分 1p 还是 2p
  68. bool bCanAdd = true;
  69. foreach (AimDeviceInfo p in aimDeviceInfos.arry)
  70. {
  71. if (_id == p.id)
  72. {
  73. aimDeviceInfo = p;
  74. bCanAdd = false;
  75. }
  76. Debug.Log("bCanAdd:"+ bCanAdd+ " , id:" + p.id +" , type:"+ p.type);
  77. }
  78. if (bCanAdd)
  79. {
  80. aimDeviceInfo = new AimDeviceInfo(_id);
  81. aimDeviceInfos.arry.Add(aimDeviceInfo);
  82. }
  83. OnSaveAimDeviceInfos();
  84. }
  85. public void SetAimDeviceType(AimDeviceType _aimDeviceType)
  86. {
  87. if (aimDeviceInfo == null) return;
  88. aimDeviceInfo.type = (int)_aimDeviceType;
  89. for (int i = 0; i < aimDeviceInfos.arry.Count; i++)
  90. {
  91. if (aimDeviceInfo.id == aimDeviceInfos.arry[i].id)
  92. {
  93. aimDeviceInfos.arry[i] = aimDeviceInfo;
  94. }
  95. }
  96. OnSaveAimDeviceInfos();
  97. }
  98. public void ReinitAxisHandler()
  99. {
  100. Debug.Log("ReinitAxisHandler");
  101. }
  102. // System.Collections.IEnumerator TestRecord() {
  103. // while (LoginMgr.myUserInfo.id == 0) yield return null;
  104. // UserComp.Instance.saveMac();
  105. // }
  106. [NonSerialized] private Quaternion newRotation = Quaternion.identity;
  107. public void SetNewRotation(Quaternion quat) {
  108. this.newRotation = quat;
  109. }
  110. public void SetNewRotation(o0.Geometry.Quaternion o0Quat) {
  111. Quaternion quat = o0.Bow.Extension.ToUnityQuaternion(o0Quat);
  112. if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w)) {
  113. Debug.LogError($"九轴Rotation存在Nan值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  114. return;
  115. }
  116. if (float.IsInfinity(quat.x) || float.IsInfinity(quat.y) || float.IsInfinity(quat.z) || float.IsInfinity(quat.w)) {
  117. Debug.LogError($"九轴Rotation存在Infinity值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  118. return;
  119. }
  120. this.newRotation = quat;
  121. }
  122. [NonSerialized] public bool lerpForRotation = true;
  123. [NonSerialized] public float lerpTimeRate = 7;
  124. public void Update()
  125. {
  126. if (m_controlObj && !m_ban9AxisCalculate)
  127. {
  128. if (lerpForRotation)
  129. m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate);
  130. else
  131. m_controlObj.localRotation = newRotation;
  132. }
  133. if (Input.GetKeyDown(KeyCode.Space))
  134. {
  135. AutoResetView.DoIdentity();
  136. }
  137. }
  138. public void OnDataReceived(byte[] bytes)
  139. {
  140. // Debug.Log("瞄准模块数据长度" + bytes.Length);
  141. if (bytes.Length != 27 && bytes.Length != 39)
  142. {
  143. if (bytes.Length == 2) {
  144. if (bytes[0] == 0x66 && bytes[1] == 0x31) {
  145. if (bInitOne)
  146. {
  147. bInitOne = false;
  148. AutoResetView.DoIdentity();
  149. }
  150. else {
  151. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  152. {
  153. //视角回正
  154. DoIdentity();
  155. //鼠标居中自然会居中
  156. }
  157. else
  158. {
  159. AutoResetView.DoIdentity();
  160. }
  161. }
  162. } else if (bytes[0] == 0x66 && bytes[1] == 0x32) {
  163. if (SB_EventSystem.ins) {
  164. // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
  165. //唤起/隐藏虚拟鼠标
  166. SB_EventSystem.ins.AwakenSimulateMouse();
  167. }
  168. } else if (bytes[1] == 10) {
  169. //显示电量
  170. DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
  171. //DeviceView.ins.RenderBattery(1, bytes[0]);
  172. }
  173. } else if (bytes[0] == 0x5b) {
  174. //红外射击检测
  175. ShootCheck.ins.ShootByInfrared(bytes);
  176. }
  177. return;
  178. }
  179. //if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles);
  180. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(newRotation);
  181. }
  182. private bool m_magCompleted;
  183. public void CorrectMagCompleted(bool value) { m_magCompleted = value; }
  184. private bool m_ban9AxisCalculate = false;
  185. public void Ban9AxisCalculate(bool ban)
  186. {
  187. // m_ban9AxisCalculate = ban;
  188. // if (!ban)
  189. // {
  190. // SetMsOldDefault();
  191. // if (m_controlObj) m_controlObj.localRotation = newRotation;
  192. // }
  193. }
  194. public void SetMsOldDefault()
  195. {
  196. }
  197. public void DoIdentity()
  198. {
  199. if (m_controlObj) m_controlObj.localRotation = newRotation;
  200. }
  201. }