Пример #1
0
        private void server_UserJoinRoom(JoinRoomPacket packet, Socket socket)
        {
            User user = server.GetUser(socket);
            Room room = server.GetRoom(packet.Guid);

            Invoke(new MethodInvoker(() => lstViewUsers.Items[user.Guid.ToString()].SubItems["Room"].Text = room.Name));
        }
Пример #2
0
        private void client_JoinRoom(JoinRoomPacket packet)
        {
            ToggleRoomTab(true);

            SwitchToTab(tabPageRoom);

            Invoke(new MethodInvoker(delegate
            {
                foreach (var user in packet.Room.Users)
                {
                    lstViewUsers.Items.Add(user.Username);
                }
            }));
        }
Пример #3
0
        public bool Process(NetIncomingMessage msg)
        {
            msg.Position = 0;
            short opcode = msg.ReadInt16();

            switch (opcode)
            {
            case JoinRoomPacket.OpCode:
            {
                JoinRoomPacket joinPacketInfo = new JoinRoomPacket(msg.ReadString());

                GameRoom room = NetworkSessionContainer.NetworkSessions.GameRooms.FirstOrDefault();       //= NetworkSessionContainer.NetworkSessions.GameRooms.Where(x => x.Name == joinPacketInfo.RoomName).FirstOrDefault();

                UserSession sendingUser = NetworkSessionContainer.NetworkSessions.UserSessions
                                          .Where(x => x.Connection == msg.SenderConnection).FirstOrDefault();

                if (room != null)
                {
                    /*if (room.Join(sendingUser)) // jak user sie nie podłączył, wysyła wiadomość osiągnięto limit graczy
                     * {
                     *   Console.WriteLine($"Dołączył do pokoju {sendingUser.ID}");
                     *   //Poinformowanie uzytkownika o tym że pokuj jest pełny
                     *
                     *   room.Start(room.Master); //USUNAĆ
                     * }*/
                }
                else
                {
                    //Poinformowanie uzytkownika o nie odnaleźieniu pokoju
                }

                break;
            }

            case CreateRoomPacket.OpCode:
            {
                CreateRoomPacket createRoomPacke = new CreateRoomPacket();
                createRoomPacke.Private = msg.ReadBoolean();
                createRoomPacke.Name    = msg.ReadString();
                Console.WriteLine("próba ytworzenia room");
                // if (!NetworkSessionContainer.NetworkSessions.GameRooms.Any(x=>x.Master.Connection==msg.SenderConnection || x.PlayersInRoomCollection.Any(y=>y.Connection== msg.SenderConnection)))
                {
                    Console.WriteLine($"Stworzono room \nNazwa:{createRoomPacke.Name}\nPrzez:{NetworkSessionContainer.NetworkSessions.UserSessions.Where(x=>x.Connection==msg.SenderConnection).FirstOrDefault().ID}");
                }
                break;
            }
            }
            return(true);
        }
