Ejemplo n.º 1
0
        private static void processAddJointMessage(byte[] msg, bool clientMessage)
        {
            short jointParentID = ObjectSerializer.deserializeShort(ref msg);

            if (NetworkEntityManager.Instance.findComponent(jointParentID, out NetworkIdentity jointParent))
            {
                short connectedBodyID = ObjectSerializer.deserializeShort(ref msg);

                if (connectedBodyID == -2)                                             //No connected body
                {
                    byte[] addJointMsg    = null;                                      //Message to forward to other clients if this is a client message
                    bool   forwardMessage = clientMessage && ServerBehaviour.Instance; //Should the message be forwarded?

                    byte jointType = ObjectSerializer.deserializeByte(ref msg);
                    switch (jointType)
                    {
                    case 0:
                        jointParent.gameObject.AddComponent <FixedJoint>();

                        if (forwardMessage)
                        {
                            addJointMsg = MessageFactory.createAddJointMessage <FixedJoint>(jointParent, null);
                        }
                        break;

                    case 1:
                        jointParent.gameObject.AddComponent <HingeJoint>();

                        if (forwardMessage)
                        {
                            addJointMsg = MessageFactory.createAddJointMessage <HingeJoint>(jointParent, null);
                        }
                        break;

                    case 2:
                        jointParent.gameObject.AddComponent <SpringJoint>();

                        if (forwardMessage)
                        {
                            addJointMsg = MessageFactory.createAddJointMessage <SpringJoint>(jointParent, null);
                        }
                        break;

                    case 3:
                        jointParent.gameObject.AddComponent <ConfigurableJoint>();

                        if (forwardMessage)
                        {
                            addJointMsg = MessageFactory.createAddJointMessage <ConfigurableJoint>(jointParent, null);
                        }
                        break;

                    case 4:
                        jointParent.gameObject.AddComponent <CharacterJoint>();

                        if (forwardMessage)
                        {
                            addJointMsg = MessageFactory.createAddJointMessage <CharacterJoint>(jointParent, null);
                        }
                        break;
                    }

                    //Message was sent from a client, now being processed on the server
                    if (forwardMessage)
                    {
                        short clientID = ObjectSerializer.deserializeShort(ref msg);       //Client who sent the message
                        ServerBehaviour.Instance.sendMessage(addJointMsg, clientID, true); //Forward to all clients but the sender
                    }
                }
                else //Connected body
                {
                    if (NetworkEntityManager.Instance.findComponent(connectedBodyID, out NetworkIdentity connectedBody))
                    {
                        if (NetworkEntityManager.Instance.findComponent(connectedBodyID, out Rigidbody connectedBodyRb))
                        {
                            byte[] addJointMsg    = null;                                      //Message to forward to other clients if this is a client message
                            bool   forwardMessage = clientMessage && ServerBehaviour.Instance; //Should the message be forwarded?

                            byte jointType = ObjectSerializer.deserializeByte(ref msg);
                            switch (jointType)
                            {
                            case 0:
                                jointParent.gameObject.AddComponent <FixedJoint>().connectedBody = connectedBodyRb;

                                if (forwardMessage)
                                {
                                    addJointMsg = MessageFactory.createAddJointMessage <FixedJoint>(jointParent, connectedBody);
                                }
                                break;

                            case 1:
                                jointParent.gameObject.AddComponent <HingeJoint>().connectedBody = connectedBodyRb;

                                if (forwardMessage)
                                {
                                    addJointMsg = MessageFactory.createAddJointMessage <HingeJoint>(jointParent, connectedBody);
                                }
                                break;

                            case 2:
                                jointParent.gameObject.AddComponent <SpringJoint>().connectedBody = connectedBodyRb;

                                if (forwardMessage)
                                {
                                    addJointMsg = MessageFactory.createAddJointMessage <SpringJoint>(jointParent, connectedBody);
                                }
                                break;

                            case 3:
                                jointParent.gameObject.AddComponent <ConfigurableJoint>().connectedBody = connectedBodyRb;

                                if (forwardMessage)
                                {
                                    addJointMsg = MessageFactory.createAddJointMessage <ConfigurableJoint>(jointParent, connectedBody);
                                }
                                break;

                            case 4:
                                jointParent.gameObject.AddComponent <CharacterJoint>().connectedBody = connectedBodyRb;

                                if (forwardMessage)
                                {
                                    addJointMsg = MessageFactory.createAddJointMessage <CharacterJoint>(jointParent, connectedBody);
                                }
                                break;
                            }

                            //Message was sent from a client, now being processed on the server
                            if (forwardMessage)
                            {
                                short clientID = ObjectSerializer.deserializeShort(ref msg);       //Client who sent the message
                                ServerBehaviour.Instance.sendMessage(addJointMsg, clientID, true); //Forward to all clients but the sender
                            }
                        }
                    }
                }
            }
        }