private void UpdateFromInput(MicrobitData data)
 {
     if (Input.GetKeyDown(KeyCode.LeftArrow))
     {
         _avatarController.MoveHorizontal(-1);
     }
     else if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         _avatarController.MoveHorizontal(1);
     }
     else if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         _avatarController.Jump();
     }
     else if (Input.GetKeyDown(KeyCode.DownArrow))
     {
     }
     if (data != null)
     {
         if (data.Left)
         {
             _avatarController.MoveHorizontal(-1);
         }
         else if (data.Right)
         {
             _avatarController.MoveHorizontal(1);
         }
         else if (data.Front)
         {
             _avatarController.Jump();
         }
     }
 }
示例#2
0
    //private void SocketWriter()
    //{
    //    if (!Connected)
    //    {
    //        return;
    //    }
    //    while (true)
    //    {
    //        if (SocketWorkerCancelled)
    //        {
    //            // Socket worker is cancelled by ourselves
    //            try
    //            {
    //                var commands = _controller.GetCommands();
    //                if (commands != null)
    //                {
    //                    foreach (Command c in commands)
    //                    {
    //                        Serializer.SerializeWithLengthPrefix(_stream, c, PrefixStyle.Base128);
    //                        _stream.Flush();
    //                    }
    //                }
    //            }
    //            catch (Exception)
    //            {
    //                // ignore all exceptions
    //            }
    //            break;
    //        }
    //        //if (_controller.IsCommandAvailable())
    //        //{
    //        Command command = _controller.BlockingGetCommand();
    //        if (command == null)
    //        {
    //            if (SocketWorkerCancelled)
    //            {
    //                // We cancel socketworker ourselves and need to send the final command if any.
    //                continue;
    //            }
    //            else
    //            {
    //                // Server breaks down. Exit directly.
    //                break;
    //            }
    //        }
    //        try
    //        {
    //            Serializer.SerializeWithLengthPrefix(_stream, command, PrefixStyle.Base128);
    //            _stream.Flush();
    //        }
    //        catch (Exception)
    //        {
    //            // Socket exception or server stopped
    //            break;
    //        }
    //        //}
    //    }
    //    // The server may breaks down, but reconnecting to server has been done in SocketReader()
    //}

    private void AddRxData(MicrobitData data)
    {
        lock (_receivedDataLock)
        {
            while (_receivedData.Count > _dataQueueLengthMax)
            {
                _receivedData.RemoveLast();
            }
            _receivedData.AddFirst(data);
        }
    }
示例#3
0
 public MicrobitData GetRxData()
 {
     lock (_receivedDataLock)
     {
         if (_receivedData.Count == 0)
         {
             return(null);
         }
         MicrobitData data = _receivedData.First.Value;
         _receivedData.RemoveFirst();
         return(data);
     }
 }
示例#4
0
 private void SocketReader()
 {
     if (!Connected)
     {
         return;
     }
     using (StreamReader reader = new StreamReader(_stream))
     {
         while (true)
         {
             if (SocketWorkerCancelled)
             {
                 // Socket Workers cancelled
                 Connected = false;
                 break;
             }
             try
             {
                 //string dataString = reader.ReadLine();
                 //Debug.LogWarning(dataString);
                 //MicrobitData data = JsonConvert.DeserializeObject<MicrobitData>(dataString);
                 MicrobitData data = Serializer.DeserializeWithLengthPrefix <MicrobitData>(_stream, PrefixStyle.Base128);
                 if (data == null)
                 {
                     // Socket Exception or Server Down
                     Connected = false;
                     break;
                 }
                 //Debug.LogWarning(string.Format("Received data: {0}", data));
                 AddRxData(data);
             }
             catch (Exception)
             {
                 // SocketWorkerCancelled or Server Stop
                 Connected = false;
                 break;
             }
         }
     }
     // Check if we need to restart the server
     if (SocketWorkerCancelled)
     {
         // We are intentionally closing down
     }
     else
     {
         // Server stopped for whatever reason. Start reconnecting
         Debug.Log("Server stops. Start reconnecting...");
         StartConnect();
     }
 }
 void FixedUpdate()
 {
     if (_testInput)
     {
         UpdateFromInput(null);
     }
     else
     {
         if (_receiver.Connected)
         {
             MicrobitData data = _receiver.GetRxData();
             if (data == null)
             {
                 Debug.Log("No data yet.");
             }
             else
             {
                 //Debug.LogWarning(data);
                 UpdateFromInput(data);
             }
         }
     }
 }