Ejemplo n.º 1
0
        public static void ParseDataMessage(NetIncomingMessage msg)
        {
            PacketType packetType = (PacketType)msg.ReadByte();

            switch (packetType)
            {
            case PacketType.PlayerConnectPacket:
                PlayerConnectPacket connectPacket = PlayerConnectPacket.FromMessage(msg);
                clients.Add(msg.SenderConnection.RemoteUniqueIdentifier, new Player(connectPacket.PlayerName));
                Write("SERVER", "PlayerConnectPacket from " + connectPacket.PlayerName + " (" + msg.SenderConnection.RemoteUniqueIdentifier.ToString() + ")");
                PlayerListPacket.New(clients.Values.ToList()).Send(Server);     // tell all clients when someone joins.
                break;

            case PacketType.PlayerChatPacket:
                Player           p          = clients[msg.SenderConnection.RemoteUniqueIdentifier];
                PlayerChatPacket chatPacket = PlayerChatPacket.FromMessage(msg);
                Write(p.Username, chatPacket.Message);
                break;

            case PacketType.PlayerMovePacket:
                p = clients[msg.SenderConnection.RemoteUniqueIdentifier];
                PlayerMovePacket movePacket = PlayerMovePacket.FromMessage(msg);
                Write(p.Username, "MOVEPACKET x=" + movePacket.Position.X + " y=" + movePacket.Position.Y + " r=" + movePacket.Rotation);
                p.Rotation             = movePacket.Rotation;
                p.Position             = movePacket.Position;
                p.NeedsSendingMovement = true;
                break;

            default:
                Write("SERVER", "Unknown packet type.");
                break;
            }
        }
Ejemplo n.º 2
0
        public override void Update(GameTime gameTime)
        {
            NetIncomingMessage msg;

            while ((msg = Main.Client.ReadMessage()) != null)
            {
                if (msg.MessageType == NetIncomingMessageType.Data)
                {
                    PacketType packetType = (PacketType)msg.ReadByte();

                    switch (packetType)
                    {
                    case PacketType.PlayerConnectPacket:
                        PlayerConnectPacket connectPacket = PlayerConnectPacket.FromMessage(msg);
                        break;

                    case PacketType.PlayerChatPacket:
                        PlayerChatPacket chatPacket = PlayerChatPacket.FromMessage(msg);
                        break;

                    case PacketType.PlayerMovePacket:
                        PlayerMovePacket movePacket = PlayerMovePacket.FromMessage(msg);
                        if (movePacket.Username == Program.UserName)
                        {
                            player.Rotation = movePacket.Rotation;
                            player.Position = movePacket.Position;
                        }
                        else
                        {
                        }
                        break;

                    case PacketType.PlayerListPacket:

                        PlayerListPacket listPacket = PlayerListPacket.FromMessage(msg);
                        HandlePlayerList(listPacket.Players);
                        break;

                    default:
                        break;
                    }
                }
            }

            input.Update(gameTime, camera);
            colorCycle.Update(gameTime);
            camera.Update(gameTime);
            player.Update(gameTime, camera);
            if (input.NeedsMoveSend)
            {
                PlayerMovePacket.New(Program.UserName, input.Position, input.Rotation).Send(Main.Client);
                input.SentMove();
            }
            foreach (Character npc in npcs)
            {
                npc.Update(gameTime, camera);
            }

            foreach (Character c in players.Values.AsEnumerable())
            {
                c.Update(gameTime, camera);
            }

            cameraLock.Syncronize();
            Bullet.Update(gameTime);
        }