Ejemplo n.º 1
0
        public void Start(int NumOfPlayers, Program.Map Map)
        {
            IPEndPoint ipPoint      = new IPEndPoint(IPAddress.Parse(HostIP), Port);
            Socket     ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            string Title = $"Waiting for Players to connect - {NumOfPlayers - 1 - ConnectedPlayers.Count}";

            string[] NullArray = new string[0];
            Interface.AnswerInterface(Title, NullArray);
            Console.CursorTop += 2;
            int OrigTop = Console.CursorTop;

            ListenSocket.Bind(ipPoint);
            ListenSocket.Listen(NumOfPlayers - 1);
            while (ConnectedPlayers.Count < NumOfPlayers - 1)
            {
                Socket newPlayer = ListenSocket.Accept();
                ConnectedPlayers.Add(newPlayer, ReceiveData(newPlayer) as Player);

                SendLobbyInfo(NumOfPlayers, Map);

                Title = $"Waiting for Players to connect - {NumOfPlayers - 1 - ConnectedPlayers.Count}";
                Interface.AnswerInterfaceTitleChange(Title);

                Console.SetCursorPosition(6, OrigTop + 2 * ConnectedPlayers.Count - 1);
                Console.Write("\x4 {0} - {1} is connected.", (char)ConnectedPlayers.Count, ConnectedPlayers.Last().Key.AddressFamily.ToString());
            }
            Console.Clear();
            PlayerInitializing();

            List <Player> Players = ConnectedPlayers.Values.ToList();

            Players.Add(HostPlayer);

            Game         = new Multiplayer.MultiplayerGame();
            Game.Map     = Map;
            Game.Players = Players;

            Game.StartAsync();
            while (Game.State == false)
            {
            }
            while (true)
            {
                SendGameInfo();
                HostPlayer.MovementAsync(Map);//Host player movement
                for (int i = 0; i < ConnectedPlayers.Count; i++)
                {
                    PlayerMovement(ConnectedPlayers.Keys.ToArray()[i]);
                }
                Players = ConnectedPlayers.Values.ToList();
                Players.Add(HostPlayer);
                Game.Players = Players;
            }
        }
Ejemplo n.º 2
0
        public void Connect()
        {
            IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);

            Socket ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            ClientPlayer = new Player();

            ClientSocket.Connect(ipPoint);
            SendPlayerPos(ClientSocket);

            Server.LobbyInfo LobbyInfo = ReceiveGameInfo(ClientSocket, false) as Server.LobbyInfo; //Creating Lobby

            string[] NullArray = new string[0];
            string   Title     = $"Waiting for Players to connect - {LobbyInfo.NumberOfPlayers - 1 - LobbyInfo.ConnectedPlayers.Count}";

            Interface.AnswerInterface(Title, NullArray);//Lobby Initialization
            Console.CursorTop += 2;
            int OrigTop = Console.CursorTop;

            for (int i = 0; i < LobbyInfo.ConnectedPlayers.Count; i++)
            {
                Console.SetCursorPosition(6, OrigTop + 2 * i);
                Console.Write("\x4 {0} - is connected.", (char)(i + 1));
            }

            int NumOfPlayersToConnect = LobbyInfo.NumberOfPlayers - 1 - LobbyInfo.ConnectedPlayers.Count;

            for (int i = 0; i < NumOfPlayersToConnect; i++)
            {
                LobbyInfo = ReceiveGameInfo(ClientSocket, false) as Server.LobbyInfo;
                Title     = $"Waiting for Players to connect - {NumOfPlayersToConnect - i}";
                Interface.AnswerInterfaceTitleChange(Title);
                Console.SetCursorPosition(6, OrigTop + 2 * LobbyInfo.ConnectedPlayers.Count - 1);
                Console.Write("\x4 {0} - is connected.", (char)LobbyInfo.ConnectedPlayers.Count);
            }
            Console.Clear();                                                                         //End of Lobby stage

            ClientPlayer = ReceiveGameInfo(ClientSocket, false) as Player;                           //Player registration

            GameInfo = ReceiveGameInfo(ClientSocket, false) as Multiplayer.MultiplayerGame.GameInfo; //Game registration
            while (true)
            {
                GameInfo = ReceiveGameInfo(ClientSocket) as Multiplayer.MultiplayerGame.GameInfo;

                ClientPlayer.MovementAsync(GameInfo.Map);
                SendPlayerPos(ClientSocket);

                Interface.MultiplayerMapDraw(GameInfo.Map, GameInfo.Players, GameInfo.Food);
                Interface.MultiplayerShowPoints(GameInfo.Map, GameInfo.Players);
            }
        }