示例#1
0
        private static void processRigidbodyMessage(byte[] msg, bool clientMessage)
        {
            short netID = ObjectSerializer.deserializeShort(ref msg);

            if (NetworkEntityManager.Instance.findComponent(netID, out NetworkTransform networkTransform)) //NetworkTransform found
            {
                bool receivedVelocity = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedVelocity) //Velocity data was sent
                {
                    float velX = ObjectSerializer.deserializeFloat(ref msg);
                    float velY = ObjectSerializer.deserializeFloat(ref msg);
                    float velZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Velocity = new Vector3(velX, velY, velZ);
                }

                bool receivedAngularVelocity = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedAngularVelocity) //Angular velocity data was sent
                {
                    float aVelX = ObjectSerializer.deserializeFloat(ref msg);
                    float aVelY = ObjectSerializer.deserializeFloat(ref msg);
                    float aVelZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.AngularVelocity = new Vector3(aVelX, aVelY, aVelZ);
                }

                //Message was sent from a client, now being processed on the server
                if (clientMessage && ServerBehaviour.Instance)
                {
                    short  clientID     = ObjectSerializer.deserializeShort(ref msg); //Client who sent the message
                    byte[] rigidbodyMsg = MessageFactory.createRigidbodyMessage(networkTransform.networkIdentity, networkTransform.rb, receivedVelocity, receivedAngularVelocity);
                    ServerBehaviour.Instance.sendMessage(rigidbodyMsg, clientID);     //Forward to all clients but the sender
                }
            }
        }
示例#2
0
        private static void processVector3SyncVarMessage(byte[] msgBytes, bool clientMessage)
        {
            short networkID = ObjectSerializer.deserializeShort(ref msgBytes);

            //Decompress the bytes which were compressed
            byte[] decompressedBytes = ObjectSerializer.deserializeBytes(ref msgBytes).decompress();

            System.Type objType   = ObjectSerializer.deserializeObject <System.Type>(ref decompressedBytes);
            string      fieldName = ObjectSerializer.deserializeString(ref decompressedBytes);

            if (NetworkEntityManager.Instance.findComponent(networkID, out NetworkIdentity netIdentity))
            {
                if (netIdentity.getNetworkBehaviour(objType, out NetworkBehaviour netBehaviour))
                {
                    Vector3 value = new Vector3();
                    value.x = ObjectSerializer.deserializeFloat(ref msgBytes);
                    value.y = ObjectSerializer.deserializeFloat(ref msgBytes);
                    value.z = ObjectSerializer.deserializeFloat(ref msgBytes);

                    netBehaviour.setSyncVar(objType, fieldName, value);

                    //Message was sent from a client, now being processed on the server
                    if (clientMessage && ServerBehaviour.Instance)
                    {
                        short  clientID     = ObjectSerializer.deserializeShort(ref msgBytes); //Client who sent the message
                        byte[] v3SyncVarMsg = MessageFactory.createVector3SyncVarMessage(netBehaviour, objType, fieldName, value);
                        ServerBehaviour.Instance.sendMessage(v3SyncVarMsg, clientID);          //Forward to all clients but the sender
                    }
                }
            }
        }
示例#3
0
        private static void processTransformMessage(byte[] msg, bool clientMessage)
        {
            short netID = ObjectSerializer.deserializeShort(ref msg);

            if (NetworkEntityManager.Instance.findComponent(netID, out NetworkTransform networkTransform)) //NetworkTransform found
            {
                bool receivedPos = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedPos) //Position data was sent
                {
                    float posX = ObjectSerializer.deserializeFloat(ref msg);
                    float posY = ObjectSerializer.deserializeFloat(ref msg);
                    float posZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Position = new Vector3(posX, posY, posZ);
                }

                bool receivedRot = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedRot) //Rotation data was sent
                {
                    float rotX = ObjectSerializer.deserializeFloat(ref msg);
                    float rotY = ObjectSerializer.deserializeFloat(ref msg);
                    float rotZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Rotation = Quaternion.Euler(new Vector3(rotX, rotY, rotZ));
                }

                bool receivedScale = ObjectSerializer.deserializeByte(ref msg) == 1;
                if (receivedScale) //Scale data was sent
                {
                    float scaleX = ObjectSerializer.deserializeFloat(ref msg);
                    float scaleY = ObjectSerializer.deserializeFloat(ref msg);
                    float scaleZ = ObjectSerializer.deserializeFloat(ref msg);
                    networkTransform.Scale = new Vector3(scaleX, scaleY, scaleZ);
                }

                //Message was sent from a client, now being processed on the server
                if (clientMessage && ServerBehaviour.Instance)
                {
                    //What transform space is this data in? (World / Local)
                    bool  worldSpace = ObjectSerializer.deserializeByte(ref msg) == 1;
                    short clientID   = ObjectSerializer.deserializeShort(ref msg); //Client who sent the message
                    NetworkTransform.TransformSpace transformSpace = worldSpace ? NetworkTransform.TransformSpace.World : NetworkTransform.TransformSpace.Local;
                    byte[] transformMsg = MessageFactory.createTransformMessage(networkTransform.networkIdentity, transformSpace, receivedPos, receivedRot, receivedScale);
                    ServerBehaviour.Instance.sendMessage(transformMsg, clientID); //Forward to all clients but the sender
                }
            }
        }