Ejemplo n.º 1
0
        public void ReadMessages()
        {
            NetIncomingMessage message;
            var stop = false;

            while (!stop)
            {
                while ((message = client.ReadMessage()) != null)
                {
                    switch (message.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                    {
                        CommandType data      = (CommandType)message.ReadByte();
                        string      commandId = message.ReadString();
                        byte        playerID  = 0;

                        ICommand command = null;
                        Player[] cl      = new Player[allPlayers.Count];
                        for (int i = 0; i < allPlayers.Count; i++)
                        {
                            cl[i] = allPlayers[i];
                        }
                        switch (data)
                        {
                        case CommandType.Movement:
                            playerID = message.ReadByte();
                            string direction = message.ReadString();
                            var    xmovement = direction.Contains("L") ? -5 : direction.Contains("R") ? 5 : 0;
                            var    ymovement = direction.Contains("U") ? -5 : direction.Contains("D") ? 5 : 0;
                            if (playerID == player.ID)
                            {
                                xmovement = 0;
                                ymovement = 0;
                            }
                            command = new MovementCommand(Player.GetPlayer(cl, playerID), xmovement, ymovement, direction, commandId);

                            break;

                        case CommandType.Welcome:
                            playerID = message.ReadByte();
                            var            playerX      = message.ReadInt32();
                            var            playerY      = message.ReadInt32();
                            var            size         = message.ReadByte();
                            ClientPlayer[] otherPlayers = new ClientPlayer[size];
                            for (int i = 0; i < size; i++)
                            {
                                var tempX   = message.ReadInt32();
                                var tempY   = message.ReadInt32();
                                var tempID  = message.ReadByte();
                                var tempRot = message.ReadFloat();
                                otherPlayers[i] = new ClientPlayer(tempX, tempY, tempRot, false);
                                otherPlayers[i].SetID(tempID);
                                otherPlayers[i].SetPos(tempX, tempY);
                            }
                            command = new WelcomeCommand <ClientPlayer>(player, otherPlayers, allPlayers, playerID, playerX, playerY, commandId);

                            break;

                        case CommandType.Connect:
                            ClientPlayer temp        = new ClientPlayer(100, 100, 0, false);
                            var          newPlayerID = message.ReadByte();
                            temp.SetID(newPlayerID);
                            command = new ConnectCommand <ClientPlayer>(allPlayers, temp, commandId);
                            break;

                        case CommandType.Disconnect:
                            var leavingPlayerID = message.ReadByte();
                            command = new DisconnectCommand <ClientPlayer>(allPlayers, (ClientPlayer)Player.GetPlayer(cl, leavingPlayerID), commandId);
                            break;

                        case CommandType.Interact:
                            break;
                        }

                        command.Run();


                        break;
                    }

                    case NetIncomingMessageType.DebugMessage:
                        Console.WriteLine(message.ReadString());
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        break;

                    default:
                        break;
                    }
                    client.Recycle(message);
                }
            }
        }
Ejemplo n.º 2
0
        public void ReadMessages()
        {
            NetIncomingMessage message;

            while (true)
            {
                while ((message = server.ReadMessage()) != null)
                {
                    switch (message.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                    {
                        var  data      = (CommandType)message.ReadByte();
                        var  commandId = message.ReadString();
                        byte playerID  = message.ReadByte();

                        ICommand command = null;

                        Player[] cl = new Player[_clients.Count];
                        for (int i = 0; i < _clients.Count; i++)
                        {
                            cl[i] = _clients[i];
                        }

                        switch (data)
                        {
                        case CommandType.Movement:
                            string direction = message.ReadString();
                            var    xmovement = direction.Contains("L") ? -5 : 0;
                            xmovement += direction.Contains("R") ? 5 : 0;
                            var ymovement = direction.Contains("U") ? -5 : 0;
                            ymovement += direction.Contains("D") ? 5 : 0;
                            command    = new MovementCommand(GetServerPlayer(message), xmovement, ymovement, direction, commandId);

                            break;

                        case CommandType.Interact:
                            break;
                        }

                        command.Run();
                        SendCommandToAll(command, playerID);


                        break;
                    }

                    case NetIncomingMessageType.DebugMessage:
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        if (message.SenderConnection.Status == NetConnectionStatus.Connected)
                        {
                            var newPlayer = new ServerPlayer(message.SenderConnection, 100, 100, 0);
                            newPlayer.SetID(currentID);
                            currentID++;

                            var command        = new ConnectCommand <ServerPlayer>(_clients, newPlayer, ICommand.GenerateRandID());
                            var welcomeCommand = new WelcomeCommand <ServerPlayer>(newPlayer, _clients.ToArray(), null, newPlayer.ID, 100, 100, ICommand.GenerateRandID());

                            var welcomeMessage = CreateMessage(welcomeCommand);

                            server.SendMessage(welcomeMessage, message.SenderConnection, NetDeliveryMethod.ReliableOrdered);

                            SendCommandToAll(command, newPlayer.ID);
                            command.Run();
                        }
                        if (message.SenderConnection.Status == NetConnectionStatus.Disconnected)
                        {
                            var leavingPlayer = GetServerPlayer(message);
                            var command       = new DisconnectCommand <ServerPlayer>(_clients, leavingPlayer, ICommand.GenerateRandID());

                            SendCommandToAll(command, leavingPlayer.ID);
                            command.Run();
                        }
                        break;

                    default:
                        break;
                    }
                    server.Recycle(message);
                }
            }
        }