Advertisement
rdgorodrigo

Untitled

Nov 13th, 2020
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.52 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using TMPro;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class NetworkConejo : Photon.MonoBehaviour
  7. {
  8.     GameObject ambientCam;
  9.     GameObject advanced_cam;
  10.     Conejo controllerScript;
  11.     PlayerHealth healthscript;
  12.     PlayerAttack attackscript;
  13.     Stamina staminascript;
  14.     SphereCollider _trigger;
  15.     private bool appliedInitialUpdate;
  16.     public GameObject HUD;
  17.     public TextMeshProUGUI text;
  18.     public GameObject Stamina;
  19.     public Sprite avatar;
  20.     public Win screen;
  21.     public Transform HatPosition;
  22.  
  23.     public double interpolationBackTime = 0.2;
  24.     public double extrapolationLimit = 0.5;
  25.     public float maxSpeedDeltaSqr = 9f;
  26.  
  27.     public bool syncLocalPosition = true;
  28.     public bool syncLocalRotation = true;
  29.     public bool checkForSpeedHacks = false;
  30.  
  31.     private State[] proxyStates = new State[20];
  32.  
  33.     private int proxyStateCount;
  34.  
  35.     protected struct State
  36.     {
  37.         public double timestamp;
  38.         public Vector3 pos;
  39.         public Vector3 vel;
  40.         public Quaternion rot;
  41.     }
  42.  
  43.     public Vector3 LocalPositionDelta
  44.     {
  45.         get
  46.         {
  47.             return localPositionDelta;
  48.         }
  49.     }
  50.     private Vector3 localPositionDelta;
  51.  
  52.     /// <summary>
  53.     /// The position of this transform in the previous frame
  54.     /// </summary>
  55.     private Vector3 prevLocalPosition;
  56.     GameObject Portals;
  57.  
  58.     void Awake()
  59.     {
  60.         controllerScript = GetComponent<Conejo>();
  61.         healthscript = GetComponent<PlayerHealth>();
  62.         attackscript = GetComponent<PlayerAttack>();
  63.         staminascript = GetComponent<Stamina>();
  64.         _trigger = GetComponent<SphereCollider>();
  65.         respawn.parent = null;
  66.     }
  67.     void Start()
  68.     {
  69.         if (photonView.isMine)
  70.         {
  71.  
  72.             advanced_cam.SetActive(true);
  73.             controllerScript.enabled = true;
  74.             healthscript.enabled = true;
  75.             attackscript.enabled = true;
  76.             staminascript.enabled = true;
  77.             HUD.SetActive(true);
  78.             Stamina.SetActive(true);
  79.             _trigger.enabled = true;
  80.             if (controllerScript.HatPrefab != null)
  81.             {
  82.                 int hatPrefab = controllerScript.HatPrefab.GetComponent<PhotonView>().viewID;
  83.                 int HatPlacement = controllerScript.HatPosition.GetComponent<PhotonView>().viewID;
  84.                 photonView.RPC("SParent", PhotonTargets.AllBufferedViaServer, hatPrefab, HatPlacement);
  85.             }
  86.             PauseManager.instance.FindHud();
  87.             if (PhotonNetwork.isMasterClient)
  88.             {
  89.                 photonView.RPC("StartPortal", PhotonTargets.MasterClient);
  90.             }
  91.         }
  92.         else
  93.         {
  94.             controllerScript.enabled = false;
  95.             advanced_cam.SetActive(false);
  96.             healthscript.enabled = false;
  97.             attackscript.enabled = false;
  98.             staminascript.enabled = false;
  99.             HUD.SetActive(false);
  100.             Stamina.SetActive(false);
  101.             _trigger.enabled = false;
  102.             gameObject.layer = LayerMask.NameToLayer("IA");
  103.         }
  104.  
  105.         text.text = photonView.owner.NickName;
  106.         controllerScript.SetIsRemotePlayer(!photonView.isMine);
  107.         gameObject.name = gameObject.name + photonView.viewID;
  108.     }
  109.  
  110.     [PunRPC]
  111.     void StartPortal()
  112.     {
  113.         PhotonNetwork.Instantiate("ForestStart", new Vector3(2,5,7), Quaternion.identity, 0);
  114.     }
  115.  
  116.     [PunRPC]
  117.     void SParent(int hatPrefab, int HatPlacement)
  118.     {
  119.         PhotonView.Find(hatPrefab).transform.SetParent(PhotonView.Find(HatPlacement).transform);
  120.         PhotonView.Find(hatPrefab).transform.localPosition = Vector3.zero;
  121.         PhotonView.Find(hatPrefab).transform.localScale = Vector3.one;
  122.     }
  123.  
  124.     [PunRPC]
  125.     public void LoadMyScene(byte[] bytes)
  126.     {
  127.         string sceneName = System.Text.Encoding.UTF8.GetString(bytes);
  128.         PhotonNetwork.LoadLevel(sceneName);
  129.         PhotonNetwork.automaticallySyncScene = true;
  130.     }
  131.  
  132.     [PunRPC]
  133.     public void LoseOthers()
  134.     {
  135.         screen.LoseOnline();
  136.     }
  137.  
  138.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  139.     {
  140.         if (stream.isWriting)
  141.         {
  142.             // We own this player
  143.             stream.SendNext(transform.localPosition);
  144.             stream.SendNext(localPositionDelta);
  145.             stream.SendNext(transform.localRotation);
  146.         }
  147.         else
  148.         {
  149.             // Network player, receive data
  150.             Vector3 pos = (Vector3)stream.ReceiveNext();
  151.             Vector3 velocity = (Vector3)stream.ReceiveNext();
  152.             Quaternion rot = (Quaternion)stream.ReceiveNext();
  153.  
  154.             if (!syncLocalPosition)
  155.             {
  156.                 pos = transform.localPosition;
  157.                 velocity = localPositionDelta;
  158.             }
  159.  
  160.             if (!syncLocalRotation)
  161.             {
  162.                 rot = transform.localRotation;
  163.             }
  164.  
  165.             // Check for speed hacks
  166.             if (checkForSpeedHacks && proxyStates.Length > 0)
  167.             {
  168.                 Vector3 delta = pos - proxyStates[0].pos;
  169.                 if (delta.sqrMagnitude > maxSpeedDeltaSqr)
  170.                 {
  171. #if UNITY_EDITOR
  172.                     Debug.LogWarning("Speed hack detected. Throttling velocity of " + delta.magnitude);
  173.                     pos = proxyStates[0].pos + delta.normalized * Mathf.Sqrt(maxSpeedDeltaSqr);
  174. #endif
  175.                 }
  176.             }
  177.  
  178.             for (int i = proxyStates.Length - 1; i >= 1; i--)
  179.             {
  180.                 proxyStates[i] = proxyStates[i - 1];
  181.             }
  182.  
  183.             State state;
  184.             state.timestamp = info.timestamp;
  185.  
  186.             state.pos = pos;
  187.             state.vel = velocity;
  188.             state.rot = rot;
  189.             proxyStates[0] = state;
  190.             proxyStateCount = Mathf.Min(proxyStateCount + 1, proxyStates.Length);
  191.  
  192.             if (proxyStates[0].timestamp < proxyStates[1].timestamp)
  193.             {
  194. #if UNITY_EDITOR
  195.                 Debug.LogWarning("Timestamp inconsistent: " + proxyStates[0].timestamp + " should be greater than " + proxyStates[1].timestamp);
  196. #endif
  197.             }
  198.         }
  199.     }
  200.  
  201.     void Update()
  202.     {
  203.         if (!photonView.isMine)
  204.         {
  205.  
  206.             double interpolationTime = PhotonNetwork.time - interpolationBackTime;
  207.             if (proxyStates[0].timestamp > interpolationTime)
  208.             {
  209.                 for (int i = 0; i < proxyStateCount; i++)
  210.                 {
  211.                     if (proxyStates[i].timestamp <= interpolationTime || i == proxyStateCount - 1)
  212.                     {
  213.                         State rhs = proxyStates[Mathf.Max(i - 1, 0)];
  214.                         State lhs = proxyStates[i];
  215.  
  216.                         double length = rhs.timestamp - lhs.timestamp;
  217.                         float t = 0.0F;
  218.                         if (length > 0.0001)
  219.                             t = (float)((interpolationTime - lhs.timestamp) / length);
  220.  
  221.                         if (syncLocalPosition) transform.localPosition = Vector3.Lerp(lhs.pos, rhs.pos, t);
  222.                         if (syncLocalRotation) transform.localRotation = Quaternion.Slerp(lhs.rot, rhs.rot, t);
  223.                         break;
  224.                     }
  225.                 }
  226.             }
  227.             // extrapolation
  228.             else
  229.             {
  230.                 State latest = proxyStates[0];
  231.  
  232.                 float extrapolationLength = (float)(interpolationTime - latest.timestamp);
  233.                 if (extrapolationLength < extrapolationLimit)
  234.                 {
  235.                     if (syncLocalPosition) transform.localPosition = latest.pos + latest.vel * extrapolationLength;
  236.                     if (syncLocalRotation) transform.localRotation = latest.rot;
  237.                 }
  238.             }
  239.         }
  240.         else
  241.         {
  242.             // position delta
  243.         }
  244.  
  245.         localPositionDelta = transform.localPosition - prevLocalPosition;
  246.         prevLocalPosition = transform.localPosition;
  247.     }
  248.  
  249.     void OnLeftRoom()
  250.     {
  251.         PhotonNetwork.Destroy(text.transform.root.gameObject);
  252.     }
  253.  
  254.     void OnDisconnected()
  255.     {
  256.         PhotonNetwork.Destroy(text.transform.root.gameObject);
  257.     }
  258.  
  259.     [PunRPC]
  260.     public void SendLeftRoom()
  261.     {
  262.         GameManagerRF.instance.SendLeftRoom();
  263.     }
  264.  
  265.     [PunRPC]
  266.     void WinCondition()
  267.     {
  268.         Debug.Log("Winner: " + PhotonNetwork.playerName);
  269.     }
  270.  
  271.     [PunRPC]
  272.     void LoseCondition()
  273.     {
  274.         Debug.Log("Loser: " + PhotonNetwork.playerName);
  275.     }
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement