コード例 #1
0
        private void AnswerPosition()
        {
            if (Players.ContainsKey(ID))
            {
                var pos = Players[ID].Position;
                outQueue.Add(NetCommand.WriteCommand(ID, Command.CreatePlayer, pos.X, pos.Y));
                int color = 0;
                switch (Players[ID].pallete)
                {
                case "yellow":
                    color = 0;
                    break;

                case "silver":
                    color = 1;
                    break;

                case "green":
                    color = 2;
                    break;

                case "red":
                    color = 3;
                    break;
                }
                outQueue.Add(NetCommand.WriteCommand(ID, Command.ChangePlayerColor, color, 0));
                outQueue.Add(NetCommand.WriteCommand(ID, Command.RotatePlayer, (int)Players[ID].direction, 0));
                outQueue.Add(NetCommand.WriteCommand(ID, Command.StopPlayer, 0));
            }
        }
コード例 #2
0
        public override void Update(GameTime gameTime)
        {
            inQueue = new List <NetIncomingMessage>();
            var msgCount = client.ReadMessages(inQueue);

            outQueue = new List <NetOutgoingMessage>();

            if (HelperFunction.IsKeyDown(Keys.Escape))
            {
                client.Disconnect("Smell ya later");
                CONTENT_MANAGER.GameInstance.Exit();
            }

            if (!Players.ContainsKey(ID) && HelperFunction.IsKeyPress(Keys.R))
            {
                outQueue.Add(NetCommand.WriteCommand(ID, Command.CreatePlayer, 112f, 112f));
            }

            foreach (var player in Players.Values)
            {
                player.Update(gameTime);
            }

            foreach (var msg in inQueue)
            {
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.DiscoveryResponse:
                    Console.WriteLine("Found server at " + msg.SenderEndPoint + " name: " + msg.ReadString());
                    client.Connect(msg.SenderEndPoint);
                    break;

                case NetIncomingMessageType.Data:
                {
                    NetCommand netcmd = NetCommand.ReadCommand(msg);

                    switch (netcmd.Command)
                    {
                    case Command.CreatePlayer:
                        Console.WriteLine(netcmd);
                        SpawnPlayer(netcmd);
                        break;

                    case Command.DestroyPlayer:
                        if (Players.ContainsKey(netcmd.LL))
                        {
                            world.Remove(Players[netcmd.LL].collision);
                            Players.Remove(netcmd.LL);
                        }
                        break;

                    case Command.QuerryPlayerPosition:
                        AnswerPosition();
                        break;

                    case Command.ChangePlayerColor:
                    case Command.MovePlayer:
                    case Command.RotatePlayer:
                    case Command.StopPlayer:
                    case Command.CreateBullet:
                    case Command.MoveBullet:
                    case Command.DestroyBullet:
                        Players[netcmd.ID].ExecuteCommand(netcmd);
                        break;
                    }
                }
                break;
                }
            }

            foreach (var msg in outQueue)
            {
                client.SendMessage(msg, NetDeliveryMethod.ReliableOrdered);
            }
            client.FlushSendQueue();

            client.Recycle(inQueue);

            canvas.Update(gameTime, CONTENT_MANAGER.CurrentInputState, CONTENT_MANAGER.LastInputState);
        }