using System; using UnityEngine; namespace SmartBowSDK { public class SmartBowHelper : MonoBehaviour { private static SmartBowHelper _Instance; internal SmartBowNetwork smartBowNetwork; internal BluetoothAim_SDK bluetoothAim; internal AimHandler_SDK aimHandler; internal ShootChecker_SDK shootChecker; public CalibrateDataStorageMode calibrateDataStorageMode = CalibrateDataStorageMode.Remote; void Awake() { DontDestroyOnLoad(gameObject); gameObject.AddComponent(); smartBowNetwork = gameObject.AddComponent(); bluetoothAim = gameObject.AddComponent(); aimHandler = gameObject.AddComponent(); shootChecker = gameObject.AddComponent(); smartBowNetwork.smartBowHelper = this; bluetoothAim.smartBowHelper = this; aimHandler.smartBowHelper = this; shootChecker.smartBowHelper = this; } /// /// 获取唯一实例 /// /// public static SmartBowHelper GetInstance() { if (!_Instance) _Instance = new GameObject(typeof(SmartBowHelper).Name).AddComponent(); return _Instance; } /// /// 创建新的实例 /// /// public static SmartBowHelper NewInstance() { return new GameObject(typeof(SmartBowHelper).Name).AddComponent(); } /// /// 获取一个window 蓝牙用例 /// /// public BluetoothWindows CreateBluetoothWindows() { return bluetoothAim.CreateWindowBLE(); } #region 手机验证 /// /// 发送手机验证码 /// public void SendPhoneValidateCode(string phone, int language, Action callback) { smartBowNetwork.SendPhoneValidateCode(phone, language, callback); } /// /// 验证手机验证码 /// public void ValidatePhone(string phone, int code, Action callback) { smartBowNetwork.ValidatePhoneCode(phone, code, callback); } #endregion #region 邮箱验证 /// /// 发送邮箱验证码 /// public void SendEmailValidateCode(string email, int language, Action callback) { smartBowNetwork.SendEmailValidateCode(email, language, callback); } /// /// 验证邮箱验证码 /// public void ValidateEmail(string email, int code, Action callback) { smartBowNetwork.SendEmailValidateCode(email, code, callback); } #endregion #region 注册 /// /// 用户注册 /// /// 服务器索引 /// 用户名 /// 密码 /// 邮箱 /// 手机号 /// 当前接收的code,海外用邮箱 serverIndex = 1,国内 serverIndex = 0用手机号 /// 回调 public void Register(int serverIndex, string username, string password, string email, string phone,int code, Action callback) { smartBowNetwork.RegisterByCode(serverIndex, username, password, email, phone, code, callback); } /// /// 重置密码(支持邮箱或手机号) /// /// 服务器索引 /// 账号(邮箱或手机号,现在国内用手机,海外用邮箱) /// 验证码 /// 新密码 /// 回调结果 public void ResetPassword(int serverIndex,string account, string codeAccount, int code, string newPassword, Action callback) { smartBowNetwork.ResetPasswordByAccount(serverIndex, account, codeAccount, code, newPassword, callback); } #endregion #region 登录 /// /// 用户登录 /// /// 游戏ID /// 渠道ID /// 账号 /// 密码 /// 回调函数 public void Login(string gameID, string channelID, string username, string password, Action callback) { smartBowNetwork.Login(gameID, channelID, username, password, callback); } /// /// 用户登出 /// /// 回调函数 public void Logout(Action callback) { smartBowNetwork.Logout(callback); } /// /// 获取用户基本信息 /// /// 回调函数 public void GetUserInfo(Action callback) { smartBowNetwork.GetUserInfo(callback); } /// /// 设置用户名 /// public void SaveUserName(string nickname, Action callback) { smartBowNetwork.SaveUserName(nickname, callback); } /// /// 设置用户扩展信息 /// public void SaveUserInfo(UserInfoExtended userInfo, Action callback) { smartBowNetwork.SaveUserInfo(userInfo, callback); } #endregion #region 分数上传 /// /// 上传单人赛季成绩 /// public void UploadSeasonSinglePlayerGameRes(string secret, int score, Action callback) { smartBowNetwork.UploadSeasonSinglePlayerGameRes(secret, score, callback); } /// /// 上传PK赛季成绩 /// public void UploadSeasonPKGameRes(string roomKey, string secret, int p2ID, int gameRes, Action callback) { smartBowNetwork.UploadSeasonPKGameRes(roomKey, secret, p2ID, gameRes, callback); } #endregion #region 排行榜 /// /// 获取排行榜(按游戏类型) /// public void GetSeasonRankListByGameType(string secret, int rankArea, string country, string state, string city, Action callback) { smartBowNetwork.GetSeasonRankListByGameType(secret, rankArea, country, state, city, callback); } /// /// 获取自定义排行榜 /// public void GetSeasonRankListByCustom(string secret, int rankArea, string country, string state, string city, int rankType, int count, Action callback) { smartBowNetwork.GetSeasonRankListByCustom(secret, rankArea, country, state, city, rankType, count, callback); } /// /// 获取当前用户的排名信息 /// public void GetUserRankByGameType(string secret, int rankArea, string country, string state, string city, int rankType, int count, Action callback) { smartBowNetwork.GetUserRankByGameType(secret, rankArea, country, state, city, rankType, count, callback); } #endregion #region 定制游戏上传接口 /// /// 上传自定义排行榜分数 /// public void UploadCustomLeaderboardScore(string secret, int taskId, int difficultType, int modeType, int deviceType, int score, Action callback) { smartBowNetwork.UploadCustomLeaderboardScore(secret, taskId, difficultType, modeType, deviceType, score, callback); } /// /// 获取自定义排行榜数据 /// public void GetCustomLeaderboard(string secret, int taskId, int difficultType, int modeType, int deviceType, int timeType, Action callback) { smartBowNetwork.GetCustomLeaderboard(secret, taskId, difficultType, modeType, deviceType, timeType, callback); } #endregion #region 蓝牙:蓝牙部分新增修改,添加当前判断设备mac ///// ///// 连接蓝牙模块 ///// //public void Connect() //{ // //之前的默认不用判断mac // //当前连接不需要需要检测设备,设置为null // bluetoothAim.ResetAimDeviceInfoToNull(); // bluetoothAim.Connect(); //} /// /// 设置蓝牙过滤名称 /// /// 不传参数时候,设置值 "" public void SetFilters(string name = "") { if (!string.IsNullOrWhiteSpace(name)) { bluetoothAim.filters = name; } else { bluetoothAim.filters = ""; } } /// /// 设置是否使用名称连接 /// /// 是否使用名称连接 public void SetIsConnectName(bool useName) { bluetoothAim.isConnectName = useName; } /// /// 设置连接的MAC地址 /// /// MAC地址字符串 public void SetConnectMacStr(string macStr) { if (!string.IsNullOrWhiteSpace(macStr)) { bluetoothAim.connectMacStr = macStr; } } /// /// 设置传感器类型,用来区分解析 /// /// public void SetSensorAxisType(SensorAxisType sensorAxisType) { bluetoothAim.sensorAxisType = sensorAxisType; SmartBowLogger.Log(this, "SetSensorAxisType:" + bluetoothAim.sensorAxisType); } /// /// 连接蓝牙模块 /// 新增连接操作,需要用户标签和设备id /// userTags: 存储的信息标签,按实际用户区分 /// deviceId:对应设备id,同一个用户下的不同设备 /// bRecreateDeviceInfo: 重新创建设备信息。false 的情况下默认限制mac /// public void Connect(string userTags,int deviceId, bool bRecreateDeviceInfo = false) { if (userTags == null || userTags.Length == 0) { SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!"); return; } if (deviceId <= 0) { SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!"); return; } //判断mac需要创建一个连接信息 bluetoothAim.initAimDeviceInfo(userTags,deviceId,bRecreateDeviceInfo); //进行蓝牙连接 bluetoothAim.Connect(); } /// /// 清空建立的蓝牙判断数据 /// userTags: 存储的信息标签,按实际用户区分 /// deviceId:对应设备id,同一个用户下的不同设备 /// public void ClearDeviceInfo(string userTags,int deviceId) { if (userTags == null || userTags.Length == 0) { SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!"); return; } if (deviceId <= 0) { SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!"); return ; } bluetoothAim.onClearAimDeviceInfoByDeviceId(userTags, deviceId); } /// /// 获取存储的数据 /// userTags: 存储的信息标签,按实际用户区分 /// deviceId:对应设备id,同一个用户下的不同设备 /// public AimDeviceInfo GetDeviceInfo(string userTags, int deviceId) { if (userTags == null || userTags.Length == 0) { SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!"); return null; } if (deviceId <= 0) { SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!"); return null; } return bluetoothAim.onGetAimDeviceInfos(userTags, deviceId); } /// /// 断开蓝牙模块 /// public void Disconnect() { bluetoothAim.Disconnect(); } /// /// 获取蓝牙状态 /// /// 蓝牙状态 public BluetoothStatusEnum GetBluetoothStatus() { return bluetoothAim.bluetoothStatus; } /// /// 监听蓝牙状态变化 /// public event BluetoothStatusChangedEvent OnBluetoothStatusChanged; /// /// 判断蓝牙模块是否初始化完成 /// /// 蓝牙模块是否初始化完成 public bool IsBluetoothModuleInited() { return bluetoothAim.moduleInited; } /// /// 监听蓝牙模块初始化完成 /// Tip:连接蓝牙模块成功后会有几秒的初始化过程,初始化完成后才能进行传感器相关的函数调用。 /// public Action OnBluetoothModuleInited; /// /// 监听蓝牙错误 /// public event BluetoothErrorEvent OnBluetoothError; /// /// 获取Mac地址 /// Tip:需要模块初始化完成才能获取 /// /// 如果返回null则是获取失败 public string GetMacAddress() { return bluetoothAim.macAddress; } /// /// 获取硬件固件版本 /// Tip:需要模块初始化完成才能获取 /// /// 如果返回null则是获取失败 public string GetDeviceVersion() { return bluetoothAim.deviceVersion; } /// /// 获取电量 /// Tip:需要模块初始化完成才能获取 /// /// 电量值(范围0~100),值为0时也代表获取失败 public int GetBattery() { return bluetoothAim.battery; } /// /// 设置目标设备名称(用于搜索和连接) /// /// 设备名称 public void SetTargetDeviceName(string deviceName) { bluetoothAim.deviceConfig.deviceName = deviceName; } #endregion #region 传感器 /// /// 开启九轴传感 /// /// 请求是否发送成功 public bool StartRotationSensor() { return bluetoothAim.RequestOpen9Axis(); } /// /// 停止九轴传感 /// /// 请求是否发送成功 public bool StopRotationSensor() { return bluetoothAim.RequestClose9Axis(); } /// /// 监听姿态角更新 /// public event RotationUpdateEvent OnRotationUpdate; /// /// 开启射箭传感 /// /// 请求是否发送成功 public bool StartShootingSensor() { return bluetoothAim.RequestOpenInfrared(); } /// /// 停止射箭传感 /// /// 请求是否发送成功 public bool StopShootingSensor() { return bluetoothAim.RequestCloseInfrared(); } /// /// 监听射箭 /// public ShootingEvent OnShooting; /// /// 开始陀螺仪校准 /// public void StartGyrCalibration() { aimHandler.StartGyrCalibration(); } /// /// 停止陀螺仪校准 /// public void StopGyrCalibration() { aimHandler.StopGyrCalibration(); } /// /// 判断陀螺仪是否正在校准 /// /// 陀螺仪是否正在校准 public bool IsGyrCalibrating() { return aimHandler.IsGyrCalibrating(); } /// /// 获取陀螺仪校准进度 /// /// 陀螺仪校准进度 public float GetGyrProgress() { return aimHandler.GetGyrProgress(); } /// /// 判断陀螺仪是否校准完成 /// /// 陀螺仪是否校准完成 public bool IsGyrCompleted() { return aimHandler.IsGyrCompleted(); } /// /// 开始地磁计校准 /// public void StartMagCalibration() { aimHandler.StartMagCalibration(); } /// /// 判断地磁计是否校准完成 /// /// 地磁计是否校准完成 public bool IsMagCompleted() { return aimHandler.IsMagCompleted(); } /// /// 视角归位 /// public void ResetAim() { aimHandler.DoIdentity(); } /// /// 监听模块的功能键短按 /// public Action OnFunctionKeyPress; /// /// 监听模块的功能键长按 /// public Action OnFunctionKeyLongPress; #endregion /// /// 蓝牙状态变化事件 /// /// 旧状态 /// 新状态 public delegate void BluetoothStatusChangedEvent(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus); internal void InvokeOnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus) { try { OnBluetoothStatusChanged?.Invoke(oldStatus, newStatus); } catch (Exception e) { Debug.LogError(e); } } /// /// 蓝牙错误事件 /// /// 错误类型 /// 消息描述 public delegate void BluetoothErrorEvent(BluetoothError error, string message); internal void InvokeOnBluetoothError(BluetoothError error, string message) { try { OnBluetoothError?.Invoke(error, message); } catch (Exception e) { Debug.LogError(e); } } internal void InvokeOnFunctionKeyPress() { try { OnFunctionKeyPress?.Invoke(); } catch (Exception e) { Debug.LogError(e); } } internal void InvokeOnFunctionKeyLongPress() { try { OnFunctionKeyLongPress?.Invoke(); } catch (Exception e) { Debug.LogError(e); } } /// /// 姿态角更新事件 /// /// 姿态角 public delegate void RotationUpdateEvent(Quaternion rotation); internal void InvokeOnRotationUpdate(Quaternion rotation) { if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return; try { OnRotationUpdate?.Invoke(rotation); } catch (Exception e) { Debug.LogError(e); } } /// /// 射箭事件 /// /// 速度(m/s) public delegate void ShootingEvent(float speed); internal void InvokeOnShooting(float speed) { try { OnShooting?.Invoke(speed); } catch (Exception e) { Debug.LogError(e); } } internal void InvokeOnBluetoothModuleInited() { try { OnBluetoothModuleInited?.Invoke(); } catch (Exception e) { Debug.LogError(e); } } /// /// 监听枪状态 /// public BleDeviceEvent OnBleDeviceState; /// /// 硬件对应的事件 /// public delegate void BleDeviceEvent(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum); internal void InvokeOnBleDevice(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum) { try { OnBleDeviceState?.Invoke(bleDeviceType,gunStatusEnum); } catch (Exception e) { Debug.LogError(e); } } /// /// 监听设备状态和连接的设备平台信息 /// public DeviceAndSystemInfoEvent OnDeviceAndSystemInfoEvent; /// /// 硬件对应的事件初始化回调 /// public delegate void DeviceAndSystemInfoEvent(ConnectPlatform connectPlatform,BluetoothDeviceType bleDeviceType); internal void InvokeOnDeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bleDeviceType) { try { OnDeviceAndSystemInfoEvent?.Invoke(connectPlatform, bleDeviceType); } catch (Exception e) { Debug.LogError(e); } } #region 6轴部分接口 /// /// 6轴数据重置初始位置数据 /// public void ResetSixAxisIdentity() { bluetoothAim.ResetSixAxisFusion(); } /// /// 监听姿态角更新 /// public event SixAxisRotationUpdateEvent OnSixAxisRotationUpdate; /// /// 姿态角更新事件 /// /// 姿态角 public delegate void SixAxisRotationUpdateEvent(Vector3 eulerAngles); internal void InvokeOnSixAxisRotationUpdate(Vector3 eulerAngles) { if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return; try { OnSixAxisRotationUpdate?.Invoke(eulerAngles); } catch (Exception e) { Debug.LogError(e); } } ///// ///// 芯片数据更新事件 ///// ///// 姿态角 //public delegate void AxisDataUpdateEvent(byte[] value); //internal void InvokeOnAxisDataUpdateEvent(byte[] value) //{ // if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return; // try // { // OnAxisDataUpdate?.Invoke(value); // } // catch (Exception e) // { // Debug.LogError(e); // } //} ///// ///// 芯片数据更新 ///// //public event AxisDataUpdateEvent OnAxisDataUpdate; #endregion } }