Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using TMPro;
- using UnityEngine.SceneManagement;
- public class NetworkConejo : Photon.MonoBehaviour
- {
- GameObject ambientCam;
- GameObject advanced_cam;
- Conejo controllerScript;
- PlayerHealth healthscript;
- PlayerAttack attackscript;
- Stamina staminascript;
- SphereCollider _trigger;
- private bool appliedInitialUpdate;
- public GameObject HUD;
- public TextMeshProUGUI text;
- public GameObject Stamina;
- public Sprite avatar;
- public Win screen;
- public Transform HatPosition;
- public double interpolationBackTime = 0.2;
- public double extrapolationLimit = 0.5;
- public float maxSpeedDeltaSqr = 9f;
- public bool syncLocalPosition = true;
- public bool syncLocalRotation = true;
- public bool checkForSpeedHacks = false;
- private State[] proxyStates = new State[20];
- private int proxyStateCount;
- protected struct State
- {
- public double timestamp;
- public Vector3 pos;
- public Vector3 vel;
- public Quaternion rot;
- }
- public Vector3 LocalPositionDelta
- {
- get
- {
- return localPositionDelta;
- }
- }
- private Vector3 localPositionDelta;
- /// <summary>
- /// The position of this transform in the previous frame
- /// </summary>
- private Vector3 prevLocalPosition;
- GameObject Portals;
- void Awake()
- {
- controllerScript = GetComponent<Conejo>();
- healthscript = GetComponent<PlayerHealth>();
- attackscript = GetComponent<PlayerAttack>();
- staminascript = GetComponent<Stamina>();
- _trigger = GetComponent<SphereCollider>();
- respawn.parent = null;
- }
- void Start()
- {
- if (photonView.isMine)
- {
- advanced_cam.SetActive(true);
- controllerScript.enabled = true;
- healthscript.enabled = true;
- attackscript.enabled = true;
- staminascript.enabled = true;
- HUD.SetActive(true);
- Stamina.SetActive(true);
- _trigger.enabled = true;
- if (controllerScript.HatPrefab != null)
- {
- int hatPrefab = controllerScript.HatPrefab.GetComponent<PhotonView>().viewID;
- int HatPlacement = controllerScript.HatPosition.GetComponent<PhotonView>().viewID;
- photonView.RPC("SParent", PhotonTargets.AllBufferedViaServer, hatPrefab, HatPlacement);
- }
- PauseManager.instance.FindHud();
- if (PhotonNetwork.isMasterClient)
- {
- photonView.RPC("StartPortal", PhotonTargets.MasterClient);
- }
- }
- else
- {
- controllerScript.enabled = false;
- advanced_cam.SetActive(false);
- healthscript.enabled = false;
- attackscript.enabled = false;
- staminascript.enabled = false;
- HUD.SetActive(false);
- Stamina.SetActive(false);
- _trigger.enabled = false;
- gameObject.layer = LayerMask.NameToLayer("IA");
- }
- text.text = photonView.owner.NickName;
- controllerScript.SetIsRemotePlayer(!photonView.isMine);
- gameObject.name = gameObject.name + photonView.viewID;
- }
- [PunRPC]
- void StartPortal()
- {
- PhotonNetwork.Instantiate("ForestStart", new Vector3(2,5,7), Quaternion.identity, 0);
- }
- [PunRPC]
- void SParent(int hatPrefab, int HatPlacement)
- {
- PhotonView.Find(hatPrefab).transform.SetParent(PhotonView.Find(HatPlacement).transform);
- PhotonView.Find(hatPrefab).transform.localPosition = Vector3.zero;
- PhotonView.Find(hatPrefab).transform.localScale = Vector3.one;
- }
- [PunRPC]
- public void LoadMyScene(byte[] bytes)
- {
- string sceneName = System.Text.Encoding.UTF8.GetString(bytes);
- PhotonNetwork.LoadLevel(sceneName);
- PhotonNetwork.automaticallySyncScene = true;
- }
- [PunRPC]
- public void LoseOthers()
- {
- screen.LoseOnline();
- }
- void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
- {
- if (stream.isWriting)
- {
- // We own this player
- stream.SendNext(transform.localPosition);
- stream.SendNext(localPositionDelta);
- stream.SendNext(transform.localRotation);
- }
- else
- {
- // Network player, receive data
- Vector3 pos = (Vector3)stream.ReceiveNext();
- Vector3 velocity = (Vector3)stream.ReceiveNext();
- Quaternion rot = (Quaternion)stream.ReceiveNext();
- if (!syncLocalPosition)
- {
- pos = transform.localPosition;
- velocity = localPositionDelta;
- }
- if (!syncLocalRotation)
- {
- rot = transform.localRotation;
- }
- // Check for speed hacks
- if (checkForSpeedHacks && proxyStates.Length > 0)
- {
- Vector3 delta = pos - proxyStates[0].pos;
- if (delta.sqrMagnitude > maxSpeedDeltaSqr)
- {
- #if UNITY_EDITOR
- Debug.LogWarning("Speed hack detected. Throttling velocity of " + delta.magnitude);
- pos = proxyStates[0].pos + delta.normalized * Mathf.Sqrt(maxSpeedDeltaSqr);
- #endif
- }
- }
- for (int i = proxyStates.Length - 1; i >= 1; i--)
- {
- proxyStates[i] = proxyStates[i - 1];
- }
- State state;
- state.timestamp = info.timestamp;
- state.pos = pos;
- state.vel = velocity;
- state.rot = rot;
- proxyStates[0] = state;
- proxyStateCount = Mathf.Min(proxyStateCount + 1, proxyStates.Length);
- if (proxyStates[0].timestamp < proxyStates[1].timestamp)
- {
- #if UNITY_EDITOR
- Debug.LogWarning("Timestamp inconsistent: " + proxyStates[0].timestamp + " should be greater than " + proxyStates[1].timestamp);
- #endif
- }
- }
- }
- void Update()
- {
- if (!photonView.isMine)
- {
- double interpolationTime = PhotonNetwork.time - interpolationBackTime;
- if (proxyStates[0].timestamp > interpolationTime)
- {
- for (int i = 0; i < proxyStateCount; i++)
- {
- if (proxyStates[i].timestamp <= interpolationTime || i == proxyStateCount - 1)
- {
- State rhs = proxyStates[Mathf.Max(i - 1, 0)];
- State lhs = proxyStates[i];
- double length = rhs.timestamp - lhs.timestamp;
- float t = 0.0F;
- if (length > 0.0001)
- t = (float)((interpolationTime - lhs.timestamp) / length);
- if (syncLocalPosition) transform.localPosition = Vector3.Lerp(lhs.pos, rhs.pos, t);
- if (syncLocalRotation) transform.localRotation = Quaternion.Slerp(lhs.rot, rhs.rot, t);
- break;
- }
- }
- }
- // extrapolation
- else
- {
- State latest = proxyStates[0];
- float extrapolationLength = (float)(interpolationTime - latest.timestamp);
- if (extrapolationLength < extrapolationLimit)
- {
- if (syncLocalPosition) transform.localPosition = latest.pos + latest.vel * extrapolationLength;
- if (syncLocalRotation) transform.localRotation = latest.rot;
- }
- }
- }
- else
- {
- // position delta
- }
- localPositionDelta = transform.localPosition - prevLocalPosition;
- prevLocalPosition = transform.localPosition;
- }
- void OnLeftRoom()
- {
- PhotonNetwork.Destroy(text.transform.root.gameObject);
- }
- void OnDisconnected()
- {
- PhotonNetwork.Destroy(text.transform.root.gameObject);
- }
- [PunRPC]
- public void SendLeftRoom()
- {
- GameManagerRF.instance.SendLeftRoom();
- }
- [PunRPC]
- void WinCondition()
- {
- Debug.Log("Winner: " + PhotonNetwork.playerName);
- }
- [PunRPC]
- void LoseCondition()
- {
- Debug.Log("Loser: " + PhotonNetwork.playerName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement