Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- using XInputDotNetPure;
- using Assets.Scripts;
- using UnityEngine.UI;
- using System.Diagnostics;
- using System.Threading;
- using Photon;
- public class ServerManager : PunBehaviour {
- string _ip = "";
- public bool _server;
- public bool _teamMatch = false;
- public bool _canRespawn = true;
- public int _playerCount;
- public int _playerCountX;
- public int _playerCountY;
- public int _playerSpacing;
- public bool isOffline;
- TeamManager teamManager;
- private bool _connect = false;
- private bool _playerCreated;
- private int _lastYAllocation;
- private int _lastXAllocation;
- private bool _aiPlayersSpawned;
- public int localPlayers;
- GameObject newestPlayer;
- Vector3 lastSpawned = new Vector3(0, 1, 0);
- private bool roomCreated;
- private bool isMatchMaking;
- private bool isCustomGame;
- GameType gameType;
- private bool customGameReadyToConfigure;
- private byte maxPlayers;
- public Text roomName;
- public Dropdown customGameType;
- public Dropdown matchMakingGameType;
- public Dropdown playersDropDown;
- public Toggle offlineToggle;
- public GameObject[] menus;
- public int playersRequiredToStartMatch;
- private Stopwatch levelTimer;
- private bool sentLevelLoadRequest;
- private int retryAttempts;
- public bool paused;
- public bool levelLoaded { get; private set; }
- public bool isDud;
- public event EventHandler matchStarted;
- // Use this for initialization
- void Start() {
- if (!isDud) {
- levelTimer = new Stopwatch();
- PhotonNetwork.automaticallySyncScene = true;
- PhotonNetwork.sendRate = 30;
- PhotonNetwork.sendRateOnSerialize = 30;
- DontDestroyOnLoad(gameObject);
- teamManager = GetComponent<TeamManager>();
- if (ControllerConfig.HasMouseKeyboardConfigured) {
- _playerCount++;
- }
- for (int i = 0; i < 4 - ControllerConfig.Offset; ++i) {
- PlayerIndex testPlayerIndex = (PlayerIndex)i;
- GamePadState testState = GamePad.GetState(testPlayerIndex);
- if (testState.IsConnected) {
- _playerCount++;
- }
- }
- Application.LoadLevel(UnityEngine.Random.Range(1, 3));
- }
- }
- public void SetGameType() {
- if (isMatchMaking) {
- gameType = (GameType)matchMakingGameType.value;
- }
- if (isCustomGame) {
- gameType = (GameType)customGameType.value;
- }
- teamManager.gameType = gameType;
- }
- public void GoBack() {
- isCustomGame = false;
- isMatchMaking = false;
- menus[0].SetActive(true);
- menus[1].SetActive(false);
- menus[2].SetActive(false);
- menus[3].SetActive(false);
- menus[4].SetActive(false);
- }
- public void GoMatchmaking() {
- isMatchMaking = true;
- menus[0].SetActive(false);
- menus[1].SetActive(true);
- menus[2].SetActive(false);
- menus[3].SetActive(false);
- menus[4].SetActive(false);
- }
- public void GoCustomGames() {
- isCustomGame = true;
- menus[0].SetActive(false);
- menus[1].SetActive(false);
- menus[2].SetActive(true);
- menus[3].SetActive(false);
- menus[4].SetActive(false);
- }
- public void DisableMenus() {
- menus[0].SetActive(false);
- menus[1].SetActive(false);
- menus[2].SetActive(false);
- menus[3].SetActive(false);
- menus[4].SetActive(false);
- paused = false;
- }
- public void GoControllerSettings() {
- menus[0].SetActive(false);
- menus[1].SetActive(false);
- menus[2].SetActive(false);
- menus[3].SetActive(false);
- menus[4].SetActive(true);
- }
- public void StartMatchmaking() {
- PhotonNetwork.offlineMode = false;
- PhotonNetwork.ConnectUsingSettings("v4.2");
- DisableMenus();
- }
- public void StartCustomGame() {
- PhotonNetwork.offlineMode = offlineToggle.isOn;
- if (!PhotonNetwork.offlineMode) {
- PhotonNetwork.ConnectUsingSettings("v4.2");
- } else {
- CreateCustomMatch();
- }
- isCustomGame = true;
- DisableMenus();
- }
- public void StartLocalGame() {
- PhotonNetwork.offlineMode = true;
- isCustomGame = true;
- DisableMenus();
- }
- public void CreateCustomMatch() {
- RoomOptions option = new RoomOptions();
- TypedLobby lobby = new TypedLobby();
- option.maxPlayers = (byte)((playersDropDown.value + 1) * 4);
- option.customRoomProperties = new ExitGames.Client.Photon.Hashtable();
- option.customRoomProperties.Add("GT", (int)gameType);
- PhotonNetwork.LeaveRoom();
- PhotonNetwork.JoinOrCreateRoom(roomName.ToString(), option, lobby);
- DisableMenus();
- }
- void OnConnectedToMaster() {
- if (isMatchMaking) {
- UnityEngine.Debug.Log("Connected to matchmaking.");
- UnityEngine.Debug.Log(string.Format("Attemting to join game with game type {0}. Attempt {1}", gameType.ToString(), retryAttempts));
- ExitGames.Client.Photon.Hashtable expectedCustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "GT", (int)gameType } };
- PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties, 16);
- }
- if (isCustomGame) {
- CreateCustomMatch();
- }
- }
- public void DisplayPause() {
- paused = true;
- menus[0].SetActive(false);
- menus[1].SetActive(false);
- menus[2].SetActive(false);
- menus[3].SetActive(true);
- Cursor.visible = true;
- }
- public void HidePause() {
- paused = false;
- DisableMenus();
- Cursor.visible = false;
- }
- public void Disconnect() {
- paused = false;
- PhotonNetwork.Disconnect();
- GoBack();
- foreach (PlayerController player in FindObjectsOfType<PlayerController>()) {
- Destroy(player.gameObject);
- }
- _playerCreated = false;
- _server = false;
- teamManager.hasAssignedColors = false;
- teamManager.hasEnumeratedTeamItems = false;
- levelLoaded = false;
- Application.LoadLevel(0);
- Destroy(gameObject);
- }
- void OnPhotonRandomJoinFailed() {
- if (isMatchMaking) {
- if (retryAttempts++ < 30) {
- OnConnectedToMaster();
- } else {
- RoomOptions options = new RoomOptions();
- TypedLobby lobby = new TypedLobby();
- options.isOpen = true;
- options.isVisible = true;
- options.maxPlayers = 16;
- options.customRoomProperties = new ExitGames.Client.Photon.Hashtable();
- options.customRoomPropertiesForLobby = new string[2] { "GT", gameType.ToString() };
- options.customRoomProperties.Add("GT", (int)gameType);
- PhotonNetwork.CreateRoom(null, options, lobby);
- UnityEngine.Debug.Log("No rooms found. Creating a new one.");
- }
- }
- }
- // Update is called once per frame
- void Update() {
- if (!isDud) {
- if (PhotonNetwork.playerList.Length >= playersRequiredToStartMatch) {
- if (matchStarted != null) {
- matchStarted(this, EventArgs.Empty);
- }
- }
- if (PhotonNetwork.connectionStateDetailed == PeerState.Joined) {
- _server = PhotonNetwork.isMasterClient;
- if (_server || PhotonNetwork.offlineMode) {
- if (!sentLevelLoadRequest) {
- levelLoaded = false;
- if (gameType == GameType.CTF) {
- PhotonNetwork.LoadLevel(1);
- }
- if (gameType == GameType.Territores) {
- PhotonNetwork.LoadLevel(2);
- }
- if (gameType == GameType.Elimination) {
- PhotonNetwork.LoadLevel(3);
- }
- sentLevelLoadRequest = true;
- }
- }
- if (levelTimer == null) {
- levelTimer = new Stopwatch();
- }
- if (!levelTimer.IsRunning && !levelLoaded) {
- levelTimer.Start();
- }
- if (levelTimer.ElapsedMilliseconds >= 5000) {
- //UnityEngine.Debug.Log("Level Loaded.");
- levelLoaded = true;
- if (teamManager.hasAssignedColors) {
- levelTimer.Reset();
- if (!_playerCreated) {
- Spawn[] spawns = FindObjectsOfType<Spawn>();
- for (int i = 0; i < _playerCount; i++) {
- SpawnPlayer(spawns[UnityEngine.Random.Range(0, spawns.Length - 1)].transform.position);
- }
- _playerCreated = true;
- }
- }
- }
- }
- }
- #if !UNITY_EDITOR
- //Screen.lockCursor = true;
- #endif
- #region Old Code
- //if (!isOffline) {
- // if (isMatchMaking) {
- // if (PhotonNetwork.connectionStateDetailed == PeerState.ConnectedToMaster) {
- // if (!_server) {
- // PhotonNetwork.JoinRandomRoom();
- // _connect = true;
- // } else {
- // PhotonNetwork.CreateRoom(Guid.NewGuid().ToString());
- // PhotonNetwork.LoadLevel(UnityEngine.Random.Range(1, 2));
- // }
- // }
- // if (PhotonNetwork.connectionStateDetailed == PeerState.Joined) {
- // if (_server) {
- // _connect = true;
- // //SpawnPlayer(_teamMatch ? "Team A" : DateTime.Now.Ticks.ToString());
- // }
- // if (PhotonNetwork.playerList.Length >= 2) {
- // if (_connect) {
- // if (!_playerCreated) {
- // if (teamManager.hasAssignedColors) {
- // for (int i = 0; i < _playerCount; i++) {
- // SpawnPlayer(_teamMatch ? "Team A" : DateTime.Now.Ticks.ToString());
- // }
- // _connect = false;
- // _playerCreated = true;
- // }
- // }
- // }
- // } else if (_server && !_playerCreated) {
- // //if (teamManager.hasAssignedColors) {
- // // for (int i = 0; i < _playerCount; i++) {
- // // SpawnPlayer(_teamMatch ? "Team A" : DateTime.Now.Ticks.ToString());
- // // }
- // // _connect = false;
- // // _playerCreated = true;
- // //}
- // }
- // }
- // } else {
- // if (!_playerCreated) {
- // if (!roomCreated) {
- // PhotonNetwork.CreateRoom(Guid.NewGuid().ToString());
- // Application.LoadLevel(UnityEngine.Random.Range(1, 2));
- // roomCreated = true;
- // }
- // if (teamManager.hasAssignedColors) {
- // //SpawnPlayer();
- // for (int i = 0; i < _playerCount; i++) {
- // SpawnPlayer(_teamMatch ? "Team A" : DateTime.Now.Ticks.ToString());
- // }
- // _playerCreated = true;
- // }
- // }
- // }
- //}
- #endregion Old Code
- }
- private void SpawnAI() {
- //MasterServer.RegisterHost("Test", "Test", "");
- if (!_aiPlayersSpawned) {
- for (int x = 0; x < _playerCount; x++) {
- string team = x % 2 == 0 ? "Team A" : "Team B";
- SpawnAI(_teamMatch ? team : DateTime.Now.Ticks.ToString());
- _aiPlayersSpawned = true;
- }
- }
- }
- public void SpawnPlayer(Vector3 location) {
- newestPlayer = PhotonNetwork.Instantiate("RobotA", location, Quaternion.identity, 0) as GameObject;
- newestPlayer.GetComponent<PlayerController>().localPlayerNumber = localPlayers++;
- teamManager.CmdRegisterPlayer(newestPlayer.GetComponent<PlayerController>().photonView.viewID);
- newestPlayer.GetComponent<PlayerController>().serverPrepDone = true;
- // photonView.RPC("ModifyPlayer", PhotonTargets.All, false, false);
- }
- public void SpawnAI(Vector3 location, string faction) {
- newestPlayer = PhotonNetwork.Instantiate("RobotA", location, Quaternion.identity, 0) as GameObject;
- // photonView.RPC("ModifyPlayer", PhotonTargets.All, true, !_teamMatch);
- }
- public void SpawnPlayer() {
- if (_lastXAllocation >= _playerCountX) {
- _lastXAllocation = 0;
- }
- if (_lastXAllocation < _playerCountX) {
- if (_lastYAllocation >= _playerCountY) {
- _lastYAllocation = 0;
- _lastXAllocation++;
- }
- if (_lastYAllocation < _playerCountY) {
- SpawnPlayer(new Vector3(_lastXAllocation * _playerSpacing, 1, _lastYAllocation++ * _playerSpacing));
- }
- }
- }
- public void SpawnAI(string faction) {
- if (_lastXAllocation >= _playerCountX) {
- _lastXAllocation = 0;
- }
- if (_lastXAllocation < _playerCountX) {
- if (_lastXAllocation < _playerCountX) {
- if (_lastYAllocation >= _playerCountY) {
- _lastYAllocation = 0;
- _lastXAllocation++;
- }
- if (_lastYAllocation < _playerCountY) {
- SpawnAI(new Vector3(_lastXAllocation * _playerSpacing, 1, _lastYAllocation++ * _playerSpacing), faction);
- }
- }
- }
- }
- [PunRPC]
- public void ModifyPlayer(Color faction, bool isAI, bool isFreeForAll) {
- if (newestPlayer != null) {
- PlayerController controller = newestPlayer.GetComponent<PlayerController>();
- if (controller != null) {
- controller.GetComponent<FactionManager>().color = faction;
- //controller._useAI = isAI;
- //controller._freeForAll = isFreeForAll;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment