Ejemplo n.º 1
0
    public void Connect(string IpString, string NickString)
    {
        if (IpString.Length == 0)
        {
            IpString = "127.0.0.1";
        }

        char[] NickArray = NickString.ToCharArray();
        NickArray  = Array.FindAll(NickArray, car => char.IsLetter(car) || car == '_');
        NickString = new string(NickArray);
        if (NickString.Length > 0)
        {
            Nickname = NickString;
        }

        var Peer = new NetworkedMultiplayerENet();

        Peer.CreateClient(IpString, Port);
        GetTree().NetworkPeer = Peer;

        RuntimeRoot.AddChild(WorldScene.Instance());

        DeathScreen = (Node)DeathScreenScene.Instance();
        RuntimeRoot.AddChild(DeathScreen);
    }
Ejemplo n.º 2
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
        }
Ejemplo n.º 3
0
    public void createServer(String serverName, int port, int max_players)
    {
        // Clear out any previous states remain
        closeServer();

        hostType               = "Server";
        serverinfo.name        = serverName;
        serverinfo.used_port   = port;
        serverinfo.max_players = max_players;

        // Initialize the networking system
        NetworkedMultiplayerENet peer = new NetworkedMultiplayerENet();

        peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.Zstd;

        // Try to create the server
        if (peer.CreateServer(serverinfo.used_port, serverinfo.max_players) != Error.Ok)
        {
            // Tell the server crate fail
            EmitSignal(nameof(ServerCreateFailSignal), "SERVER CREATE FAIL AT " + serverName + ":" + port + ". PLEASE CHECK DETAIL AND TRY AGAIN.");
            return;
        }

        //  # Assign it into the tree
        GetTree().NetworkPeer = peer;
        // Tell the server has been created successfully
        EmitSignal(nameof(ServerCreatedSignal));

        registerPlayer(gamestateNetworkPlayer.ToString());
    }
Ejemplo n.º 4
0
    public override void _Ready()
    {
        GetTree().Connect("network_peer_connected", this, "NetworkPeerConnected");
        GetTree().Connect("connected_to_server", this, "ConnectedServer");

        NetworkedMultiplayerENet peer = new NetworkedMultiplayerENet();

        _players = new Dictionary <int, Player>();

        Error err = peer.CreateServer(port, 2);

        GD.Print(err);

        if (err != 0)
        {
            GD.Print("Cliente");
            Error rr = peer.CreateClient(host, port);
            GetTree().SetNetworkPeer(peer);
            return;
        }

        GetTree().SetNetworkPeer(peer);

        int    unique = GetTree().GetNetworkUniqueId();
        string name   = GetTree().GetNetworkUniqueId().ToString();

        SpawnPlayer(unique, name);
    }
Ejemplo n.º 5
0
        public void Disconnect()
        {
            MDLog.Info(LOG_CAT, "Disconnected from server");
            IsSessionStarted = false;
            foreach (int PeerId in Players.Keys)
            {
                MDPlayerInfo Player = Players[PeerId];
                OnPlayerLeftEvent(PeerId);
                Player.RemoveAndFree();
            }

            NetworkedMultiplayerENet peer = GetPeer();

            if (peer != null)
            {
                peer.CloseConnection();
                SetNetworkPeer(null);
            }

            StopUPNP();
            Players.Clear();
            ClearNetworkedNodes();
            OnSessionEndedEvent();
            SceneBuffer.Clear();
        }
Ejemplo n.º 6
0
    public void _ConnectionFailed()
    {
        NetworkedMultiplayerENet peer = (NetworkedMultiplayerENet)GetTree().NetworkPeer;

        peer.CloseConnection();
        GetTree().NetworkPeer = null;
    }
Ejemplo n.º 7
0
    public bool JoinGame(string address, string name)
    {
        if (address.Empty() || name.Empty())
        {
            EmitSignal(nameof(ErrorSignal), "Please enter a name and an address!");
            return(false);
        }
        GD.Print($"Joining game with address {address}");

        PlayerName    = name;
        ServerAddress = address;

        var clientPeer = new NetworkedMultiplayerENet();
        var result     = clientPeer.CreateClient(address, default_port);

        if (result != 0)
        {
            EmitSignal(nameof(ErrorSignal), $"Connection failed! ({result.ToString()})");
            return(false);
        }

        GetTree().NetworkPeer = clientPeer;
        EmitSignal(nameof(SuccessSignal), "Connecting...");

        return(true);
    }
