Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.78 KB | None | 0 0
  1. public class PhotonTransformView : MonoBehaviour, IPunObservable
  2. {
  3.     //Since this component is very complex, we seperated it into multiple objects.
  4.     //The PositionModel, RotationModel and ScaleMode store the data you are able to
  5.     //configure in the inspector while the control objects below are actually moving
  6.     //the object and calculating all the inter- and extrapolation
  7.  
  8.     [SerializeField]
  9.     PhotonTransformViewPositionModel m_PositionModel = new PhotonTransformViewPositionModel();
  10.  
  11.     [SerializeField]
  12.     PhotonTransformViewRotationModel m_RotationModel = new PhotonTransformViewRotationModel();
  13.  
  14.     [SerializeField]
  15.     PhotonTransformViewScaleModel m_ScaleModel = new PhotonTransformViewScaleModel();
  16.  
  17.     PhotonTransformViewPositionControl m_PositionControl;
  18.     PhotonTransformViewRotationControl m_RotationControl;
  19.     PhotonTransformViewScaleControl m_ScaleControl;
  20.  
  21.     PhotonView m_PhotonView;
  22.  
  23.     bool m_ReceivedNetworkUpdate = false;
  24.  
  25.     /// <summary>
  26.     /// Flag to skip initial data when Object is instantiated and rely on the first deserialized data instead.
  27.     /// </summary>
  28.     bool m_firstTake = false;
  29.  
  30.     void Awake()
  31.     {
  32.         this.m_PhotonView = GetComponent<PhotonView>();
  33.  
  34.         this.m_PositionControl = new PhotonTransformViewPositionControl(this.m_PositionModel);
  35.         this.m_RotationControl = new PhotonTransformViewRotationControl(this.m_RotationModel);
  36.         this.m_ScaleControl = new PhotonTransformViewScaleControl(this.m_ScaleModel);
  37.     }
  38.  
  39.     void OnEnable()
  40.     {
  41.         m_firstTake = true;
  42.     }
  43.  
  44.     void Update()
  45.     {
  46.         if (this.m_PhotonView == null || this.m_PhotonView.isMine == true || PhotonNetwork.connected == false)
  47.         {
  48.             return;
  49.         }
  50.  
  51.         this.UpdatePosition();
  52.         this.UpdateRotation();
  53.         this.UpdateScale();
  54.     }
  55.  
  56.     void UpdatePosition()
  57.     {
  58.         if (this.m_PositionModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
  59.         {
  60.             return;
  61.         }
  62.  
  63.         transform.localPosition = this.m_PositionControl.UpdatePosition(transform.localPosition);
  64.     }
  65.  
  66.     void UpdateRotation()
  67.     {
  68.         if (this.m_RotationModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
  69.         {
  70.             return;
  71.         }
  72.  
  73.         transform.localRotation = this.m_RotationControl.GetRotation(transform.localRotation);
  74.     }
  75.  
  76.     void UpdateScale()
  77.     {
  78.         if (this.m_ScaleModel.SynchronizeEnabled == false || this.m_ReceivedNetworkUpdate == false)
  79.         {
  80.             return;
  81.         }
  82.  
  83.         transform.localScale = this.m_ScaleControl.GetScale(transform.localScale);
  84.     }
  85.  
  86.     /// <summary>
  87.     /// These values are synchronized to the remote objects if the interpolation mode
  88.     /// or the extrapolation mode SynchronizeValues is used. Your movement script should pass on
  89.     /// the current speed (in units/second) and turning speed (in angles/second) so the remote
  90.     /// object can use them to predict the objects movement.
  91.     /// </summary>
  92.     /// <param name="speed">The current movement vector of the object in units/second.</param>
  93.     /// <param name="turnSpeed">The current turn speed of the object in angles/second.</param>
  94.     public void SetSynchronizedValues(Vector3 speed, float turnSpeed)
  95.     {
  96.         this.m_PositionControl.SetSynchronizedValues(speed, turnSpeed);
  97.     }
  98.  
  99.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  100.     {
  101.         this.m_PositionControl.OnPhotonSerializeView(transform.localPosition, stream, info);
  102.         this.m_RotationControl.OnPhotonSerializeView(transform.localRotation, stream, info);
  103.         this.m_ScaleControl.OnPhotonSerializeView(transform.localScale, stream, info);
  104.  
  105.         if (this.m_PhotonView.isMine == false && this.m_PositionModel.DrawErrorGizmo == true)
  106.         {
  107.             this.DoDrawEstimatedPositionError();
  108.         }
  109.  
  110.         if (stream.isReading == true)
  111.         {
  112.             this.m_ReceivedNetworkUpdate = true;
  113.  
  114.             // force latest data to avoid initial drifts when player is instantiated.
  115.             if (m_firstTake)
  116.             {
  117.                 m_firstTake = false;
  118.  
  119.                 if (this.m_PositionModel.SynchronizeEnabled)
  120.                 {
  121.                     this.transform.localPosition = this.m_PositionControl.GetNetworkPosition();
  122.                 }
  123.  
  124.                 if (this.m_RotationModel.SynchronizeEnabled)
  125.                 {
  126.                     this.transform.localRotation = this.m_RotationControl.GetNetworkRotation();
  127.                 }
  128.  
  129.                 if (this.m_ScaleModel.SynchronizeEnabled)
  130.                 {
  131.                     this.transform.localScale = this.m_ScaleControl.GetNetworkScale();
  132.                 }
  133.  
  134.             }
  135.  
  136.         }
  137.     }
  138.  
  139.     //void OnDrawGizmos()
  140.     //{
  141.     //    if( Application.isPlaying == false || m_PhotonView == null || m_PhotonView.isMine == true || PhotonNetwork.connected == false )
  142.     //    {
  143.     //        return;
  144.     //    }
  145.  
  146.     //    DoDrawNetworkPositionGizmo();
  147.     //    DoDrawExtrapolatedPositionGizmo();
  148.     //}
  149.  
  150.     void DoDrawEstimatedPositionError()
  151.     {
  152.         Vector3 targetPosition = this.m_PositionControl.GetNetworkPosition();
  153.  
  154.         // we are synchronizing the localPosition, so we need to add the parent position for a proper positioning.
  155.         if (transform.parent != null)
  156.         {
  157.             targetPosition = transform.parent.position + targetPosition ;
  158.         }
  159.  
  160.         Debug.DrawLine(targetPosition, transform.position, Color.red, 2f);
  161.         Debug.DrawLine(transform.position, transform.position + Vector3.up, Color.green, 2f);
  162.         Debug.DrawLine(targetPosition , targetPosition + Vector3.up, Color.red, 2f);
  163.     }
  164.  
  165.     //void DoDrawNetworkPositionGizmo()
  166.     //{
  167.     //    if( m_PositionModel.DrawNetworkGizmo == false || m_PositionControl == null )
  168.     //    {
  169.     //        return;
  170.     //    }
  171.  
  172.     //    ExitGames.Client.GUI.GizmoTypeDrawer.Draw( m_PositionControl.GetNetworkPosition(),
  173.     //                                               m_PositionModel.NetworkGizmoType,
  174.     //                                               m_PositionModel.NetworkGizmoColor,
  175.     //                                               m_PositionModel.NetworkGizmoSize );
  176.     //}
  177.  
  178.     //void DoDrawExtrapolatedPositionGizmo()
  179.     //{
  180.     //    if( m_PositionModel.DrawExtrapolatedGizmo == false ||
  181.     //        m_PositionModel.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.Disabled ||
  182.     //        m_PositionControl == null )
  183.     //    {
  184.     //        return;
  185.     //    }
  186.  
  187.     //    ExitGames.Client.GUI.GizmoTypeDrawer.Draw( m_PositionControl.GetNetworkPosition() + m_PositionControl.GetExtrapolatedPositionOffset(),
  188.     //                                               m_PositionModel.ExtrapolatedGizmoType,
  189.     //                                               m_PositionModel.ExtrapolatedGizmoColor,
  190.     //                                               m_PositionModel.ExtrapolatedGizmoSize );
  191.     //}
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement