Exemple #1
0
        public override void Callback(int peerId, AbstractNetworkMessage abstractNetworkMessage)
        {
            ClientPeerConnectionUpdate clientPeerConnectionUpdate = abstractNetworkMessage as ClientPeerConnectionUpdate;

            if (Tree.IsNetworkServer())
            {
                if (clientPeerConnectionUpdate.Disconnected)
                {
                    ClientRepresentation disconnectedPeer = Server.ConnectedClients[peerId];
                    //disconnect actual peer
                    Server.ENetInstance.DisconnectPeer(peerId, true);
                    foreach (var peer in Server.ConnectedClients)
                    {
                        if (peer.Key != peerId)
                        {
                            ObjectBroker.Instance.NetworkService.toClient(peer.Key, new ClientPeerConnectionUpdate(disconnectedPeer, true));
                        }
                    }
                }
            }
            else
            {
                if (!clientPeerConnectionUpdate.Disconnected)
                {
                    if (Tree.Root.GetNode(SceneService.NODE_PATH_SCENE).HasNode(GD.Str(clientPeerConnectionUpdate.ClientRepresentation.Id)))
                    {
                        return;
                    }

                    PackedScene         peerRepresentationResource = ResourceLoader.Load("res://assets/scenes/Character/Character.tscn") as PackedScene;
                    Godot.KinematicBody peerRepresentation         = peerRepresentationResource.Instance() as Godot.KinematicBody;
                    peerRepresentation.Name = GD.Str(clientPeerConnectionUpdate.ClientRepresentation.Id);
                    peerRepresentation.SetNetworkMaster(clientPeerConnectionUpdate.ClientRepresentation.Id);
                    peerRepresentation.Translation = clientPeerConnectionUpdate.ClientRepresentation.Translation;
                    peerRepresentation.Rotation    = clientPeerConnectionUpdate.ClientRepresentation.Rotation;

                    Node peerName = peerRepresentation.GetNode("Name");
                    ((Label)peerName.GetNode("Viewport/Label")).Text = clientPeerConnectionUpdate.ClientRepresentation.Name;

                    Tree.Root.GetNode(SceneService.NODE_PATH_SCENE).AddChild(peerRepresentation);
                }
                else
                {
                    Node peerNode = Tree.Root.GetNode(SceneService.NODE_PATH_SCENE).GetNode(GD.Str(clientPeerConnectionUpdate.ClientRepresentation.Id));
                    Tree.Root.GetNode(SceneService.NODE_PATH_SCENE).RemoveChild(peerNode);
                    peerNode.QueueFree();
                }
            }
        }
        public override void Callback(int peerId, AbstractNetworkMessage abstractNetworkMessage)
        {
            ServerConnetedClientsSync serverConnetedClientsSync = abstractNetworkMessage as ServerConnetedClientsSync;

            if (Tree.IsNetworkServer())
            {
                serverConnetedClientsSync.ConnectedClients = new Dictionary <int, Vector3>();
                foreach (var entry in Server.ConnectedClients)
                {
                    if (entry.Key != peerId)
                    {
                        serverConnetedClientsSync.ConnectedClients.Add(entry.Key, entry.Value.Translation);
                    }
                }
                serverConnetedClientsSync.Action = NetworkMessageAction.SERVER_CONNECTED_CLIENTS_SYNC;
                ObjectBroker.Instance.NetworkService.toClient(peerId, serverConnetedClientsSync, TransferModeEnum.Reliable);
            }
            else
            {
                foreach (var entry in serverConnetedClientsSync.ConnectedClients)
                {
                    if (Tree.Root.GetNode(SceneService.NODE_PATH_SCENE).HasNode(GD.Str(entry.Key)))
                    {
                        LOG.Warn(System.String.Format("Node with name {0} is already present in the current client scene", entry.Key));
                    }

                    PackedScene         peerRepresentationResource = ResourceLoader.Load("res://assets/scenes/Character/Character.tscn") as PackedScene;
                    Godot.KinematicBody peerRepresentation         = peerRepresentationResource.Instance() as Godot.KinematicBody;
                    peerRepresentation.Name = GD.Str(entry.Key);
                    peerRepresentation.SetNetworkMaster(entry.Key);
                    peerRepresentation.Translation = entry.Value;
                    Tree.Root.GetNode(SceneService.NODE_PATH_SCENE).AddChild(peerRepresentation);
                }
                ObjectBroker.Instance.NetworkService.toServer(new ServerCompletedSync(), TransferModeEnum.Reliable);
            }
        }