Ejemplo n.º 8
0
 private void onDisconnectPressed()
 {
     GetTree().SetNetworkPeer(null);
     peer    = null;
     players = new List <Node>();
     panel.changeToGray();
 }
Ejemplo n.º 9
0
    public static void Disconnect(bool BuildMenu = true)
    {
        World.Close();

        NetworkedMultiplayerENet EN = Self.GetTree().GetNetworkPeer() as NetworkedMultiplayerENet;

        if (EN != null)
        {
            EN.CloseConnection();
        }

        Self.GetTree().SetNetworkPeer(null);
        PeerList.Clear();
        Net.Players.Clear();
        Nicknames.Clear();
        Net.Players.Clear();

        IsWaitingForServer    = false;
        WaitingForServerTimer = MaxWaitForServerDelay;

        if (BuildMenu)
        {
            Menu.BuildMain();
        }
    }
Ejemplo n.º 10
0
        public bool StartServer(int Port, int MaxPlayers = DEFAULT_MAX_PLAYERS)
        {
            NetworkedMultiplayerENet peer = new NetworkedMultiplayerENet();

            peer.Connect("peer_connected", this, nameof(ServerOnPeerConnected));
            peer.Connect("peer_disconnected", this, nameof(ServerOnPeerDisconnected));
            ConfigurePeer(peer);

            Error error   = peer.CreateServer(Port, MaxPlayers);
            bool  Success = error == Error.Ok;

            MDLog.CLog(Success, LOG_CAT, MDLogLevel.Info,
                       $"Starting server on port {Port} with {MaxPlayers} max players.");
            MDLog.CLog(!Success, LOG_CAT, MDLogLevel.Error, $"Failed to start server on port {Port}");

            if (Success)
            {
                UPNPPort = Port;
                SetNetworkPeer(peer);
                ServerOnStarted();
            }
            else
            {
                MDLog.Error(LOG_CAT, "Failed to start server");
                OnSessionFailedEvent();
            }

            return(Success);
        }
Ejemplo n.º 11
0
    public static void Host()
    {
        if (Self.GetTree().NetworkPeer != null)
        {
            if (Self.GetTree().IsNetworkServer())
            {
                Console.ThrowPrint("Cannot host when already hosting");
            }
            else
            {
                Console.ThrowPrint("Cannot host when connected to a server");
            }
            return;
        }

        PeerList.Clear();
        World.Start();

        NetworkedMultiplayerENet Peer = new NetworkedMultiplayerENet();

        Peer.CreateServer(Port, Game.MaxPlayers);
        Self.GetTree().SetNetworkPeer(Peer);
        Self.GetTree().SetMeta("network_peer", Peer);

        Console.Log($"Started hosting on port '{Port}'");

        PeerList.Add(Self.GetTree().GetNetworkUniqueId());
        Nicknames[ServerId] = Game.Nickname;
        Game.SpawnPlayer(Self.GetTree().GetNetworkUniqueId(), true);

        World.DefaultPlatforms();
    }
Ejemplo n.º 12
0
    public static void Host()
    {
        if (Self.GetTree().NetworkPeer != null)
        {
            if (Self.GetTree().IsNetworkServer())
            {
                Console.Print("Error: Cannot host when already hosting");
            }
            else
            {
                Console.Print("Error: Cannot host when connected to a server");
            }
            return;
        }

        PeerList.Clear();
        Game.StartWorld(AsServer: true);

        NetworkedMultiplayerENet Peer = new NetworkedMultiplayerENet();

        Peer.CreateServer(Port, Game.MaxPlayers);
        Self.GetTree().SetNetworkPeer(Peer);
        Self.GetTree().SetMeta("network_peer", Peer);

        Console.Log("Started hosting on port '" + Port.ToString() + "'");

        PeerList.Add(Self.GetTree().GetNetworkUniqueId());
        Game.SpawnPlayer(Self.GetTree().GetNetworkUniqueId(), true);
    }
