Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- //Blue 1
- //Red 2
- public class NetworkManager : MonoBehaviour {
- private bool waitingLaunch = false;
- private bool waitLaunch = false;
- public int localTeam;
- public int playerCount;
- private bool waiting = true;
- public Font font;
- int TeamBlue;
- int TeamRed;
- bool RedFull = false;
- bool BlueFull = false;
- private string VERSION = "V1.0";
- public string roomName = "MR";
- public string playerPrefabName = "Player";
- public Transform SpawnBlue;
- public Transform SpawnRed;
- public GameObject LobbyCam;
- public GameObject Terrain;
- void Update()
- {
- if (TeamRed == 3)
- RedFull = true;
- if (TeamBlue == 3)
- BlueFull = true;
- if(playerCount == 6 && waiting)
- {
- if (waitLaunch) {
- waiting = false;
- LobbyCam.SetActive(false);
- if (localTeam == 1) {
- GameObject myPlayer = PhotonNetwork.Instantiate(playerPrefabName, SpawnBlue.transform.position, SpawnBlue.transform.rotation, 0);
- GameObject Cam = PhotonNetwork.Instantiate("Main Camera", SpawnBlue.transform.position, SpawnBlue.transform.rotation, 0);
- myPlayer.GetComponent<PlayerAttribute>().Team = "Blue";
- myPlayer.GetComponent<PlayerMovement>().enabled = true;
- myPlayer.GetComponent<Attack>().enabled = true;
- Cam.SetActive(true);
- Cam.GetComponent<Follow>().target = myPlayer.transform;
- } else
- {
- GameObject myPlayer = PhotonNetwork.Instantiate(playerPrefabName, SpawnRed.transform.position, SpawnRed.transform.rotation, 0);
- GameObject Cam = PhotonNetwork.Instantiate("Main Camera", SpawnRed.transform.position, SpawnRed.transform.rotation, 0);
- myPlayer.GetComponent<PlayerAttribute>().Team = "Red";
- myPlayer.GetComponent<PlayerMovement>().enabled = true;
- myPlayer.GetComponent<Attack>().enabled = true;
- Cam.SetActive(true);
- Cam.GetComponent<Follow>().target = myPlayer.transform;
- //myPlayer.transform.FindChild("HealthPrefab").GetComponent<CameraFacingBillboard>().camera = Cam as Camera;
- }
- }else
- {
- StartCoroutine(LaunchDelay());
- }
- }
- }
- void Start() {
- Debug.Log("Start");
- PhotonNetwork.ConnectUsingSettings(VERSION);
- }
- void OnJoinedLobby() {
- Debug.Log("Joined Lobby");
- Terrain.SetActive (true);
- PhotonNetwork.JoinRandomRoom();
- }
- void OnPhotonRandomJoinFailed()
- {
- RoomOptions roomOptions = new RoomOptions() { isVisible = true, maxPlayers = 6 };
- PhotonNetwork.CreateRoom(null, roomOptions, TypedLobby.Default);
- }
- void OnJoinedRoom()
- {
- PhotonView photonView = PhotonView.Get(this);
- photonView.RPC("UpdatePlayerCount", PhotonTargets.AllBuffered, true);
- if (BlueFull)
- {
- photonView.RPC("UpdateRedTeamCount", PhotonTargets.AllBuffered, true);
- localTeam = 2;
- }else if (RedFull)
- {
- photonView.RPC("UpdateBlueTeamCount", PhotonTargets.AllBuffered, true);
- localTeam = 1;
- }else if (TeamBlue > TeamRed)
- {
- photonView.RPC("UpdateRedTeamCount", PhotonTargets.AllBuffered, true);
- localTeam = 2;
- }else if(TeamRed > TeamBlue)
- {
- photonView.RPC("UpdateBlueTeamCount", PhotonTargets.AllBuffered, true);
- localTeam = 1;
- }else if(TeamBlue == TeamRed)
- {
- if(Random.Range(0, 3) == 1)
- {
- photonView.RPC("UpdateBlueTeamCount", PhotonTargets.AllBuffered, true);
- localTeam = 1;
- }else{
- photonView.RPC("UpdateRedTeamCount", PhotonTargets.AllBuffered, true);
- localTeam = 2;
- }
- }
- }
- void OnLeftRoom()
- {
- PhotonView photonView = PhotonView.Get(this);
- photonView.RPC("UpdatePlayerCount", PhotonTargets.AllBuffered, false);
- if(localTeam == 1)
- {
- photonView.RPC("UpdateBlueTeamCount", PhotonTargets.AllBuffered, false);
- }
- else
- {
- photonView.RPC("UpdateRedTeamCount", PhotonTargets.AllBuffered, false);
- }
- }
- IEnumerator LaunchDelay()
- {
- waitingLaunch = true;
- yield return new WaitForSeconds(5);
- waitLaunch = true;
- waitingLaunch = false;
- }
- void OnGUI()
- {
- GUIStyle MainStyle = new GUIStyle();
- MainStyle.font = font;
- if (waiting)
- {
- GUI.Label(new Rect(0, 0, 100, 10), "Players In Room: " + playerCount, MainStyle);
- GUI.Label(new Rect(0, 20, 100, 10), "Players In Red: " + TeamRed, MainStyle);
- GUI.Label(new Rect(0, 40, 100, 10), "Players In Blue: " + TeamBlue, MainStyle);
- Rect TabList = new Rect(0, 100, 100, 10);
- foreach (PhotonPlayer pl in PhotonNetwork.playerList)
- {
- GUI.Label(TabList, "" + pl.name, MainStyle);
- TabList.y += 20;
- }
- GUI.Label(new Rect(Screen.width / 2, 0, 100, 10), "Hello, " + PhotonNetwork.player.name, MainStyle);
- }
- }
- [PunRPC]
- void UpdatePlayerCount(bool AddToCount)
- {
- if (AddToCount)
- {
- playerCount += 1;
- }
- else
- {
- playerCount -= 1;
- }
- }
- [PunRPC]
- void UpdateRedTeamCount(bool AddToCount)
- {
- if (AddToCount)
- {
- TeamRed += 1;
- }
- else
- {
- TeamRed -= 1;
- }
- }
- [PunRPC]
- void UpdateBlueTeamCount(bool AddToCount)
- {
- if (AddToCount)
- {
- TeamBlue += 1;
- }
- else
- {
- TeamBlue -= 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment