Exemplo n.º 1
0
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        // I have control over the pieces, so I can move them and then send pos + rot over network
        if (stream.IsWriting)
        {
            stream.SendNext(MyFlippedCoordinates.PositionRelativeToBoard(Rb.position, Board));
            stream.SendNext(Rb.isKinematic);
            stream.SendNext(Rb.rotation);
            stream.SendNext(Rb.velocity);
        }

        // No control over the object, so I update this class with the values read from the network
        // if (stream.IsReading)
        else
        {
            NetworkPositionRb  = (Vector3)stream.ReceiveNext();
            NetworkIsKinematic = (bool)stream.ReceiveNext();
            NetworkRotation    = (Quaternion)stream.ReceiveNext();

            float lag = Mathf.Abs((float)(PhotonNetwork.Time - info.SentServerTime));
            Rb.velocity        = (Vector3)stream.ReceiveNext();
            NetworkPositionRb += Rb.velocity * lag;
            Distance           = Vector3.Distance(Rb.position, NetworkPositionRb);
        }
    }
Exemplo n.º 2
0
    void SpawnPlayer(int i)
    {
        GameObject piece = IsFirstPlayer
                        ? Instantiate(YellowPlayer, Positions[i].transform.position, PieceRotation)
                        : Instantiate(RedPlayer, Positions[i].transform.position, PieceRotation);

        PhotonView view = piece.GetComponent <PhotonView>();

        if (PhotonNetwork.AllocateViewID(view))
        {
            object[] data = new object[]
            {
                MyFlippedCoordinates.PositionRelativeToBoard(piece.transform.position, Board),
                piece.transform.rotation,
                view.ViewID,
                IsFirstPlayer
            };

            RaiseEventOptions raiseEventOptions = new RaiseEventOptions
            {
                Receivers     = ReceiverGroup.Others,
                CachingOption = EventCaching.AddToRoomCache,
            };

            SendOptions sendOptions = new SendOptions
            {
                Reliability = true
            };

            PhotonNetwork.RaiseEvent((byte)RaiseEventCodes.PlayerSpawnEventCode, data, raiseEventOptions, sendOptions);
        }
    }
Exemplo n.º 3
0
 void FixedUpdate()
 {
     // I have no control over these pieces so retrieve network position and rotation
     // Need to add offset for other side of the board
     if (!PhotonView.IsMine)
     {
         transform.position = Vector3.MoveTowards(
             Rb.position,
             MyFlippedCoordinates.FlipPerspectiveOfBoardPiece(NetworkPositionRb, Board),
             Distance * (1.0f / PhotonNetwork.SerializationRate));
         Rb.isKinematic = NetworkIsKinematic;
         Rb.rotation    = NetworkRotation.normalized;
     }
 }
Exemplo n.º 4
0
    void OnEvent(EventData photonEvent)
    {
        if (photonEvent.Code == (byte)RaiseEventCodes.PlayerSpawnEventCode)
        {
            object[] data = (object[])photonEvent.CustomData;

            Vector3    receivedPosition     = (Vector3)data[0];
            Quaternion receivedRotation     = (Quaternion)data[1];
            int        receivedViewID       = (int)data[2];
            bool       receievedFirstPlayer = (bool)data[3];

            GameObject player = receievedFirstPlayer
                                ? Instantiate(YellowPlayer,
                                              MyFlippedCoordinates.FlipPerspectiveOfBoardPiece(receivedPosition, Board), receivedRotation)
                                : Instantiate(RedPlayer,
                                              MyFlippedCoordinates.FlipPerspectiveOfBoardPiece(receivedPosition, Board), receivedRotation);

            PhotonView view = player.GetComponent <PhotonView>();
            view.ViewID = receivedViewID;
        }
    }