Ejemplo n.º 13
0
    private void onConnectPressed()
    {
        players = new List <Node>();
        peer    = new NetworkedMultiplayerENet();
        int    port    = System.Convert.ToInt32(panel.GetNode <TextEdit>("Port").Text);
        String address = panel.GetNode <TextEdit>("Address").Text;

        if (panel.GetNode <OptionButton>("OptionButton").GetSelectedId() == 0)
        {
            peer.CreateServer(port, maxPlayers);
            GetTree().SetNetworkPeer(peer);

            panel.changeToRed();
            players.Add(myself);
            myself = playerScene.Instance();
            myself.GetNode <RigidBody2D>("Ridiculous").Position = new Vector2(random.Next(20, 380), random.Next(100, 335));
            myself.SetNetworkMaster(GetTree().GetNetworkUniqueId());
            myself.SetName(GetTree().GetNetworkUniqueId().ToString());
            GetNode <Panel>("Panel").AddChild(myself);
        }
        else
        {
            peer.CreateClient(address, port);
            GetTree().SetNetworkPeer(peer);
            panel.changeToBlue();

            myself = playerScene.Instance();
            myself.GetNode <RigidBody2D>("Ridiculous").Position = new Vector2(random.Next(20, 380), random.Next(100, 335));
            myself.SetNetworkMaster(GetTree().GetNetworkUniqueId());
            myself.SetName(GetTree().GetNetworkUniqueId().ToString());
            GetNode <Panel>("Panel").AddChild(myself);
        }
    }
Ejemplo n.º 14
0
    public void ServerDisconnected()
    {
        Players.Clear();
        GetTree().NetworkPeer = null;

        var   clientPeer = new NetworkedMultiplayerENet();
        Error result     = 0;

        for (int i = 0; i < 10; i++)
        {
            result = clientPeer.CreateClient(ServerAddress, default_port);

            if (result != 0)
            {
                EmitSignal(nameof(ErrorSignal), $"Connection failed! ({result.ToString()})");
                System.Threading.Thread.Sleep(1000);
            }
            else
            {
                break;
            }
        }

        if (result == 0)
        {
            GetTree().NetworkPeer = clientPeer;
            EmitSignal(nameof(SuccessSignal), "Connecting...");
            return;
        }

        EmitSignal(nameof(ErrorSignal), "Disconnected from the server");
    }
Ejemplo n.º 15
0
    public void onClickJoin()
    {
        //get data
        string ip = lineIP.GetText();

        if (!int.TryParse(linePort.GetText(), out port))
        {
            port = 8920;
        }

        string name = lineName.GetText();

        if (name.Empty())
        {
            name = "NoName";
        }

        myData       = new DataPlayer(0, name); //data model example
        myData.color = new Random().Next(0, 3); //de 0,1,2

        //view
        panelStart.SetVisible(false);
        panelChat.SelfModulate = new Color(.2f, .2f, 0.5f);//blue style
        back.SetVisible(true);
        chat.BbcodeText = ("[color=purple]Connecting to " + ip + " : " + port + " ...[/color]");

        //CONNECTION
        var peer = new NetworkedMultiplayerENet();

        peer.CreateClient(ip, port);
        GetTree().SetNetworkPeer(peer);

        myData.id = GetTree().GetNetworkUniqueId();//get ID
    }
Ejemplo n.º 16
0
    //ONCLICK START CLIENT OR SERVER:
    public void onClickHost()
    {
        //IF NEED WORLD, LOAD HERE (BEFORE START SERVER)
        //LoadWorld();

        //Get view data
        if (!int.TryParse(linePort.GetText(), out port))
        {
            port = 8920;
        }

        if (!int.TryParse(lineMax.GetText(), out maxPlayers))
        {
            maxPlayers = 4;//4 when parse fail
        }

        //CREATE SERVER
        var   peer = new NetworkedMultiplayerENet();
        Error err  = peer.CreateServer(port, maxPlayers);

        if (err != Error.Ok)
        {
            GD.Print("YOU CAN NOT CREATE SERVER:" + err.ToString());
            return;
        }
        GetTree().SetNetworkPeer(peer);

        //set View
        panelChat.SelfModulate = new Color(1f, 0, 0); //red style
        panelStart.SetVisible(false);                 //server cant use chat

        chat.BbcodeText = ("[color=red]SERVER READY![/color]");
    }
Ejemplo n.º 17
0
    public void ConnectTo(string InIp, int port)
    {
        //Set static string Ip
        NetworkedMultiplayerENet Peer = new NetworkedMultiplayerENet();

        Peer.CreateClient(InIp, port);
        GetTree().NetworkPeer = Peer;
    }
Ejemplo n.º 18
0
 public void receive_server_disconnected()
 {
     LOG.Warn("Connection to server was lost");
     GetTree().NetworkPeer = null;
     ENetInstance          = null;
     dispatcher            = null;
     ObjectBroker.Instance.SceneService.LoadUI("MainMenu/MainMenu.tscn");
 }
