Пример #1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            Write("SERVER", "Starting up.");

            NetPeerConfiguration config = new NetPeerConfiguration("Adventure");

            config.Port              = 14242;
            config.PingInterval      = 2;
            config.ConnectionTimeout = 4;
            //config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

            Server = new NetServer(config);
            Server.Start();

            clients = new Dictionary <long, Player>();

            // ======================= LOOP ==========================
            while (!IsExiting)
            {
                DateTime start = DateTime.Now;

                #region Network Recieving
                try {
                    NetIncomingMessage msg = null;
                    while ((msg = Server.ReadMessage()) != null)
                    {
                        MessageHandle(msg);
                    }
                } catch (Exception e) {
                    Write("ERROR", e.ToString());
                }
                #endregion

                // DO LOGIC HERE

                foreach (Player p in clients.Values)
                {
                    if (p.NeedsSendingMovement)
                    {
                        PlayerMovePacket.New(p.Username, p.Position, p.Rotation).Send(Server);
                        p.NeedsSendingMovement = false;
                    }
                }

                /* FOR REFERENCE
                 * // Broadcast the message:
                 * NetOutgoingMessage om = server.CreateMessage();
                 * om.Write(senderName);
                 * om.Write(text);
                 * server.SendMessage(om, server.Connections, NetDeliveryMethod.ReliableOrdered, 0);
                 */

                // Reenable if we want set logic loop lengths.
                double loopDuration = DateTime.Now.Subtract(start).TotalMilliseconds;
                if (loopDuration < 1000f / LoopsPerSecond)
                {
                    System.Threading.Thread.Sleep((int)(1000f / LoopsPerSecond - loopDuration));
                }
            }
        }
Пример #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);
        }