示例#1
0
        public IEnumerator SendPositionUpdates()
        {
            while (InGame)
            {
                var pos = GameRoom.clientGoT.position;

                if (pos.x != GameRoom.clientGoScript.px || pos.y != GameRoom.clientGoScript.py)
                {
                    ENetNetwork.Send(PacketType.ClientPositionUpdate, PacketFlags.None, pos.x, pos.y);
                }

                GameRoom.clientGoScript.px = pos.x;
                GameRoom.clientGoScript.py = pos.y;

                yield return(new WaitForSeconds(POSITION_UPDATE_DELAY / 1000));
            }
        }
示例#2
0
        private bool WantsToQuit()
        {
            IsQuitting = true;

            if (Host == null) // Might press player then stop in Unity editor before client is created
            {
                return(false);
            }

            ENetNetwork.Send(PacketType.ClientDisconnect, PacketFlags.Reliable);

            if (Connected)
            {
                StartCoroutine(Quitting());
                return(false);
            }

            return(true);
        }
示例#3
0
        public void SpawnClient()
        {
            clientGo       = Instantiate(oClientPrefab, Vector3.zero, Quaternion.identity);
            clientGo.name  = $"mClient (You)";
            clientGoScript = clientGo.AddComponent <ClientBehaviour>();
            clientGoT      = clientGo.transform;
            var ui = Instantiate(oClientCanvasPrefab, Vector3.zero, Quaternion.identity);

            ui.GetComponent <Canvas>().worldCamera = Camera.main;
            ui.transform.SetParent(clientGo.transform);
            ui.transform.position = new Vector3(0, 0.35f, 0);
            ui.GetComponentInChildren <TMP_Text>().text = ENetClient.myName;

            var pos = clientGoT.position;

            ENetNetwork.Send(PacketType.ClientPositionUpdate, PacketFlags.Reliable, pos.x, pos.y);

            // We are in the game, we are ready to receive the initial positions of all the other clients
            ENetNetwork.Send(PacketType.ClientRequestPositions, PacketFlags.Reliable);

            ENetClient.InGame = true;
        }
示例#4
0
        private void UpdateENet()
        {
            if (Host == null)
            {
                return;
            }

            ENet.Event netEvent;

            if (!Running && !Disconnecting)
            {
                Disconnecting = true;
                ENetNetwork.Send(PacketType.ClientDisconnect, PacketFlags.Reliable);
                return;
            }

            if (Host.CheckEvents(out netEvent) <= 0)
            {
                if (Host.Service(15, out netEvent) <= 0)
                {
                    return;
                }
            }

            switch (netEvent.Type)
            {
            case ENet.EventType.None:
                break;

            case ENet.EventType.Connect:
                //Debug.Log("Client connected to server");
                Connected = true;
                break;

            case ENet.EventType.Disconnect:
                Debug.Log("Client disconnected from server");
                Connected = false;

                GameRoom.clients.Remove(myID);

                if (!IsQuitting)
                {
                    Destroy(gameObject);
                    CleanUp();
                    SceneManager.LoadScene("Main Menu");
                    Disconnecting = false;
                }

                break;

            case ENet.EventType.Timeout:
                Debug.Log("Client connection timeout");

                if (GameRoom.clients != null)
                {
                    GameRoom.clients.Remove(myID);
                }

                string activeScene = SceneManager.GetActiveScene().name;
                if (activeScene == "Main")
                {
                    SceneManager.LoadScene("Main Menu");
                    Destroy(gameObject);
                    CleanUp();
                }

                if (activeScene == "Account Management")
                {
                    UIAccountManagement.ConnectingENet = false;
                    UIAccountManagement.UpdateText("Failed to connect to ENet server");
                }

                break;

            case ENet.EventType.Receive:
                //Debug.Log("Packet received from server - Channel ID: " + netEvent.ChannelID + ", Data length: " + netEvent.Packet.Length);
                HandlePacket(ref netEvent);
                netEvent.Packet.Dispose();
                break;
            }
        }