Пример #4
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            MouseState mouse = Mouse.GetState();


            /////////TEST
            akumulator += gameTime.ElapsedGameTime.TotalMilliseconds;


            foreach (PlayerClass otherPl in OtherPlayerList)
            {
                otherPl.Interpolate();
            }



            /*   if (akumulator > movePacketInterval && player.PlayerNetInfo != null && player.Forward != player.OldForward)
             * {
             *     var newMSG = Client.CreateMessage();
             *     newMSG.Write((short)6066);
             *     newMSG.Write(player.Forward.X);
             *     newMSG.Write(player.Forward.Y);
             *     Client.SendMessage(newMSG, NetDeliveryMethod.UnreliableSequenced);
             *     akumulator = 0;
             * }*/


            /*
             * if (akumulator > movePacketInterval && player.PlayerNetInfo!=null)
             * {
             *   var newMSG = Client.CreateMessage();
             *   newMSG.Write((short)6066);
             *   newMSG.Write(player.Position.X);
             *   newMSG.Write(player.Position.Y);
             *   Client.SendMessage(newMSG, NetDeliveryMethod.UnreliableSequenced);
             *   akumulator = 0;
             * }*/
            /////////TEST

            NetIncomingMessage message;

            while ((message = Client.ReadMessage()) != null)
            {
                switch (message.MessageType)
                {
                case NetIncomingMessageType.Data:
                {
                    // handle custom messages
                    if (message.LengthBytes >= 2)
                    {
                        short opcode;
                        opcode = message.ReadInt16();
                        if (opcode == 2000)
                        {
                            PlayerClass SpawnPlayerPacket = new PlayerClass();
                            SpawnPlayerPacket.ID = message.PeekInt32();
                            //message.ReadAllProperties((object) player);
                            player.PlayerNetInfo = SpawnPlayerPacket;

                            var newMSG = Client.CreateMessage();

                            JoinRoomPacket joinRoom = new JoinRoomPacket(roomName);
                            newMSG.Write(JoinRoomPacket.OpCode);
                            newMSG.Write(joinRoom.RoomName);
                            Client.SendMessage(newMSG, NetDeliveryMethod.ReliableSequenced);
                            akumulator = 0;
                        }
                        else if (opcode == 2620)
                        {
                            PlayerClass playerTemp = new PlayerClass();
                            message.ReadAllProperties(playerTemp);
                            // playerTemp.isAlive = true;
                            OtherPlayerList.Add(playerTemp);
                        }
                        else if (opcode == 6066)
                        {
                            MovePacket movePacket = new MovePacket();
                            message.ReadAllProperties(movePacket);
                            PlayerClass player2Move = OtherPlayerList.FirstOrDefault(x => x.ID == movePacket.ID);
                            if (player2Move != null)
                            {
                                player2Move.StartPosition = player2Move.EndPosition;
                                player2Move.EndPosition   = new Vector2(movePacket.X, movePacket.Y);
                                player2Move.interStep     = 0;
                            }
                        }
                        else if (opcode == ResurrectPointAddPacket.OpCode)
                        {
                            ResurrectPointAddPacket resurrectPlayerPoint = new ResurrectPointAddPacket();
                            message.ReadAllProperties(resurrectPlayerPoint);
                            PlayerClass player = OtherPlayerList.FirstOrDefault(x => x.ID == resurrectPlayerPoint.ID);
                            //   player.isAlive = false;
                            if (player != null)
                            {
                                ResurrectPoint ressurectPoint = new ResurrectPoint("ResurrectID" + player.ID);
                                ressurectPoint.Position = new Vector2(resurrectPlayerPoint.X, resurrectPlayerPoint.Y);
                                //Tu też będzie sie ustawiało kolor żeby gracz wiedział o kogo chodzi poza tym kto sie będzie respawnił po najechaniu na pole decydować bedzie serwer(oczywiście respawnić będzie legitnego człowieka)
                                Map.MapElements.Add(ressurectPoint);
                            }
                        }
                        else if (opcode == RevivedPlayerPacket.OpCode)
                        {
                            RevivedPlayerPacket revivedPlayer = new RevivedPlayerPacket();
                            message.ReadAllProperties(revivedPlayer);
                            PlayerClass player = OtherPlayerList.FirstOrDefault(x => x.ID == revivedPlayer.ID);
                            // player.isAlive = true;
                            ResurrectPoint revivePoint =
                                (ResurrectPoint)Map.GetMapElementByName <IMapElement>("ResurrectID" + player.ID);
                            if (revivePoint != null)
                            {
                                Map.MapElements.Remove(revivePoint);
                            }
                        }
                    }
                    break;
                }

                case NetIncomingMessageType.StatusChanged:
                    // handle connection status messages
                    switch (message.SenderConnection.Status)
                    {
                    case NetConnectionStatus.Connected:
                    {
                        var newMSG = Client.CreateMessage();
                        newMSG.Write((short)2000);
                        newMSG.Write("Client");
                        Client.SendMessage(newMSG, NetDeliveryMethod.UnreliableSequenced);
                        break;
                    }
                    }
                    break;

                case NetIncomingMessageType.DebugMessage:
                    // handle debug messages
                    // (only received when compiled in DEBUG mode)

                    break;

                /* .. */
                default:
                    break;
                }
            }


            foreach (IKeyStroke key in keys)
            {
                key.Update();
            }



            if (IsActive)
            {
                Ray _castRay = ((BasicCamera)Director.InstanceDirector.Camera).CalculateCursorRay(mouse.X, mouse.Y);
                positionOnPlane = PlaneControll.IntersectPoint(_castRay.Direction, _castRay.Position, Vector3.Up, new Vector3(1, 0, 1));
                StandFloorDebug = Map.UpdatePlayerMovmentType(player);
                collision       = Map.MapPath[0].FloorPolygon.IsCollide(player.CollisionObject);
            }
            //  player.Update(gameTime, positionOnPlane);

            if (mouse.RightButton == ButtonState.Pressed && player.AliveBoiiii == false && IsActive == true)
            {
                var newMSG = Client.CreateMessage();
                newMSG.Write((short)6066);
                newMSG.Write(positionOnPlane.X);
                newMSG.Write(positionOnPlane.Z);
                Client.SendMessage(newMSG, NetDeliveryMethod.UnreliableSequenced);
            }



            Director.InstanceDirector.Camera.Update(gameTime);
            Map.Update(gameTime);


            player.OldForward = player.Forward;
            base.Update(gameTime);
        }
