AimHandler.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. if (m_axisHandler.GetType() == typeof(Axis9Handler)) return 9;
  43. if (m_axisHandler.GetType() == typeof(Axis663Handler)) return 663;
  44. return 0;
  45. }
  46. }
  47. private AxisBaseHandler m_axisHandler;
  48. //陀螺仪校准进度记录
  49. [NonSerialized] public int gyrCalibrateCompleteCount = 0;
  50. [NonSerialized] public int gyrCalibrateTotalCount = 2000;
  51. public static AimHandler ins;
  52. public bool bInitOne = false;
  53. void Start()
  54. {
  55. ins = this;
  56. BluetoothDispatcher.aim = OnDataReceived;
  57. // m_axisHandler = new Axis9NopackHandler(this); //九轴旧
  58. m_axisHandler = new Axis9Handler(this); //九轴新
  59. // m_axisHandler = new Axis663Handler(this); //双陀螺仪
  60. m_axisHandler.Init();
  61. // StartCoroutine(TestRecord());
  62. }
  63. //public void onClearAimDeviceInfos() {
  64. // PlayerPrefs.DeleteKey("aim-device-info-" + LoginMgr.myUserInfo.id);
  65. // aimDeviceInfos.arry.Clear();
  66. //}
  67. public void OnGetAimDeviceInfos() {
  68. string deviceInfo = PlayerPrefs.GetString("aim-device-info-" + LoginMgr.myUserInfo.id, "");
  69. if (deviceInfo != "")
  70. {
  71. aimDeviceInfos = JsonUtility.FromJson<AimDeviceInfos>(deviceInfo);//这里的类是依据最外层{}决定的
  72. }
  73. aimDeviceInfoChangeEvent?.Invoke();
  74. }
  75. public void OnSaveAimDeviceInfos()
  76. {
  77. aimDeviceInfoChangeEvent?.Invoke();
  78. PlayerPrefs.SetString("aim-device-info-" + LoginMgr.myUserInfo.id, JsonUtility.ToJson(aimDeviceInfos));
  79. }
  80. //是否存在AimDeviceInfo,存在更新,不存在创建;
  81. public void onCreateAimDeviceInfoById(int _id)
  82. {
  83. OnGetAimDeviceInfos();
  84. //deviceIndex 区分 1p 还是 2p
  85. bool bCanAdd = true;
  86. foreach (AimDeviceInfo p in aimDeviceInfos.arry)
  87. {
  88. if (_id == p.id)
  89. {
  90. aimDeviceInfo = p;
  91. bCanAdd = false;
  92. }
  93. Debug.Log("bCanAdd:"+ bCanAdd+ " , id:" + p.id +" , type:"+ p.type);
  94. }
  95. if (bCanAdd)
  96. {
  97. aimDeviceInfo = new AimDeviceInfo(_id);
  98. aimDeviceInfos.arry.Add(aimDeviceInfo);
  99. }
  100. OnSaveAimDeviceInfos();
  101. }
  102. public void SetAimDeviceType(AimDeviceType _aimDeviceType)
  103. {
  104. if (aimDeviceInfo == null) return;
  105. aimDeviceInfo.type = (int)_aimDeviceType;
  106. for (int i = 0; i < aimDeviceInfos.arry.Count; i++)
  107. {
  108. if (aimDeviceInfo.id == aimDeviceInfos.arry[i].id)
  109. {
  110. aimDeviceInfos.arry[i] = aimDeviceInfo;
  111. }
  112. }
  113. OnSaveAimDeviceInfos();
  114. }
  115. public void ReinitAxisHandler()
  116. {
  117. Debug.Log("ReinitAxisHandler");
  118. m_axisHandler.Init();
  119. }
  120. // System.Collections.IEnumerator TestRecord() {
  121. // while (LoginMgr.myUserInfo.id == 0) yield return null;
  122. // UserComp.Instance.saveMac();
  123. // }
  124. [NonSerialized] private Quaternion newRotation = Quaternion.identity;
  125. public void SetNewRotation(Quaternion quat) {
  126. this.newRotation = quat;
  127. }
  128. public void SetNewRotation(o0.Geometry.Quaternion o0Quat) {
  129. Quaternion quat = o0.Bow.Extension.ToUnityQuaternion(o0Quat);
  130. if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w)) {
  131. Debug.LogError($"九轴Rotation存在Nan值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  132. return;
  133. }
  134. if (float.IsInfinity(quat.x) || float.IsInfinity(quat.y) || float.IsInfinity(quat.z) || float.IsInfinity(quat.w)) {
  135. Debug.LogError($"九轴Rotation存在Infinity值,double:{o0Quat.ToString()},float:{quat.ToString()}");
  136. return;
  137. }
  138. this.newRotation = quat;
  139. }
  140. [NonSerialized] public bool lerpForRotation = true;
  141. [NonSerialized] public float lerpTimeRate = 7;
  142. public void Update()
  143. {
  144. if (m_controlObj && !m_ban9AxisCalculate)
  145. {
  146. if (lerpForRotation)
  147. m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate);
  148. else
  149. m_controlObj.localRotation = newRotation;
  150. }
  151. if (IsMagCompleted())
  152. {
  153. if (!m_magCompleted)
  154. {
  155. StartCoroutine(SaveMag());
  156. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_success"));
  157. }
  158. m_magCompleted = true;
  159. } else {
  160. m_magCompleted = false;
  161. }
  162. if (Input.GetKeyDown(KeyCode.Space))
  163. {
  164. AutoResetView.DoIdentity();
  165. }
  166. }
  167. public void OnDataReceived(byte[] bytes)
  168. {
  169. // Debug.Log("瞄准模块数据长度" + bytes.Length);
  170. if (bytes.Length != 27 && bytes.Length != 39)
  171. {
  172. if (bytes.Length == 2) {
  173. if (bytes[0] == 0x66 && bytes[1] == 0x31) {
  174. if (bInitOne)
  175. {
  176. bInitOne = false;
  177. AutoResetView.DoIdentity();
  178. }
  179. else {
  180. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
  181. {
  182. //视角回正
  183. DoIdentity();
  184. //鼠标居中自然会居中
  185. }
  186. else
  187. {
  188. AutoResetView.DoIdentity();
  189. }
  190. }
  191. } else if (bytes[0] == 0x66 && bytes[1] == 0x32) {
  192. if (SB_EventSystem.ins) {
  193. // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
  194. //唤起/隐藏虚拟鼠标
  195. SB_EventSystem.ins.AwakenSimulateMouse();
  196. }
  197. } else if (bytes[1] == 10) {
  198. //显示电量
  199. DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
  200. //DeviceView.ins.RenderBattery(1, bytes[0]);
  201. }
  202. } else if (bytes[0] == 0x5b) {
  203. //红外射击检测
  204. ShootCheck.ins.ShootByInfrared(bytes);
  205. }
  206. return;
  207. }
  208. m_axisHandler.Update(bytes);
  209. if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles);
  210. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(newRotation);
  211. }
  212. private bool m_magCompleted;
  213. public void CorrectMagCompleted(bool value) { m_magCompleted = value; }
  214. private bool m_ban9AxisCalculate = false;
  215. public void Ban9AxisCalculate(bool ban)
  216. {
  217. // m_ban9AxisCalculate = ban;
  218. // if (!ban)
  219. // {
  220. // SetMsOldDefault();
  221. // if (m_controlObj) m_controlObj.localRotation = newRotation;
  222. // }
  223. }
  224. public void SetMsOldDefault()
  225. {
  226. if (m_axisHandler.GetType() == typeof(Axis9NopackHandler))
  227. (m_axisHandler as Axis9NopackHandler).msOld = default;
  228. }
  229. public void DoIdentity()
  230. {
  231. m_axisHandler.DoIdentity();
  232. if (m_controlObj) m_controlObj.localRotation = newRotation;
  233. }
  234. public void NotifyAxisOnShot() { m_axisHandler.NotifyAxisOnShot(); }
  235. public void CalibrateGyr(bool calibration) { m_axisHandler.CalibrateGyr(calibration); }
  236. public void InitGyr(string record) { m_axisHandler.InitGyr(record); }
  237. public void InitMag(string record) { m_axisHandler.InitMag(record); }
  238. public void ResetGyr() { m_axisHandler.ResetGyr(); }
  239. public void ResetMag() { m_axisHandler.ResetMag(); }
  240. public void ApplyImpreciseMag() { m_axisHandler.ApplyImpreciseMag(); }
  241. public bool IsGyrCompleted() { return m_axisHandler.IsGyrCompleted(); }
  242. public bool IsMagCompleted() { return m_axisHandler.IsMagCompleted(); }
  243. public IEnumerator SaveGyr() { return m_axisHandler.SaveGyr(); }
  244. public IEnumerator SaveMag() { return m_axisHandler.SaveMag(); }
  245. public void ResumeCalibrateRecord(string record) { m_axisHandler.ResumeCalibrateRecord(record); }
  246. }