Ver Fonte

模拟鼠标控制器

lvjincheng há 3 anos atrás
pai
commit
0a1eb47a00

+ 3 - 0
Assets/BowArrow/Scripts/Bluetooth/BluetoothAim.cs

@@ -113,6 +113,9 @@ public class BluetoothAim : MonoBehaviour
             });
             sequence.SetUpdate(true);
             DeviceReconnectView.Show();
+            SimulateMouseController.ins?.SetBleConnected(false);
+        } else if (status == BluetoothStatusEnum.ConnectSuccess) {
+            SimulateMouseController.ins?.SetBleConnected(true);
         }
     }
 

+ 1 - 0
Assets/BowArrow/Scripts/Entry.cs

@@ -35,6 +35,7 @@ public class Entry : MonoBehaviour
         };
 
         PersistenHandler.Init();
+        SimulateMouseController.Init();
     }
 
     [SerializeField] Text versionText;

+ 68 - 0
Assets/BowArrow/Scripts/Expand/SimulateMouseController.cs

@@ -0,0 +1,68 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.SceneManagement;
+
+/*
+模拟鼠标控制器
+主要负责自动开关模拟鼠标
+在蓝牙正常连接状态下,非游戏界面自动开启模拟鼠标,游戏界面自动关闭模拟鼠标。
+*/
+public class SimulateMouseController
+{
+    public static SimulateMouseController ins;
+
+    public static void Init() {
+        ins = new SimulateMouseController();
+        SceneManager.sceneLoaded += (scene, mode) => {
+            ins.AddOpenLocker("NotGame");
+        };
+    }
+
+    bool bleConnected = false;
+
+    //有locker就要保持鼠标开启状态
+    HashSet<object> openLockerSet = new HashSet<object>();
+
+    public void AddOpenLocker(object locker) {
+        openLockerSet.Add(locker);
+        CheckAndOpenOrClose();
+
+    }
+
+    public void RemoveOpenLocker(object locker) {
+        openLockerSet.Remove(locker);
+        CheckAndOpenOrClose();
+    }
+
+    //蓝牙是否处于连接状态
+    public void SetBleConnected(bool connected) {
+        bleConnected = connected;
+        CheckAndOpenOrClose();
+    }
+
+    //检测和改变开关
+    void CheckAndOpenOrClose() {
+        if (bleConnected) {
+            if (openLockerSet.Count > 0) {
+                Open();
+            } else {
+                Close();
+            }
+        } else {
+            Close();   
+        }
+    }
+
+    void Open() {
+        if (SB_EventSystem.ins && !SB_EventSystem.ins.simulateMouseIsAwaked) {
+            SB_EventSystem.ins.AwakenSimulateMouse();
+        }
+    }
+
+    void Close() {
+        if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
+            SB_EventSystem.ins.AwakenSimulateMouse();
+        }
+    }
+}

+ 11 - 0
Assets/BowArrow/Scripts/Expand/SimulateMouseController.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: f768cae05285be443af186eab60e3a99
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 6 - 0
Assets/BowArrow/Scripts/GameChallenge/Views/HuntGameSelectLevelView.cs

@@ -5,6 +5,11 @@ using UnityEngine.UI;
 /* 动物关卡的选难度界面 */
 public class HuntGameSelectLevelView : MonoBehaviour
 {
+    void Awake() 
+    {
+        SimulateMouseController.ins?.AddOpenLocker(this);
+    }
+
     void Start()
     {
         GameMgr.ins.addLockerForGamePause(this);
@@ -38,5 +43,6 @@ public class HuntGameSelectLevelView : MonoBehaviour
     void OnDestroy()
     {
         if (GameMgr.ins) GameMgr.ins.removeLockerForGamePause(this);
+        SimulateMouseController.ins?.RemoveOpenLocker(this);
     }
 }

+ 1 - 0
Assets/BowArrow/Scripts/GameChallenge/Views/HunterGameSettleView.cs

@@ -43,6 +43,7 @@ public class HunterGameSettleView : MonoBehaviour
         if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
             if (SocketPlayer.ins) SocketPlayer.ins.isGameOver = true;
         }
+        SimulateMouseController.ins?.AddOpenLocker("NotGame");
     }
 
     void CheckOpenNextLevelBtn(ChallengeGameMode gameMode, string gameRes) {

+ 1 - 0
Assets/BowArrow/Scripts/Manager/GameMgr.cs

@@ -126,6 +126,7 @@ public class GameMgr : MonoBehaviour
 
     void DoGameModeStart() {
         gameMode.Start();
+        SimulateMouseController.ins?.RemoveOpenLocker("NotGame");
     }
 
     HashSet<Object> gamePauseLockers = new HashSet<Object>();

+ 1 - 0
Assets/BowArrow/Scripts/View/PKGameSettleView.cs

@@ -48,6 +48,7 @@ public class PKGameSettleView : MonoBehaviour
         if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
             if (SocketPlayer.ins) SocketPlayer.ins.isGameOver = true;
         }
+        SimulateMouseController.ins?.AddOpenLocker("NotGame");
     }
 
     public void GoHome() {

+ 6 - 1
Assets/BowArrow/Scripts/View/TimeLimitGameDistanceSelectView.cs

@@ -5,7 +5,11 @@ using UnityEngine.UI;
 /* 限时模式的靶子距离选择界面 */
 public class TimeLimitGameDistanceSelectView : MonoBehaviour
 {
-    
+    void Awake() 
+    {
+        SimulateMouseController.ins?.AddOpenLocker(this);
+    }
+
     void Start()
     {
         GameMgr.ins.addLockerForGamePause(this);
@@ -36,5 +40,6 @@ public class TimeLimitGameDistanceSelectView : MonoBehaviour
     void OnDestroy()
     {
         if (GameMgr.ins) GameMgr.ins.removeLockerForGamePause(this);
+        SimulateMouseController.ins?.RemoveOpenLocker(this);
     }
 }

+ 1 - 0
Assets/BowArrow/Scripts/View/TimeLimitGameSettleView.cs

@@ -38,6 +38,7 @@ public class TimeLimitGameSettleView : MonoBehaviour
         }, score, 1);
 
         AudioMgr.ins.PlayWin();
+        SimulateMouseController.ins?.AddOpenLocker("NotGame");
     }
 
     public void GoHome() {