Пример #5
0
        public void Recive(NetIncomingMessage msg)
        {
            msg.Position = 0;
            short opcode = msg.ReadInt16();

            switch (opcode)
            {
            case JoinRoomPacket.OpCode:
            {
                JoinRoomPacket joinPacketInfo = new JoinRoomPacket("");
                string         name           = msg.ReadString();

                GameRoom
                    GameStateWithRooms = NetworkSessionContainer.NetworkSessions.GameRooms.Where(x => x.Room.Name == name).FirstOrDefault();

                UserSession sendingUser = NetworkSessionContainer.NetworkSessions.UserSessions
                                          .Where(x => x.Connection == msg.SenderConnection).FirstOrDefault();

                if (GameStateWithRooms != null)
                {
                    if (GameStateWithRooms.Room.Join(sendingUser))         // jak user sie nie podłączył, wysyła wiadomość osiągnięto limit graczy
                    {
                        Console.WriteLine($"Dołączył {sendingUser.Name} do pokoju {GameStateWithRooms.Room.Name} ");
                        sendingUser.UserGameState = new GameRoomState(sendingUser, GameStateWithRooms);
                    }
                }
                else
                {
                    //Poinformowanie uzytkownika o nie odnaleźieniu pokoju
                }

                if (GameStateWithRooms.Room.RoomMember.Count == 2)
                {
                    Console.WriteLine($"{GameStateWithRooms.Room.Name} Wystartował ");
                    GameStateWithRooms.Start();
                }

                break;
            }

            case CreateRoomPacket.OpCode:
            {
                CreateRoomPacket newRoom = new CreateRoomPacket();
                msg.ReadAllProperties(newRoom);

                if (!NetworkSessionContainer.NetworkSessions.GameRooms.Any(x =>
                                                                           x.Room.RoomMember.Exists(y => y.Connection == msg.SenderConnection) == true))
                {
                    UserSession masterSession =
                        NetworkSessionContainer.NetworkSessions.UserSessions.FirstOrDefault(x =>
                                                                                            x.Connection == msg.SenderConnection);
                    GameRoom newGameRoom = new GameRoom(masterSession, newRoom.Name, 8);

                    newGameRoom.Room.Master = masterSession;
                    //Wyciek pamięci przed zmianą statusu ostatniego gracza trzeba usunąć element z listy
                    //Dać też heart beat jak sypnie to dowidzenia
                    masterSession.UserGameState = new GameRoomState(masterSession, newGameRoom);
                    NetworkSessionContainer.NetworkSessions.GameRooms.Add(newGameRoom);
                }

                break;
            }
            }
        }