Ejemplo n.º 1
0
    private void Update()
    {
        if (netId.localPlayerOwns)
        {
            Movement();
            CameraMovement();
            Shooting();
            if (NetworkClock.IsTimeToSend())
            {
                netId.SendDataOverNetwork(NetworkEventType.UpdatePosition, NetworkSubeventType.Null, transform);
            }
        }

        else
        {
            transform.position = Vector3.Lerp(lastPos, newPos, currentLerp);
            currentLerp       += lerpRatio;

            while (netId.dataQueue.Count != 0)
            {
                NetworkEvent currentData = netId.dataQueue[0];
                netId.dataQueue.RemoveAt(0);
                if (currentData.GetNetworkEventType() == NetworkEventType.UpdatePosition)
                {
                    Vector3[] newTrans = (Vector3[])currentData.GetData();
                    lastPos     = new Vector3(newPos.x, newPos.y, newPos.z);
                    newPos      = newTrans[0];
                    currentLerp = lerpRatio;

                    transform.eulerAngles = newTrans[1];
                    transform.localScale  = newTrans[2];
                }
            }
        }
    }
Ejemplo n.º 2
0
    // Singleton pattern
    private void Awake()
    {
        //Check if instance already exists
        if (Instance == null)
        {
            //if not, set instance to this
            Instance = this;
        }
        //If instance already exists and it's not this:
        else if (Instance != this)
        {
            //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
            Destroy(gameObject);
        }

        //Sets this to not be destroyed when reloading scene
        DontDestroyOnLoad(gameObject);
    }
        // Equivalent to ServerAcceptCallback
        private static void ClientConnectCallback(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState;

            client.EndConnect(ar);

            SendState ss = new SendState();

            ss.workSocket = client;
            sendState     = ss;

            RecvState rs = new RecvState();

            rs.workSocket = client;
            recvState     = rs;

            NetworkClock.StartCommunication();
        }
        // When a connection is accepted, initialize state objects then start queuing data for sending (and get ready to receive)
        private static void ServerAcceptCallback(IAsyncResult ar)
        {
            connectionFound = true;

            Socket listener = (Socket)ar.AsyncState;
            Socket handler  = listener.EndAccept(ar);

            SendState ss = new SendState();

            ss.workSocket = handler;
            sendState     = ss;

            RecvState rs = new RecvState();

            rs.workSocket = handler;
            recvState     = rs;

            NetworkClock.StartCommunication();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets called when the server we are connecting to has resolved our login request
        /// </summary>
        protected virtual void OnServerLoginResponse(PacketLoginResult result)
        {
            try
            {
                if (result.ReplyCode == ReplyType.OK)
                {
                    Roles = result.Parms.GetStringArrayProperty(-1);

                    int numSamplesForClockSync = ConfigHelper.GetIntConfig("NumSamplesForClockSync", 10); // 10 samples
                    int syncTimeAllowed = ConfigHelper.GetIntConfig("ClockSyncTimeAllowed", 15000); // across 15 seconds
                    int timeBetweenSyncs = ConfigHelper.GetIntConfig("TimeBetweenClockSync", 0); // only sync once per session
                    Clock = new NetworkClock(this, numSamplesForClockSync, syncTimeAllowed, timeBetweenSyncs);
                }

                FireServerLoginResultArrived(this, result);
            }
            catch (Exception e)
            {
                Log.LogMsg("Login failure. " + e.Message);
            }
        }