Guest User

Manager

a guest
Aug 24th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. //Blue 1
  6. //Red 2
  7. public class NetworkManager : MonoBehaviour {
  8.  
  9. private bool waitingLaunch = false;
  10. private bool waitLaunch = false;
  11. public int localTeam;
  12. public int playerCount;
  13. private bool waiting = true;
  14. public Font font;
  15. int TeamBlue;
  16. int TeamRed;
  17. bool RedFull = false;
  18. bool BlueFull = false;
  19.  
  20. private string VERSION = "V1.0";
  21. public string roomName = "MR";
  22. public string playerPrefabName = "Player";
  23. public Transform SpawnBlue;
  24. public Transform SpawnRed;
  25. public GameObject LobbyCam;
  26. public GameObject Terrain;
  27.  
  28. void Update()
  29. {
  30.  
  31. if (TeamRed == 3)
  32. RedFull = true;
  33.  
  34. if (TeamBlue == 3)
  35. BlueFull = true;
  36.  
  37. if(playerCount == 6 && waiting)
  38. {
  39. if (waitLaunch) {
  40. waiting = false;
  41. LobbyCam.SetActive(false);
  42. if (localTeam == 1) {
  43. GameObject myPlayer = PhotonNetwork.Instantiate(playerPrefabName, SpawnBlue.transform.position, SpawnBlue.transform.rotation, 0);
  44. GameObject Cam = PhotonNetwork.Instantiate("Main Camera", SpawnBlue.transform.position, SpawnBlue.transform.rotation, 0);
  45. myPlayer.GetComponent<PlayerAttribute>().Team = "Blue";
  46. myPlayer.GetComponent<PlayerMovement>().enabled = true;
  47. myPlayer.GetComponent<Attack>().enabled = true;
  48. Cam.SetActive(true);
  49. Cam.GetComponent<Follow>().target = myPlayer.transform;
  50. } else
  51. {
  52. GameObject myPlayer = PhotonNetwork.Instantiate(playerPrefabName, SpawnRed.transform.position, SpawnRed.transform.rotation, 0);
  53. GameObject Cam = PhotonNetwork.Instantiate("Main Camera", SpawnRed.transform.position, SpawnRed.transform.rotation, 0);
  54. myPlayer.GetComponent<PlayerAttribute>().Team = "Red";
  55. myPlayer.GetComponent<PlayerMovement>().enabled = true;
  56. myPlayer.GetComponent<Attack>().enabled = true;
  57. Cam.SetActive(true);
  58. Cam.GetComponent<Follow>().target = myPlayer.transform;
  59. //myPlayer.transform.FindChild("HealthPrefab").GetComponent<CameraFacingBillboard>().camera = Cam as Camera;
  60. }
  61. }else
  62. {
  63. StartCoroutine(LaunchDelay());
  64. }
  65. }
  66. }
  67.  
  68. void Start() {
  69.  
  70. Debug.Log("Start");
  71. PhotonNetwork.ConnectUsingSettings(VERSION);
  72. }
  73.  
  74. void OnJoinedLobby() {
  75. Debug.Log("Joined Lobby");
  76. Terrain.SetActive (true);
  77. PhotonNetwork.JoinRandomRoom();
  78. }
  79.  
  80.  
  81.  
  82. void OnPhotonRandomJoinFailed()
  83. {
  84. RoomOptions roomOptions = new RoomOptions() { isVisible = true, maxPlayers = 6 };
  85. PhotonNetwork.CreateRoom(null, roomOptions, TypedLobby.Default);
  86. }
  87.  
  88. void OnJoinedRoom()
  89. {
  90. PhotonView photonView = PhotonView.Get(this);
  91. photonView.RPC("UpdatePlayerCount", PhotonTargets.AllBuffered, true);
  92.  
  93. if (BlueFull)
  94. {
  95. photonView.RPC("UpdateRedTeamCount", PhotonTargets.AllBuffered, true);
  96. localTeam = 2;
  97. }else if (RedFull)
  98. {
  99. photonView.RPC("UpdateBlueTeamCount", PhotonTargets.AllBuffered, true);
  100. localTeam = 1;
  101. }else if (TeamBlue > TeamRed)
  102. {
  103. photonView.RPC("UpdateRedTeamCount", PhotonTargets.AllBuffered, true);
  104. localTeam = 2;
  105. }else if(TeamRed > TeamBlue)
  106. {
  107. photonView.RPC("UpdateBlueTeamCount", PhotonTargets.AllBuffered, true);
  108. localTeam = 1;
  109. }else if(TeamBlue == TeamRed)
  110. {
  111. if(Random.Range(0, 3) == 1)
  112. {
  113. photonView.RPC("UpdateBlueTeamCount", PhotonTargets.AllBuffered, true);
  114. localTeam = 1;
  115. }else{
  116. photonView.RPC("UpdateRedTeamCount", PhotonTargets.AllBuffered, true);
  117. localTeam = 2;
  118. }
  119. }
  120.  
  121.  
  122. }
  123.  
  124. void OnLeftRoom()
  125. {
  126. PhotonView photonView = PhotonView.Get(this);
  127. photonView.RPC("UpdatePlayerCount", PhotonTargets.AllBuffered, false);
  128. if(localTeam == 1)
  129. {
  130. photonView.RPC("UpdateBlueTeamCount", PhotonTargets.AllBuffered, false);
  131. }
  132. else
  133. {
  134. photonView.RPC("UpdateRedTeamCount", PhotonTargets.AllBuffered, false);
  135. }
  136. }
  137.  
  138. IEnumerator LaunchDelay()
  139. {
  140. waitingLaunch = true;
  141. yield return new WaitForSeconds(5);
  142. waitLaunch = true;
  143. waitingLaunch = false;
  144.  
  145. }
  146.  
  147. void OnGUI()
  148. {
  149. GUIStyle MainStyle = new GUIStyle();
  150. MainStyle.font = font;
  151. if (waiting)
  152. {
  153. GUI.Label(new Rect(0, 0, 100, 10), "Players In Room: " + playerCount, MainStyle);
  154. GUI.Label(new Rect(0, 20, 100, 10), "Players In Red: " + TeamRed, MainStyle);
  155. GUI.Label(new Rect(0, 40, 100, 10), "Players In Blue: " + TeamBlue, MainStyle);
  156.  
  157. Rect TabList = new Rect(0, 100, 100, 10);
  158. foreach (PhotonPlayer pl in PhotonNetwork.playerList)
  159. {
  160. GUI.Label(TabList, "" + pl.name, MainStyle);
  161. TabList.y += 20;
  162. }
  163.  
  164. GUI.Label(new Rect(Screen.width / 2, 0, 100, 10), "Hello, " + PhotonNetwork.player.name, MainStyle);
  165. }
  166. }
  167.  
  168. [PunRPC]
  169. void UpdatePlayerCount(bool AddToCount)
  170. {
  171. if (AddToCount)
  172. {
  173. playerCount += 1;
  174. }
  175. else
  176. {
  177. playerCount -= 1;
  178. }
  179. }
  180. [PunRPC]
  181. void UpdateRedTeamCount(bool AddToCount)
  182. {
  183. if (AddToCount)
  184. {
  185. TeamRed += 1;
  186. }
  187. else
  188. {
  189. TeamRed -= 1;
  190. }
  191. }
  192. [PunRPC]
  193. void UpdateBlueTeamCount(bool AddToCount)
  194. {
  195. if (AddToCount)
  196. {
  197. TeamBlue += 1;
  198. }
  199. else
  200. {
  201. TeamBlue -= 1;
  202. }
  203. }
  204. }
Add Comment
Please, Sign In to add comment