Exemplo n.º 1
0
        public void ReceiveUdpData(byte[] bytes)
        {
            int index = 0;

            while (index < bytes.Length)
            {
                if (structConverter.GetDataType(bytes, 0) != DataType.Time)
                {
                    break;
                }
                index += 1 + structConverter.ConvertBytesToStruct(bytes, 1, out TimeData timeData);

                DataType dataType = structConverter.GetDataType(bytes, index);
                index++;
                switch (dataType)
                {
                case DataType.Input:
                    index += structConverter.ConvertBytesToStruct(bytes, index, out InputData inputData);
                    shipController.AddInputData(ref timeData, ref inputData);
                    break;

                default:
                    break;
                }
            }
        }
Exemplo n.º 2
0
        private void ReceiveTCPData()
        {
            tcp.Receive(out byte[] bytes);
            if (bytes != null)
            {
                int index = 0;
                while (index < bytes.Length)
                {
                    DataType type = converter.GetDataType(bytes, index);
                    index++;
                    switch (type)
                    {
                    case DataType.Initialize:
                        converter.ConvertStructToBytes(DateTime.UtcNow.TimeOfDay.TotalSeconds, DataType.InitialTime, out byte[] buf);
                        tcp.Send(buf);
                        index += converter.ConvertBytesToStruct(bytes, index, out InitializeData initializeData);
                        id     = initializeData.id;
                        break;

                    case DataType.CreateShipAction:
                        index += converter.ConvertBytesToStruct(bytes, index, out CreateShipActionData createShipActionData);
                        switch (createShipActionData.owner)
                        {
                        case ShipOwner.Player:
                            if (id == createShipActionData.id)
                            {
                                shipControllers.Add(shipsController.CreatePlayerShip(sender, createShipActionData.position, createShipActionData.id, createShipActionData.shipType, createShipActionData.team));
                            }
                            else
                            {
                                shipControllers.Add(shipsController.CreateEnemyPlayerShip(createShipActionData.position, createShipActionData.id, createShipActionData.shipType, createShipActionData.team));
                            }
                            break;

                        case ShipOwner.AI:
                            shipControllers.Add(shipsController.CreateAIShip(createShipActionData.position, createShipActionData.id, createShipActionData.shipType, createShipActionData.team));
                            break;
                        }
                        break;

                    case DataType.DestroyShipAction:
                        index += converter.ConvertBytesToStruct(bytes, index, out DestroyShipActionData destroyShipActionData);
                        for (int i = 0; i < shipControllers.Count; i++)
                        {
                            if (destroyShipActionData.id == shipControllers[i].Id)
                            {
                                shipsController.RemoveShip(shipControllers[i]);
                                shipControllers.RemoveAt(i);
                            }
                        }
                        break;

                    case DataType.CheckConnection:
                        tcp.Send(new byte[] { (byte)DataType.CheckConnection });
                        break;
                    }
                }
            }
        }