using UnityEngine;
using UnityEngine.UI;
using SmartBowSDK;
namespace DuckHunter
{
public class SmartBowController : MonoBehaviour
{
[SerializeField] Button _btnConnect;
[SerializeField] Button _btnCalibrateGyr;
[SerializeField] Button _btnCalibrateMag;
[SerializeField] Button _btnResetAim;
public static SmartBowController Instance;
void Awake()
{
if (Instance)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
void Start()
{
_btnCalibrateGyr.onClick.AddListener(OnClick_CalibrateGyr);
_btnCalibrateMag.onClick.AddListener(OnClick_CalibrateMag);
SmartBowHelper.GetInstance().calibrateDataStorageMode = CalibrateDataStorageMode.Local;
//监听蓝牙状态变化
SmartBowHelper.GetInstance().OnBluetoothStatusChanged += OnBluetoothStatusChanged;
//监听蓝牙抛出的错误
SmartBowHelper.GetInstance().OnBluetoothError += OnBluetoothError;
//监听蓝牙模块初始化完成
SmartBowHelper.GetInstance().OnBluetoothModuleInited += OnBluetoothModuleInited;
//监听姿态角变化
SmartBowHelper.GetInstance().OnRotationUpdate += OnRotationUpdate;
//监听射击
SmartBowHelper.GetInstance().OnShooting += OnShooting;
//监听模块功能键短按
SmartBowHelper.GetInstance().OnFunctionKeyPress += OnFunctionKeyPress;
//监听模块功能键长按
SmartBowHelper.GetInstance().OnFunctionKeyLongPress += OnFunctionKeyLongPress;
UpdateBtnConnectText();
// ActiveCanvasUI();
// ActiveBoxBLEModule(false);
}
float lastAutoConnectTime = 0;
void Update()
{
// if (_boxBLEActived)
// {
// UpdateCalibrateGyrText();
// UpdateCalibrateMagText();
// }
UpdateCalibrateGyrText();
UpdateCalibrateMagText();
if (
SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None &&
Time.realtimeSinceStartup - lastAutoConnectTime > 1
) {
lastAutoConnectTime = Time.realtimeSinceStartup;
SmartBowHelper.GetInstance().Connect();
}
}
///
/// 按钮逻辑-连接/断开蓝牙模块
///
public void OnClick_Connect()
{
AudioMgr.ins.PlayBtn();
if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connecting) return;
if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None)
{
SmartBowHelper.GetInstance().Connect();
return;
}
if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connected)
{
SmartBowHelper.GetInstance().Disconnect();
return;
}
}
///
/// 按钮逻辑-视角归位
///
public void OnClick_ResetAim()
{
AudioMgr.ins.PlayBtn();
SmartBowHelper.GetInstance().ResetAim();
}
///
/// 按钮逻辑-开始/停止陀螺仪校准
///
public void OnClick_CalibrateGyr()
{
AudioMgr.ins.PlayBtn();
if (SmartBowHelper.GetInstance().IsGyrCalibrating())
SmartBowHelper.GetInstance().StopGyrCalibration();
else
SmartBowHelper.GetInstance().StartGyrCalibration();
}
void UpdateCalibrateGyrText()
{
Text text = _btnCalibrateGyr.GetComponentInChildren();
//当陀螺仪校准完成时
if (SmartBowHelper.GetInstance().IsGyrCompleted())
{
//UI显示校准完成
text.text = "陀螺仪初始化完成";
}
else
{
//获取陀螺仪校准进度
int progress = (int)(SmartBowHelper.GetInstance().GetGyrProgress() * 100);
//UI显示校准进度
if (SmartBowHelper.GetInstance().IsGyrCalibrating()) text.text = string.Format("停止初始化陀螺仪{0}%", progress);
else text.text = string.Format("开始初始化陀螺仪{0}%", progress);
}
}
float _clickCalibrateMagTime = -100;
///
/// 按钮逻辑-重置地磁计校准
///
public void OnClick_CalibrateMag()
{
AudioMgr.ins.PlayBtn();
_clickCalibrateMagTime = Time.realtimeSinceStartup;
SmartBowHelper.GetInstance().StartMagCalibration();
}
void UpdateCalibrateMagText()
{
Text text = _btnCalibrateMag.GetComponentInChildren();
//当地磁计校准完成时
if (SmartBowHelper.GetInstance().IsMagCompleted())
{
text.text = "地磁计初始化完成";
}
else
{
text.text = Time.realtimeSinceStartup - _clickCalibrateMagTime < 1 ? "地磁计已重置" : "地磁计自动初始化中";
}
}
void UpdateBtnConnectText()
{
// //获取蓝牙状态
// BluetoothStatusEnum bluetoothStatus = SmartBowHelper.GetInstance().GetBluetoothStatus();
// Text text = _btnConnect.GetComponentInChildren();
// //显示蓝牙状态-未连接
// if (bluetoothStatus == BluetoothStatusEnum.None) text.text = "连接";
// //显示蓝牙状态-连接中
// if (bluetoothStatus == BluetoothStatusEnum.Connecting) text.text = "连接中";
// //显示蓝牙状态-已连接
// if (bluetoothStatus == BluetoothStatusEnum.Connected)
// {
// //判断蓝牙模块是否初始化完成
// if (SmartBowHelper.GetInstance().IsBluetoothModuleInited()) text.text = "已连接";
// else text.text = "已连接 (初始化中)";
// }
}
void OnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
{
UpdateBtnConnectText();
if (newStatus == BluetoothStatusEnum.Connected) {
BowCamera.isTouchMode = false;
} else {
BowCamera.isTouchMode = true;
}
}
void OnBluetoothError(BluetoothError error, string message)
{
Debug.Log("OnBluetoothError:" + error + " " + message);
string errorText = null;
if (error == BluetoothError.BluetoothNotEnabled)
errorText = "蓝牙开关未打开";
else if (error == BluetoothError.LocationPermissionNotGranted)
errorText = "尚未授予定位权限";
else if (error == BluetoothError.ScanPermissionNotGranted)
errorText = "尚未授予扫描附近设备权限";
else if (error == BluetoothError.ScanNotFoundTargetDevice)
errorText = "连接失败,未发现目标设备!";
if (errorText != null)
TextSmartBowTip.Show(errorText);
}
///
/// 当蓝牙模块初始化完成时
///
void OnBluetoothModuleInited()
{
UpdateBtnConnectText();
//获取Mac地址
string macAddress = SmartBowHelper.GetInstance().GetMacAddress();
Debug.Log("Mac地址为" + macAddress);
//获取电量
int battery = SmartBowHelper.GetInstance().GetBattery();
Debug.Log("当前电量为" + battery);
//开启九轴传感
SmartBowHelper.GetInstance().StartRotationSensor();
//开启射箭传感
SmartBowHelper.GetInstance().StartShootingSensor();
}
void OnRotationUpdate(Quaternion rotation)
{
CameraToLook.ins.transform.localRotation = rotation;
}
float _lastShootTime = 0;
void OnShooting(float speed)
{
if (Time.realtimeSinceStartup - _lastShootTime < 1) return;
_lastShootTime = Time.realtimeSinceStartup;
if (ArmBow.ins) {
ArmBow.ins.shootSpeed = speed;
ArmBow.ins.ADS_fire();
}
}
void OnFunctionKeyPress()
{
Debug.Log("功能键短按");
AutoResetView.DoIdentity();
}
[ContextMenu("TestIdentity")]
void TestIdentity() {
AutoResetView.DoIdentity();
}
void OnFunctionKeyLongPress()
{
Debug.Log("功能键长按");
// SB_EventSystem.ins.AwakenSimulateMouse();
}
private void ActiveCanvasUI()
{
transform.Find("CanvasUI").gameObject.SetActive(true);
}
// bool _boxBLEActived = false;
// public void ActiveBoxBLEModule(bool active)
// {
// var o = transform.Find("CanvasUI/BoxBLEModule").gameObject;
// o.SetActive(active);
// _boxBLEActived = active;
// }
///
/// 反转激活BoxBLEModule
///
public void ActiveBoxBLEModule()
{
// var o = transform.Find("CanvasUI/BoxBLEModule").gameObject;
// o.SetActive(!o.activeSelf);
// _boxBLEActived = o.activeSelf;
}
///
/// 按钮逻辑-停止传感器
///
public void OnClick_StopSensor()
{
//停止九轴传感
SmartBowHelper.GetInstance().StopRotationSensor();
//停止射箭传感
SmartBowHelper.GetInstance().StopShootingSensor();
}
}
}