Example #1
0
    //Called when the object is dragged by the mouse
    void OnMouseDrag()
    {
        //Check we have a client to send from
        if (client == null)
        {
            Debug.LogError("No client assigned to DragObject!");
            return;
        }

        //Firstly we need to work out where the object should be, we can ignore the z-coord returned
        Vector3 newPos = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(10);

        //We want to send the new position of the object to the other clients so we write the position
        //into a DarkRiftWriter as x, y and z components
        DarkRiftWriter writer = new DarkRiftWriter();

        writer.Write(newPos.x);
        writer.Write(newPos.y);

        //Then we'll create a new TagSubject message and put the DarkRiftWriter into it.
        //The tag and subject indicate what the message is about so we'll put a tag of '0' to indicate a
        //movement and set the subject to the ID of this object so that we can easily identify it later.
        Message message = new TagSubjectMessage(dragID, 0, writer);

        //We can then send the message
        client.SendMessage(message, SendMode.Unreliable);    //TODO 1 will this be a problem without others as an intention?

        //Last but not least we'll actually move the object on our screen so set the target position to
        //the new position
        targetPosition = new Vector3(newPos.x, newPos.y, 0);
    }
Example #2
0
    void Client_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        TagSubjectMessage message = e.Message as TagSubjectMessage;

        //Check the message is a TagSubject message and has our ID as the tag
        if (message != null && message.Tag == dragID)
        {
            //Get the reader from the message so we can read the data
            DarkRiftReader reader = message.GetReader();

            //And update our position!
            targetPosition = new Vector3(reader.ReadSingle(), reader.ReadSingle(), 0);
        }
    }
Example #3
0
    internal void DestroyBlock(Vector3 position)
    {
        if (client == null)
        {
            Debug.LogError("No client assigned to BlockWorld component!");
            return;
        }

        //Don't worry about snapping, we'll do that on the server
        DarkRiftWriter writer = new DarkRiftWriter();

        writer.Write(position.x);
        writer.Write(position.y);
        writer.Write(position.z);

        TagSubjectMessage message = new TagSubjectMessage(WORLD_TAG, DESTROY_BLOCK_SUBJECT, writer);

        client.SendMessage(message, SendMode.Reliable);
    }
    void Client_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        TagSubjectMessage message = e.Message as TagSubjectMessage;

        //Check the tag
        if (message != null && message.Tag == MOVEMENT_TAG)
        {
            DarkRiftReader reader = message.GetReader();

            //Read message
            Vector3 newPosition = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
            Vector3 newRotation = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
            uint    id          = reader.ReadUInt32();

            //Update characters to move to new positions
            characters[id].NewPosition = newPosition;
            characters[id].NewRotation = newRotation;
        }
    }
Example #5
0
    void Client_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        TagSubjectMessage message = e.Message as TagSubjectMessage;
        DarkRiftReader    reader  = message.GetReader();

        //Check the tag
        if (message != null && message.Tag == WORLD_TAG)
        {
            Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());

            switch (message.Subject)
            {
            case PLACE_BLOCK_SUBJECT:
                GameObject o = Instantiate(
                    blockPrefab,
                    position,
                    Quaternion.identity
                    ) as GameObject;

                o.transform.SetParent(transform);

                blocks.Add(o);

                break;

            case DESTROY_BLOCK_SUBJECT:
                GameObject block = blocks.SingleOrDefault(b => b != null && b.transform.position == position);

                if (block == null)
                {
                    return;
                }

                Destroy(block);

                blocks.Remove(block);

                break;
            }
        }
    }
    void Client_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        TagSubjectMessage message = e.Message as TagSubjectMessage;

        //Check the tag
        if (message != null && message.Tag == SPAWN_TAG)
        {
            DarkRiftReader reader = message.GetReader();

            switch (message.Subject)
            {
            case SPAWN_SUBJECT:
                SpawnPlayer(reader);
                break;

            case DESPAWN_SUBJECT:
                DespawnPlayer(reader);
                break;
            }
        }
    }
Example #7
0
    void SendTransform()
    {
        //Serialize
        DarkRiftWriter writer = new DarkRiftWriter();

        writer.Write(transform.position.x);
        writer.Write(transform.position.y);
        writer.Write(transform.position.z);
        writer.Write(transform.eulerAngles.x);
        writer.Write(transform.eulerAngles.y);
        writer.Write(transform.eulerAngles.z);

        TagSubjectMessage message = new TagSubjectMessage(MOVEMENT_TAG, 0, writer);

        //Send
        client.SendMessage(message, SendMode.Unreliable);

        //Store last values sent
        lastPosition = transform.position;
        lastRotation = transform.eulerAngles;
    }