| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- using System;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- public enum AimDeviceType {
- NONE = -1,
- HOUYIPRO = 3,
- //枪械类,现在默认是M9
- Gun = 4,
- ARTEMISPRO = 5,
- //枪械类:新增设备
- PistolM17 = 6,
- RifleM416 = 7
- }
- [Serializable]//需要在转换为json格式的类的上方添加序列化
- public class AimDeviceInfo
- {
- public int id;
- public int type = -1;//类型 AimDeviceType
- public int pos; //位置
- public bool bInitMac = false; //是否初始化Mac
- public string mac; //记录当前
- public AimDeviceInfo(int _id) {
- this.id = _id;
- }
- public void setInitMac(string macTemp) {
- bInitMac = true;
- mac = macTemp;
- }
- public void resetInitMac() {
- bInitMac = false;
- mac = "";
- }
- }
- [Serializable]
- public class AimDeviceInfos
- {
- public List<AimDeviceInfo> arry;
- }
- /* 瞄准处理器 */
- public class AimHandler : MonoBehaviour
- {
- //记录一个设备
- public AimDeviceInfo aimDeviceInfo = null;
- public AimDeviceInfo tempAimDeviceInfo = null;//临时处理值。最后赋值给aimDeviceInfo
- public AimDeviceInfos aimDeviceInfos = new AimDeviceInfos();
- int deviceSelectIndex = 0;//默认选中第一个设备(1p)
- public Action aimDeviceInfoChangeEvent;
- public int DeviceType
- {
- get
- {
- //if (m_axisHandler.GetType() == typeof(Axis9Handler)) return 9;
- //if (m_axisHandler.GetType() == typeof(Axis663Handler)) return 663;
- return 0;
- }
- }
- //陀螺仪校准进度记录
- [NonSerialized] public int gyrCalibrateCompleteCount = 0;
- [NonSerialized] public int gyrCalibrateTotalCount = 2000;
- public static AimHandler ins;
- public bool bInitOne = false;
- public OnCrossBtnEventEvent OnCrossBtnEvent;
- public delegate void OnCrossBtnEventEvent();
- /// <summary>
- /// 准心事件
- /// </summary>
- public void InvokeOnCrossBtnEvent()
- {
- try
- {
- OnCrossBtnEvent?.Invoke();
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- void Start()
- {
- ins = this;
- }
- public void onClearAimDeviceInfosNew()
- {
- PlayerPrefs.DeleteKey("aim-device-info-" + LoginMgr.myUserInfo.id);
- aimDeviceInfos.arry.Clear();
- }
- /// <summary>
- /// 移除2p。即除了1p之后的设备
- /// </summary>
- public void onClear2PAimDeviceInfosNew()
- {
- OnGetAimDeviceInfos();
- if (aimDeviceInfos.arry.Count > 1)
- {
- aimDeviceInfos.arry.RemoveRange(1, aimDeviceInfos.arry.Count - 1);
- }
- OnSaveAimDeviceInfos();
- }
- public void OnGetAimDeviceInfos() {
- string deviceInfo = PlayerPrefs.GetString("aim-device-info-" + LoginMgr.myUserInfo.id, "");
- //Debug.Log("get deviceSelectIndex:" + deviceInfo);
- if (deviceInfo != "")
- {
- aimDeviceInfos = JsonUtility.FromJson<AimDeviceInfos>(deviceInfo);//这里的类是依据最外层{}决定的
- }
- aimDeviceInfoChangeEvent?.Invoke();
- }
- public void OnSaveAimDeviceInfos()
- {
- aimDeviceInfoChangeEvent?.Invoke();
- PlayerPrefs.SetString("aim-device-info-" + LoginMgr.myUserInfo.id, JsonUtility.ToJson(aimDeviceInfos));
- }
- //创建一个选择值
- public void onCreateTempAimDeviceInfo() {
- tempAimDeviceInfo = new AimDeviceInfo(deviceSelectIndex);
- Debug.Log("onCreateTempAimDeviceInfo deviceSelectIndex:" + deviceSelectIndex);
- }
- //是否存在AimDeviceInfo,存在更新,不存在创建;
- public void onCreateAimDeviceInfoById()
- {
- OnGetAimDeviceInfos();
- //deviceIndex 区分 1p 还是 2p
- bool bCanAdd = true;
- foreach (AimDeviceInfo p in aimDeviceInfos.arry)
- {
- if (deviceSelectIndex == p.id)
- {
- aimDeviceInfo = p;
- bCanAdd = false;
- }
- //Debug.Log("33deviceSelectIndex:" + deviceSelectIndex + ",bCanAdd:" + bCanAdd+ " , id:" + p.id +" , type:"+ p.type);
- }
- if (bCanAdd)
- {
- //Debug.Log("add deviceSelectIndex:" + deviceSelectIndex);
- aimDeviceInfo = new AimDeviceInfo(deviceSelectIndex);
- aimDeviceInfos.arry.Add(aimDeviceInfo);
- }
- OnSaveAimDeviceInfos();
- }
- public void SetAimDeviceSelectIndex(int selectIndex) {
- if (selectIndex < 0) {
- Debug.LogWarning("selectIndex不能小于0:"+ selectIndex);
- return;
- }
- deviceSelectIndex = selectIndex;
- //Debug.Log("SetAimDeviceSelectIndex :" + selectIndex);
- }
- public void SetTempAimDeviceType(AimDeviceType _aimDeviceType)
- {
- if (tempAimDeviceInfo == null) return;
- tempAimDeviceInfo.type = (int)_aimDeviceType;
- }
- public void SetAimDeviceType(AimDeviceType _aimDeviceType)
- {
- if (aimDeviceInfo == null) return;
- aimDeviceInfo.type = (int)_aimDeviceType;
- _AimDeviceInfosUpdate();
- OnSaveAimDeviceInfos();
- }
- public void SetAimDeviceType(int _aimDeviceType)
- {
- if (aimDeviceInfo == null) return;
- aimDeviceInfo.type = _aimDeviceType;
- _AimDeviceInfosUpdate();
- OnSaveAimDeviceInfos();
- }
- //APP连接需进行MAC地址的比对。
- //只有再引导初始化页面才保存连接使用的mac地址。
- public void SetAimDeviceMac(string _mac) {
- if (aimDeviceInfo == null) return;
- aimDeviceInfo.setInitMac(_mac);
- _AimDeviceInfosUpdate();
- OnSaveAimDeviceInfos();
- }
- //重置mac存储信息
- public void ResetAimDeviceMac() {
- if (aimDeviceInfo == null) return;
- aimDeviceInfo.resetInitMac();
- _AimDeviceInfosUpdate();
- OnSaveAimDeviceInfos();
- }
- void _AimDeviceInfosUpdate() {
- for (int i = 0; i < aimDeviceInfos.arry.Count; i++)
- {
- if (aimDeviceInfo.id == aimDeviceInfos.arry[i].id)
- {
- aimDeviceInfos.arry[i] = aimDeviceInfo;
- }
- }
- }
- #region 根据设备1p或者2p 获取HOUYIPRO的状态
- public bool isHOUYIPRO(BluetoothPlayer bluetoothPlayer)
- {
- bool _isHOUYIPRO = false;
- foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry)
- {
- if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.HOUYIPRO)
- {
- _isHOUYIPRO = true;
- }
- }
- return _isHOUYIPRO;
- }
- #endregion
- #region 根据设备1p或者2p 获取ARTEMISPro的状态
- public bool isARTEMISPro(BluetoothPlayer bluetoothPlayer)
- {
- bool _isARTEMISPro = false;
- foreach (AimDeviceInfo p in ins.aimDeviceInfos.arry)
- {
- if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.ARTEMISPRO)
- {
- _isARTEMISPro = true;
- }
- }
- return _isARTEMISPro;
- }
- #endregion
- [NonSerialized] public bool lerpForRotation = true;
- [NonSerialized] public float lerpTimeRate = 7;
- //加一个枪射击的触发时间
- float _lastShootTime = 0;
- public void Update()
- {
- }
- public void OnDataReceived(byte[] bytes)
- {
- if (bytes.Length != 27 && bytes.Length != 39)
- {
- if (bytes.Length == 2)
- {
- if (bytes[0] == 0x66 && bytes[1] == 0x31)
- {
- if (bInitOne)
- {
- bInitOne = false;
- if (aimDeviceInfo.type == (int)AimDeviceType.NONE)
- {
- AutoResetView.DoIdentity();
- }
- else
- {
- //准心开关
- CrossBtnEvent();
- }
- }
- else
- {
- if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
- {
- //流程触发红外描点
- InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType<InfraredScreenPositioningView>();
- if (infraredScreenPositioningView)
- {
- InvokeOnCrossBtnEvent();
- }
- }
- else
- {
- if (aimDeviceInfo.type == (int)AimDeviceType.NONE)
- {
- AutoResetView.DoIdentity();
- }
- else
- {
- //准心开关
- CrossBtnEvent();
- }
- }
- }
- }
- else if (bytes[0] == 0x66 && bytes[1] == 0x32)
- {
- //红外部分
- if (GlobalData.MyDeviceMode == DeviceMode.Gun || BluetoothAim.ins && BluetoothAim.ins.isMainConnectToInfraredDevice())
- {
- InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
- //‘校准'功能的调用,使用原来调出光标的功能键;即在HOUYI Pro使用长按视角归位键;在ARTEMIS Pro使用快速双击按键
- if (infraredGuider == null)
- {
- AutoResetView.DoIdentity();
- }
- else
- {
- //先判断是否处于校准步骤
- infraredGuider.onLongClickDevice();
- }
- }
- else if (SB_EventSystem.ins)
- {
- // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) {
- //唤起/隐藏虚拟鼠标
- SB_EventSystem.ins.AwakenSimulateMouse();
- }
- }
- else if (bytes[1] == 10)
- {
- //显示电量
- DeviceBatteryView.ins.RenderBattery(1, bytes[0]);
- //DeviceView.ins.RenderBattery(1, bytes[0]);
- }
- }
- else if (bytes[0] == 0x5b)
- {
- if (GlobalData.MyDeviceMode == DeviceMode.Gun)
- {
- if (Time.realtimeSinceStartup - _lastShootTime >= 1)
- {
- _lastShootTime = Time.realtimeSinceStartup;
- InfraredScreenPositioningView view = FindAnyObjectByType<InfraredScreenPositioningView>();
- if (view)
- {
- InvokeOnCrossBtnEvent();
- }
- }
- }
- // 无论如何都执行红外射击检测
- ShootCheck.ins.ShootByInfrared(bytes);
- }
- else if (bytes[0] == 0x5C)
- {
- //00 弹夹分离,01 上弹夹
- ShootCheck.ins.UpdateTheMagazine(bytes);
- }
- else if (bytes[0] == 0x60)
- {
- //枪械是否上膛
- ShootCheck.ins.UpdateChamberState(bytes);
- }
- else if (bytes[0] == 0x5E)
- {
- // Debug.Log("接收到系统数据:" + BitConverter.ToString(bytes));
- var boxUserSettings = FindAnyObjectByType<CustomUIView.BoxUserSettings>();
- //设备类型
- switch (bytes[1])
- {
- case 0x01:
- //Debug.Log("设备类型:HOUYI Pro");
- UserSettings.ins.selectDevicesName = "HOUYI Pro";
- UserSettings.ins.Save();
- boxUserSettings?.OnUpdateClickInfoByName();
- break;
- case 0x02:
- // Debug.Log("设备类型:ARTEMIS Pro");
- UserSettings.ins.selectDevicesName = "ARTEMIS Pro";
- UserSettings.ins.Save();
- boxUserSettings?.OnUpdateClickInfoByName();
- break;
- case 0x03:
- // Debug.Log("设备类型:Pistol 1");
- UserSettings.ins.selectDevicesName = "Pistol M9";
- UserSettings.ins.Save();
- boxUserSettings?.OnUpdateClickInfoByName();
- break;
- }
- // 系统类型
- //switch (bytes[2])
- //{
- // case 0x01:
- // Debug.Log("系统类型:移动手机");
- // break;
- // case 0x02:
- // Debug.Log("系统类型:PC电脑");
- // break;
- // case 0x03:
- // Debug.Log("系统类型:VR设备");
- // break;
- //}
- //记录接收到系统数据
- ShootCheck.ins.UpdateDeviceAndSysInfo(bytes);
- }
- return;
- }
- }
- /// <summary>
- /// 操作准心开关事件
- /// </summary>
- private void CrossBtnEvent()
- {
- Debug.Log("CrossBtnEvent");
- //准心开关
- InvokeOnCrossBtnEvent();
- //b端不处理准心
- if (CommonConfig.StandaloneModeOrPlatformB) {
- InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
- if (infraredGuider)
- {
- infraredGuider.onClickDevice();
- }
- return;
- }
- if (GameAssistUI.ins)
- {
- InfraredGuider infraredGuider = FindAnyObjectByType<InfraredGuider>();
- if (infraredGuider == null)
- {
- //显示控制准心按钮
- Transform _button5 = GameAssistUI.ins.transform.Find("Button5");
- if (_button5.gameObject.activeSelf)
- {
- Button crossHairBtn = _button5.GetComponent<Button>();
- crossHairBtn.onClick.Invoke();
- }
- else
- {
- bool onlyShow = !CrossHair.ins.GetOnlyShow();
- CrossHair.ins.SetOnlyShow(onlyShow);
- //保存准心状态
- if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
- }
- }
- else {
-
- infraredGuider.onClickDevice();
- }
- //if (GameMgr.bShowDistance)
- //{
- // //显示控制准心按钮
- // Button crossHairBtn = GameAssistUI.ins.transform.Find("Button5").GetComponent<Button>();
- // crossHairBtn.onClick.Invoke();
- //}
- //else {
- // //校准
- // InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
- //}
- }
- else if (DuckHunter.GameUI.Instance)
- {
- //显示控制准心按钮
- Transform _btnCrosshair = DuckHunter.GameUI.Instance.transform.Find("BtnCrosshair");
- if (_btnCrosshair.gameObject.activeSelf)
- {
- Button crossHairBtn = _btnCrosshair.GetComponent<Button>();
- crossHairBtn.onClick.Invoke();
- }
- else {
- bool onlyShow = !DuckHunter.CrossHair.Instance.GetOnlyShow();
- DuckHunter.CrossHair.Instance.SetOnlyShow(onlyShow);
- //记录准心值
- if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
- }
-
- }
- else if (HyperspaceGame.UIManager._ins) {
- //打枪
- Transform _btnCrosshair = HyperspaceGame.UIManager._ins.transform.Find("BtnCrosshair");
- if (_btnCrosshair.gameObject.activeSelf)
- {
- Button crossHairBtn = _btnCrosshair.GetComponent<Button>();
- crossHairBtn.onClick.Invoke();
- }
- else {
- bool onlyShow = !HyperspaceGame.UIManager._ins.GetOnlyShow();
- HyperspaceGame.UIManager._ins.SetOnlyShow(onlyShow);
- //记录准心值
- if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
- }
-
- }
- else
- {
- if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.StartsWith("FruitMaster")){
- //水果
- GameObject _crosshairBtn = GameObject.Find("PermanentCanvas/CrossHair_Btn");
- if (_crosshairBtn.gameObject.activeSelf)
- {
- Button crossHairBtn = _crosshairBtn.GetComponent<Button>();
- crossHairBtn.onClick.Invoke();
- }
- else {
- bool onlyShow = !JCFruitMaster.ins.GetOnlyShow();
- JCFruitMaster.ins.SetOnlyShow(onlyShow);
- //记录准心值
- if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
- }
-
- }
-
- }
- }
- }
|