Ejemplo n.º 19
0
 private void ConfigurePeer(NetworkedMultiplayerENet Peer)
 {
     Peer.ServerRelay     = MDPeerConfigs.ServerRelay;
     Peer.AlwaysOrdered   = MDPeerConfigs.AlwaysOrdered;
     Peer.ChannelCount    = MDPeerConfigs.ChannelCount;
     Peer.CompressionMode = MDPeerConfigs.CompressionMode;
     Peer.TransferChannel = MDPeerConfigs.TransferChannel;
 }
    //creates a peer and attempts to connect a server
    public void ConnectClient(string address, int port)
    {
        NetworkedMultiplayerENet peer = new NetworkedMultiplayerENet();

        peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.Zlib;
        peer.CreateClient(address, port);
        GetTree().NetworkPeer = peer;
        GD.Print($"connecting to server {address}:{port}");
    }
Ejemplo n.º 21
0
    private void CreateServer()
    {
        NetworkedMultiplayerENet peer = new NetworkedMultiplayerENet();

        peer.CompressionMode = NetworkedMultiplayerENet.CompressionModeEnum.Zlib;
        peer.CreateServer(server_port, max_players);
        GetTree().NetworkPeer = peer;
        GD.Print("server listening on 3074");
    }
Ejemplo n.º 22
0
 public void InitClient(string address, Godot.Object obj, string success, string fail)
 {
     GetTree().Connect("connected_to_server", obj, success);
     GetTree().Connect("connection_failed", obj, fail);
     peer = new Godot.NetworkedMultiplayerENet();
     peer.CreateClient(address, DefaultPort);
     this.GetTree().SetNetworkPeer(peer);
     selfPeerId = this.GetTree().GetNetworkUniqueId();
 }
Ejemplo n.º 23
0
 public void InitServer(Godot.Object obj, string playerJoin, string playerLeave)
 {
     peer = new Godot.NetworkedMultiplayerENet();
     this.GetTree().Connect("network_peer_connected", obj, playerJoin);
     this.GetTree().Connect("network_peer_disconnected", obj, playerLeave);
     peer.CreateServer(DefaultPort, MaxPlayers);
     this.GetTree().SetNetworkPeer(peer);
     selfPeerId = this.GetTree().GetNetworkUniqueId();
 }
Ejemplo n.º 24
0
        private void ConnectToServer()
        {
            _network = new NetworkedMultiplayerENet();
            _network.CreateClient(_ip, _port);
            GetTree().NetworkPeer = _network;

            _network.Connect("connection_failed", this, "_OnConnectionFailed");
            _network.Connect("connection_succeeded", this, "_OnConnectionSucceeded");
        }
Ejemplo n.º 25
0
    public static void ConnectTo(string InIp)
    {
        Ip = InIp;

        NetworkedMultiplayerENet Peer = new NetworkedMultiplayerENet();

        Peer.CreateClient(Ip, Port);
        Self.GetTree().SetNetworkPeer(Peer);
        Self.GetTree().SetMeta("network_peer", Peer);
    }
Ejemplo n.º 26
0
    public void JoinGame()
    {
        var address = AddressText.Text;

        GD.Print($"Joining game with address {address}");

        var clientPeer = new NetworkedMultiplayerENet();
        var result     = clientPeer.CreateClient(address, default_port);

        GetTree().NetworkPeer = clientPeer;
    }
Ejemplo n.º 27
0
    public void OnConnectPressed()
    {
        ClientVariables.SaveMainMenu();
        NetworkedMultiplayerENet peer = new NetworkedMultiplayerENet();
        Error result = peer.CreateClient(ClientVariables.NetworkOptions.IPAddress, ClientVariables.NetworkOptions.Port);

        if (result == Error.Ok)
        {
            GetTree().NetworkPeer = peer;
        }
    }
Ejemplo n.º 28
0
    private void JoinGame()
    {
        GD.Print("Joining network");
        HideAll();
        label.Text = "Joining Game";

        NetworkedMultiplayerENet host = new NetworkedMultiplayerENet();

        host.CreateClient(hostip.Text, PORT_NUM);
        GetTree().SetNetworkPeer(host);
    }
Ejemplo n.º 29
0
    public void hostGame(String newName)
    {
        // Assign new name.
        playerName = newName;

        // Create server
        NetworkedMultiplayerENet host = new NetworkedMultiplayerENet();

        host.CreateServer(PORT, MAX_PLAYERS);
        GetTree().NetworkPeer = host;
    }
Ejemplo n.º 30
0
    public void joinGame(String IPAddress, String newName)
    {
        // Assign new data
        playerName = newName;

        // Create client
        NetworkedMultiplayerENet host = new NetworkedMultiplayerENet();

        host.CreateClient(IPAddress, PORT);
        GetTree().NetworkPeer = host;
    }