Beispiel #1
0
        void AddSpaceshipToClient(uint clientID)
        {
            if (otherClientsTransformsByID.ContainsKey(clientID))
            {
                return;
            }

            int playerIndex = (spaceshipController.ObjectID == 0) ? 1 : 0;

            EnemySpaceship enemySpaceship = CreateSpaceship(enemySpaceshipPrefabs[playerIndex],
                                                            playerSpawnPoints[playerIndex]) as EnemySpaceship;
            EnemySpaceshipTransformData enemySpaceshipTransformData;

            enemySpaceshipTransformData.spaceship         = enemySpaceship;
            enemySpaceshipTransformData.previousTransform = null;
            enemySpaceshipTransformData.lastTransform     = null;

            TransformPacket transformPacket = new TransformPacket();
            TransformData   transformData;

            transformData.flags           = (int)TransformFlag.None;
            transformData.inputSequenceID = 0;
            transformData.position        = null;
            transformData.rotation        = null;
            transformData.scale           = null;
            transformPacket.Payload       = transformData;

            PacketsManager.Instance.SendPacket(transformPacket,
                                               null,
                                               UdpNetworkManager.Instance.GetSenderID(),
                                               spaceshipController.ObjectID,
                                               reliable: true);

            otherClientsTransformsByID.Add(clientID, enemySpaceshipTransformData);
        }
        void OnDataReceived(ushort userPacketTypeIndex, uint senderID, Stream stream)
        {
            if (userPacketTypeIndex != (ushort)UserPacketType.Transform || senderID != UdpNetworkManager.Instance.GetSenderID())
            {
                return;
            }

            TransformPacket transformPacket = new TransformPacket();

            transformPacket.Deserialize(stream);

            int lastSequenceIDProcessedByServer = inputsSent.FindIndex(ip => ip.Payload.sequenceID == transformPacket.Payload.inputSequenceID);

            if (lastSequenceIDProcessedByServer != -1)
            {
                float[] positionReceived            = transformPacket.Payload.position;
                Vector3 serverAuthoritativePosition = new Vector3(positionReceived[0], positionReceived[1], positionReceived[2]);
                Vector3 accumulatedMovement         = Vector3.zero;

                for (int i = lastSequenceIDProcessedByServer + 1; i < inputsSent.Count; i++)
                {
                    float[] movement       = inputsSent[i].Payload.movement;
                    Vector3 movementVector = new Vector3(movement[0], movement[1], movement[2]);

                    accumulatedMovement += movementVector * Speed * Time.fixedDeltaTime;
                }

                inputsSent.RemoveRange(0, lastSequenceIDProcessedByServer);
                ReconcilePosition(serverAuthoritativePosition, accumulatedMovement);
            }
        }
Beispiel #3
0
    public void UpdateTransform(string transformPacketString)
    {
        Debug.LogWarning(transformPacketString);


        try
        {
            TransformPacket transformPacket = JsonUtility.FromJson <TransformPacket>(transformPacketString);


            if (transformPacket.objectId == objectId)
            {
                newPosition = new Vector3(
                    transformPacket.positionX + 1,
                    transformPacket.positionY,
                    transformPacket.positionZ
                    );

                newRotation = new Vector3(
                    transformPacket.rotationX,
                    transformPacket.rotationY,
                    transformPacket.rotationZ
                    );
            }
        }
        catch (System.Exception e)
        {
            Debug.LogWarning(e);
        }
    }
Beispiel #4
0
        void UpdateServerClients()
        {
            using (var dicIterator = inputsByClientID.GetEnumerator())
                while (dicIterator.MoveNext())
                {
                    TransformPacket    transformPacket = new TransformPacket();
                    TransformData      transformData;
                    EnemySpaceship     enemySpaceship    = dicIterator.Current.Value.spaceship;
                    Vector3            accumulatedInputs = Vector3.zero;
                    List <InputPacket> inputs            = dicIterator.Current.Value.inputs;

                    if (inputs.Count > 0)
                    {
                        for (int i = 0; i < inputs.Count; i++)
                        {
                            float[] movement    = inputs[i].Payload.movement;
                            Vector3 inputVector = new Vector3(movement[0], movement[1], movement[2]);

                            accumulatedInputs += inputVector;
                        }

                        enemySpaceship.Move(accumulatedInputs);

                        float[] spaceshipPosition =
                        {
                            enemySpaceship.transform.position.x,
                            enemySpaceship.transform.position.y,
                            enemySpaceship.transform.position.z,
                        };

                        transformData.flags = (int)TransformFlag.PositionBit | (int)TransformFlag.InputSequenceIDBit;

                        transformData.inputSequenceID = inputs[inputs.Count - 1].Payload.sequenceID;
                        transformData.position        = spaceshipPosition;
                        transformData.rotation        = null;
                        transformData.scale           = null;

                        transformPacket.Payload = transformData;

                        inputs.Clear();
                        PacketsManager.Instance.SendPacket(transformPacket, null, dicIterator.Current.Key, enemySpaceship.ObjectID);
                    }
                }
        }
Beispiel #5
0
        void OnDataReceivedByClient(ushort userPacketTypeIndex, uint senderID, Stream stream)
        {
            if (senderID == UdpNetworkManager.Instance.GetSenderID())
            {
                return;
            }

            if (userPacketTypeIndex == (uint)UserPacketType.Transform)
            {
                AddSpaceshipToClient(senderID);

                TransformPacket transformPacket = new TransformPacket();

                transformPacket.Deserialize(stream);

                EnemySpaceshipTransformData enemySpaceshipTransformData = otherClientsTransformsByID[senderID];

                if (enemySpaceshipTransformData.lastTransform == null ||
                    transformPacket.Payload.inputSequenceID > enemySpaceshipTransformData.lastTransform.Payload.inputSequenceID)
                {
                    enemySpaceshipTransformData.previousTransform = enemySpaceshipTransformData.lastTransform;
                    enemySpaceshipTransformData.lastTransform     = transformPacket;
                    otherClientsTransformsByID[senderID]          = enemySpaceshipTransformData;
                }
            }

            if (userPacketTypeIndex == (uint)UserPacketType.Notification)
            {
                NotificationPacket notificationPacket = new NotificationPacket();

                notificationPacket.Deserialize(stream);

                if (notificationPacket.Payload.playerStatus == (uint)PlayerStatus.Dead)
                {
                    spaceshipController.Die();
                }
            }
        }
    IEnumerator Start()
    {
        while (true)
        {
            TransformPacket transformPacket = new TransformPacket
            {
                objectId  = this.objectId,
                positionX = transform.localPosition.x,
                positionY = transform.localPosition.y,
                positionZ = transform.localPosition.z,

                rotationX = transform.rotation.eulerAngles.x,
                rotationY = transform.rotation.eulerAngles.y,
                rotationZ = transform.rotation.eulerAngles.z
            };

            string transformPacketString = JsonUtility.ToJson(transformPacket);
            OnTransformUpdate.Invoke(transformPacketString);

            Debug.Log(transformPacketString);

            yield return(new WaitForSeconds(.1f));
        }
    }