Beispiel #1
0
        void _OnHostPressed()
        {
            //Create peer
            this.peer = new NetworkedMultiplayerENet();
            this.peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.RangeCoder;
            Error error = this.peer.CreateServer(PORT, 8);

            if (error != Error.Ok)
            {
                this.ShowError("Error: Unable to host server\nPerhaps there is alreay a server open?");
                return;
            }

            this.p1Id             = 1;
            this.p2Id             = 0;
            GetTree().NetworkPeer = this.peer;
            Lobby.state           = GameState.CHAR_SELECTION;
            //Hosts should start out as a spectator
            Lobby.role        = MultiplayerRole.P1;
            Lobby.highLatency = false;

            this.Visible = false;

            //Goto CS as spectator
            CharSelection cs = (ResourceLoader.Load("res://scenes/ui/char_selection/char_selection.tscn") as PackedScene)
                               .Instance() as CharSelection;

            GetTree().Root.AddChild(cs);
            //Set the character selection up for multiplayer, with this as a spectator
            cs.SetMp();
            cs.SetCallback(this._MpCSCallback);
            cs.PlayersUpdated(true, false);     //Get the player connected sprites set correctly
        }
Beispiel #2
0
 /// <summary>
 /// Create new connection to the server with a certain multiplayer role
 /// </summary>
 /// <returns>True on connection established, false on otherwise</returns>
 public bool CreateConnection(MultiplayerRole role)
 {
     _role       = role;
     _connection = new MultiplayerConnection(role, _settings);
     _connection.Establish();
     return(_connection.IsConnectionEstablished);
 }
        /// <summary>
        /// Merge with current multiplayer data object with a json object
        /// </summary>
        /// <param name="jsonString">JSON object with expected fields for player data</param>
        /// <param name="role">Role of multiplayer game connection (host vs guest)</param>
        public void Merge(Json json, MultiplayerRole role)
        {
            List <string> board = new List <string>();
            int           time  = json.ReadInteger("time");
            int           flag  = json.ReadInteger("flag");

            json.ReadArray("board", ref board);

            Merge(time, flag, board.ToArray(), role);
        }
    private static MultiplayerRole getMultiplayerRole()
    {
        MultiplayerRole role = MultiplayerRole.Server;

        GameStorage <MultiplayerRole> selectedMultiplayerRoleStorage = GameStorageKeys.SelectedMultiplayerRole;

        if (selectedMultiplayerRoleStorage.Exists())
        {
            role = selectedMultiplayerRoleStorage.Get();
        }

        return(role);
    }
Beispiel #5
0
        //Host a regular, local game
        void _OnLocalPressed()
        {
            Lobby.role  = MultiplayerRole.OFFLINE;
            Lobby.state = GameState.CHAR_SELECTION;
            this.p1Id   = 0;
            this.p2Id   = 0;
            //Move to char selection
            this.Visible = false;
            CharSelection cs = (ResourceLoader.Load("res://scenes/ui/char_selection/char_selection.tscn") as PackedScene)
                               .Instance() as CharSelection;

            cs.SetCallback(this._LocalCSCallback);
            GetTree().Root.AddChild(cs);
        }
        /// <summary>
        /// Merge with current multiplayer data object with all needed data
        /// </summary>
        /// <param name="time">Time played for certain role</param>
        /// <param name="flag">Flags remaining for certain role</param>
        /// <param name="board">Stringified board for merging</param>
        /// <param name="role">Role of multiplayer game connection (host vs guest)</param>
        public void Merge(int time, int flag, string[] board, MultiplayerRole role)
        {
            switch (role)
            {
            case MultiplayerRole.Guest:
                Guest.Time = time;
                Guest.Flag = flag;
                Guest.Board.MergeBoard(board);
                break;

            case MultiplayerRole.Host:
                Host.Time = time;
                Host.Flag = flag;
                Host.Board.MergeBoard(board);
                break;
            }
        }
Beispiel #7
0
        void _OnJoinPressed()
        {
            String ip = IP.ResolveHostname(nodeAddr.Text);

            if (ip == "")
            {
                this.ShowError("Error: Invalid host!");
                return;
            }

            //Create host
            this.peer = new NetworkedMultiplayerENet();
            this.peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.RangeCoder;
            this.peer.CreateClient(ip, PORT);
            GetTree().NetworkPeer = this.peer;

            this.ShowError("Connecting...", "Cancel");
            Lobby.state = GameState.WAITING;
            Lobby.role  = MultiplayerRole.SPECTATOR;
        }
Beispiel #8
0
        //Reset all the way back to title screen
        public void Reset()
        {
            //Unpause game (if it is paused)
            GetTree().Paused = false;

            this.RemoveAll();

            //Make cursor visible
            Input.SetMouseMode(Input.MouseMode.Visible);

            //If online close connection
            if (Lobby.role != MultiplayerRole.OFFLINE)
            {
                this.peer.CloseConnection();
            }
            Lobby.state = GameState.TITLE;
            Lobby.role  = MultiplayerRole.OFFLINE;

            //Reset everything
            GetTree().ChangeScene("res://scenes/ui/lobby.tscn");
        }
        public MultiplayerConnection(MultiplayerRole role, GameSettings settings)
        {
            _uploadTask   = null;
            _downloadTask = null;
            _role         = role;
            _stopWatch    = new StopWatch.StopWatch();
            CurrentData   = new MultiplayerData(settings);

            // set initial data
            IsConnectionClosed = false;

            switch (role)
            {
            case MultiplayerRole.Host:
                (CurrentData.Host.Board as MinesweeperBoard).PopulateBomb();
                break;

            case MultiplayerRole.Guest:
                (CurrentData.Guest.Board as MinesweeperBoard).PopulateBomb();
                break;
            }
        }
Beispiel #10
0
        /*
         *      Which of these is called when?
         *      If a player has changed -> Call SetPlayerId(...) on all clients
         *      If a player hasn't changed -> Call SetupClient on just the new spectator
         */

        //Called whenever the game has to be setup
        //This is called internally when the players have changed
        //Or by the server if you join late as a spectator
        private void SetupClient(int p1Id, int p2Id, bool highLatency)
        {
            //Set the role correctly
            if (p1Id == GetTree().GetNetworkUniqueId())
            {
                Lobby.role = MultiplayerRole.P1;
            }
            else if (p2Id == GetTree().GetNetworkUniqueId())
            {
                Lobby.role = MultiplayerRole.P2;
            }
            else
            {
                Lobby.role = MultiplayerRole.SPECTATOR;
            }

            Lobby.highLatency = highLatency;

            //Set the player ids
            this.p1Id = p1Id;
            this.p2Id = p2Id;
            //It doesn't matter what state you were in, once this is called you're in char selections
            Lobby.state = GameState.CHAR_SELECTION;

            //Case reseting lobby
            CharSelection cs = (ResourceLoader.Load("res://scenes/ui/char_selection/char_selection.tscn") as PackedScene)
                               .Instance() as CharSelection;

            GetTree().Root.AddChild(cs);
            //Set the character selection up for multiplayer, with this as a spectator
            cs.SetMp();
            cs.SetCallback(this._MpCSCallback);
            cs.PlayersUpdated(p1Id != 0, p2Id != 0);

            this.ResetSpectators();
        }
Beispiel #11
0
 /// <summary>
 /// Get multiplayer page but must be awaited because the main thread must not be blocked
 /// when multiplayer page is processing in this method
 /// </summary>
 /// <param name="mainMenuPage">Exit page from multiplayer page</param>
 /// <param name="role">Role of multiplayer connection</param>
 /// <returns>New multiplayer page</returns>
 public async Task <MultiplayerPage> GetMultiplayerPage(GraphicalPage mainMenuPage, MultiplayerRole role)
 => await Task.Run(() =>
 {
     while (_settings == null)
     {
     }
     var page = new MultiplayerPage(_settings)
     {
         PreviousPage = mainMenuPage
     };
     page.CreateConnection(role);
     return(page);
 });
 /// <summary>
 /// Merge with current multiplayer data object with a json string
 /// </summary>
 /// <param name="jsonString">JSON string with expected fields for player data</param>
 /// <param name="role">Role of multiplayer game connection (host vs guest)</param>
 public void Merge(string jsonString, MultiplayerRole role) => Merge(SplashKit.CreateJson(jsonString), role);