Advertisement
Guest User

Untitled

a guest
Mar 19th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using Photon.Pun;
  4. using System.Collections.Generic;
  5. using TMPro;
  6. using PlayFab;
  7. using PlayFab.ClientModels;
  8.  
  9. public class LevelManager : MonoBehaviourPunCallbacks
  10. {
  11.     public GameObject player;
  12.     public GameObject mainCamera;
  13.     public GameObject escapeMenu;
  14.     public GameObject errorPanel;
  15.     public List<GameObject> spawnpointList = new List<GameObject>();
  16.     public TMP_Text timerText;
  17.     [SerializeField] private float timer;
  18.     [HideInInspector] public bool escapeMenuOpen;
  19.     string playerName;
  20.     private SceneToLoad sceneToLoad;
  21.     private int randomNumber;
  22.  
  23.     private void Awake()
  24.     {
  25.         GetAccountInfo();
  26.     }
  27.  
  28.     void Start()
  29.     {
  30.         sceneToLoad = FindObjectOfType<SceneToLoad>();
  31.         escapeMenu.SetActive(false);
  32.         Cursor.lockState = CursorLockMode.Locked;
  33.         Spawn();
  34.     }
  35.  
  36.     void Update()
  37.     {
  38.         timer -= Time.deltaTime;
  39.         int minutes = Mathf.FloorToInt(timer / 60F);
  40.         int seconds = Mathf.FloorToInt(timer - minutes * 60);
  41.         timerText.text = string.Format("{0:0}:{1:00}", minutes, seconds);
  42.         if (Input.GetKeyDown(KeyCode.Escape))
  43.         {
  44.             EscapeMenuOpen();
  45.         }
  46.     }
  47.  
  48.     void GetAccountInfo()
  49.     {
  50.         var request = new GetAccountInfoRequest();
  51.         PlayFabClientAPI.GetAccountInfo(request, OnAccountInfoSuccess, OnError);
  52.     }
  53.  
  54.     private void OnAccountInfoSuccess(GetAccountInfoResult result)
  55.     {
  56.         playerName = result.AccountInfo.TitleInfo.DisplayName;
  57.     }
  58.  
  59.     private void OnError(PlayFabError error)
  60.     {
  61.         errorPanel.SetActive(true);
  62.     }
  63.  
  64.     void RandomNumber()
  65.     {
  66.         randomNumber = Random.Range(0, spawnpointList.Count);
  67.         Spawn();
  68.     }
  69.  
  70.     public void Spawn()
  71.     {      
  72.         bool dontSpawn = spawnpointList[randomNumber].GetComponent<ColliderOccupied>().occupied;
  73.         if(!dontSpawn)
  74.         {
  75.             Vector3 randomspawnPoint = spawnpointList[randomNumber].GetComponent<Transform>().position;
  76.             player = PhotonNetwork.Instantiate("Player", randomspawnPoint, Quaternion.identity, 0);
  77.             mainCamera.GetComponent<CameraFollow>().target = player.transform;
  78.             PhotonNetwork.NickName = playerName;
  79.         } else
  80.         {
  81.             RandomNumber();
  82.         }              
  83.     }
  84.  
  85.     public void Logout()
  86.     {
  87.         PlayFabClientAPI.ForgetAllCredentials();
  88.         PhotonNetwork.Disconnect();
  89.         sceneToLoad.sceneToLoad = "Login";
  90.         SceneManager.LoadScene("MenuLoading");
  91.     }
  92.  
  93.     void EscapeMenuOpen()
  94.     {
  95.         if (escapeMenu.activeSelf)
  96.         {
  97.             escapeMenuOpen = false;
  98.             escapeMenu.SetActive(false);
  99.             player.GetComponent<PlayerMovement>().enabled = true;
  100.             player.GetComponent<Shooting>().enabled = true;
  101.             Cursor.lockState = CursorLockMode.Locked;
  102.         }
  103.         else
  104.         {
  105.             escapeMenuOpen = true;
  106.             escapeMenu.SetActive(true);
  107.             player.GetComponent<PlayerMovement>().enabled = false;
  108.             player.GetComponent<Shooting>().enabled = false;
  109.             Cursor.lockState = CursorLockMode.None;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement