Просмотр исходного кода

激光开关 心跳包 弹夹弹出

17600099689 1 год назад
Родитель
Сommit
a8e50d1856

+ 73 - 3
Assets/BowArrow/Scripts/ArrowSerialPort.cs

@@ -67,7 +67,8 @@ public class ArrowSerialPort : MonoBehaviour
         else if (msg.Equals("OPENED"))
         {
             SerialPortHelper.ins.OnConnect(arg0.DeviceName);
-            RequestDeviceIno();//应用启动 请求一次设备信息
+            //应用启动 间隔十秒请求一次设备信息
+            InvokeRepeating("RequestDeviceIno", 0f, 10f);
             LOG($"{PortName} 串口打开成功!");
         }
         else if (msg.Equals("CLOSED"))//串口断开
@@ -128,13 +129,65 @@ public class ArrowSerialPort : MonoBehaviour
                 case 0x82://按键消息
                     OnDeviceButton(bytes);
                     break;
+                case 0x83://激光控制
+                    OnLightChange(bytes);
+                    break;
+                case 0x84://弹夹消息(仅手枪)
+                    OnMagazinesInfo(bytes);
+                    break;
             }
         }
     }
-    
+
+    bool lightWaitClose = false;
+    public void DelayCloseLight()
+    {
+        if (!lightWaitClose)
+        {
+            LOG("游戏时间结束 延迟关闭激光");
+            Invoke("DoDelayCloseLight", 5f);
+            lightWaitClose = true;
+        }
+    }
+
+    public void CancelDelayCloseLight()
+    {
+        LOG("关闭延迟关闭激光");
+        lightWaitClose = false;
+        CancelInvoke("DoDelayCloseLight");
+    }
+
+    private void DoDelayCloseLight()
+    {
+        RequestLightState(false);
+    }
+
     #region APP请求
     // 异或校验内容:命令+长度+数据内容
-    
+
+    /// <summary>
+    /// 设置激光状态
+    /// </summary>
+    public void RequestLightState(bool on)
+    {
+        List<byte> data = new List<byte>();
+        data.Add(0xAA);//起始码
+        data.Add(0x83);//命令号
+        data.Add(0x06);//长度
+        data.Add((byte)(on ? 0x01 : 0x00));//长度
+        byte temp = 0;
+        for (int i = 1; i < data.Count; i++)
+        {
+            temp ^= data[i];
+        }
+        data.Add(temp);//异或校验
+        data.Add(0x55);//结束码
+        serialPortUtility?.Write(data.ToArray());
+        LOG($"设置激光状态:{on}!");
+
+        OnLightChange(data.ToArray());
+    }
+
     /// <summary>
     /// app请求设备信息
     /// </summary>
@@ -151,6 +204,23 @@ public class ArrowSerialPort : MonoBehaviour
     #endregion
     
     #region 返回消息处理
+    /// <summary>
+    /// 弹夹消息
+    /// </summary>
+    /// <param name="bytes"></param>
+    private void OnMagazinesInfo(byte[] bytes)
+    {
+        Debug.Log($"{PortName} 收到弹夹消息!");
+        SerialPortHelper.ins.OnMagazineChange(bytes);
+    }
+
+    private void OnLightChange(byte[] bytes)// 0xAA 0x83 0x06 0x01 0x84 0x55
+    {
+        lightWaitClose = false;
+        Debug.Log($"{PortName} 激光状态变更回包!");
+        var isOn = bytes[3] == 0x01;
+        UserSettings.ins.lightState = isOn;
+    }
 
     /// <summary>
     /// 设备信息响应

+ 3 - 0
Assets/BowArrow/Scripts/Manager/LoginMgr/LoginMgr.cs

@@ -196,6 +196,9 @@ public class UserSettings
     //选择的红外连接设备
     public string selectDevicesName = "";
 
+    //激光设备开启状态 默认关闭
+    public bool lightState = false;
+
     //b端 每一局的设置
     public int PerRoundCoin = 2; //投币
     public int PerRoundSeconds = 1200;//时间

+ 10 - 1
Assets/BowArrow/Scripts/SerialPortHelper.cs

@@ -26,11 +26,13 @@ public class SerialPortHelper : MonoBehaviour
 
     public static System.Action<byte[]> aim;
     public static System.Action<byte[]> shoot;
+    public static System.Action<byte[]> magazineChange;
 
     void Start()
     {
         shoot = handleDataBytes;
         aim = OnDataReceived;
+        magazineChange = OnMagazineChange;
     }
 
     public void OnConnect(string deviceName)
@@ -45,7 +47,7 @@ public class SerialPortHelper : MonoBehaviour
 
     bool newPort = false;//true 用0 8串口   false 4替代0  6替代8
     /// <summary>
-    /// 获取串口实例(0串口和8串口)
+    /// 获取串口实例(0串口和8串口  4替代0  6替代8
     /// </summary>
     public SerialPortExample GetPort(int port)
     {
@@ -73,6 +75,13 @@ public class SerialPortHelper : MonoBehaviour
         return arrowPort;
     }
 
+    #region 弹匣变更
+    public void OnMagazineChange(byte[] bytes)//3-弹夹状态(0x00  弹夹弹出  0x01 弹夹插入)
+    {
+
+    }
+    #endregion
+
     #region 按键
     /// <summary>
     /// 

+ 15 - 2
Assets/BowArrow/Scripts/Standalone/StandaloneAPI.cs

@@ -124,8 +124,15 @@ public class StandaloneAPI
         long t = JCUnityLib.TimeUtils.GetTimestamp();
         long dt = t - _LastTimeMS;
         _LastTimeMS = t;
-        _GameTimeCountDownMS -= dt;
-        if (_GameTimeCountDownMS < 0) _GameTimeCountDownMS = 0;
+        if (_GameTimeCountDownMS <= 0)
+        {
+            _GameTimeCountDownMS = 0;
+            //延迟一分钟关闭激光
+            if (UserSettings.ins.lightState)
+                SerialPortHelper.ins.GetPort()?.DelayCloseLight();
+        }
+        else if (_GameTimeCountDownMS > 0)
+            _GameTimeCountDownMS -= dt;
     }
 
     private static bool _TimeCounterInited;
@@ -161,6 +168,12 @@ public class StandaloneAPI
         //如果符合设置数量。增加时间
         if (CoinCount >= UserSettings.ins.PerRoundCoin)
         {
+            //打开激光
+            var port = SerialPortHelper.ins.GetPort();
+            port?.CancelDelayCloseLight();
+            if (!UserSettings.ins.lightState)
+                port?.RequestLightState(true);
+
             CoinCount -= UserSettings.ins.PerRoundCoin;
             AddGameTimeCountDown(UserSettings.ins.PerRoundSeconds);
             Debug.Log("UserSettings.ins.PerRoundCoin:" + UserSettings.ins.PerRoundCoin);