Created
May 2, 2011 08:59
-
-
Save AngryAnt/951320 to your computer and use it in GitHub Desktop.
GameManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public enum GameStatus | |
{ | |
Setup, | |
Initializing, | |
ReInitializing, | |
AwaitingMasterServer, | |
Connecting, | |
Hosting, | |
Joining, | |
Running | |
}; | |
public class GameManager : MonoBehaviour | |
{ | |
public Player playerPrefab; | |
public const int maxCargo = 3; | |
private GameGUI gameGUI; | |
private MenuGUI menuGUI; | |
private ProgressGUI progressGUI; | |
private GameStatus networkStatus = GameStatus.Setup; | |
private const string kGameNetworkName = "AtomicBeeHive"; | |
private bool disconnectHandled = false; | |
public GameStatus Status | |
{ | |
set | |
{ | |
Debug.Log (value.ToString ()); | |
networkStatus = value; | |
} | |
get | |
{ | |
return networkStatus; | |
} | |
} | |
public string PlayerName | |
{ | |
get | |
{ | |
return PlayerPrefs.GetString ("Player name", "Player 7.5"); | |
} | |
set | |
{ | |
PlayerPrefs.SetString ("Player name", value); | |
} | |
} | |
public Color PlayerColor | |
{ | |
get | |
{ | |
return new Color ( | |
PlayerPrefs.GetFloat ("Player color R", 1.0f), | |
PlayerPrefs.GetFloat ("Player color G", 1.0f), | |
PlayerPrefs.GetFloat ("Player color B", 1.0f), | |
1.0f | |
); | |
} | |
set | |
{ | |
PlayerPrefs.SetFloat ("Player color R", value.r); | |
PlayerPrefs.SetFloat ("Player color G", value.g); | |
PlayerPrefs.SetFloat ("Player color B", value.b); | |
} | |
} | |
IEnumerator Start () | |
{ | |
DontDestroyOnLoad (this); | |
gameGUI = GetComponent<GameGUI> (); | |
menuGUI = GetComponent<MenuGUI> (); | |
progressGUI = GetComponent<ProgressGUI> (); | |
float timeoutStart = 0.0f; | |
while (Application.isPlaying) | |
{ | |
yield return null; | |
menuGUI.enabled = Status == GameStatus.Setup; | |
gameGUI.enabled = Status == GameStatus.Running; | |
progressGUI.enabled = !menuGUI.enabled && !gameGUI.enabled; | |
switch (Status) | |
{ | |
case GameStatus.Initializing: | |
disconnectHandled = false; | |
Network.Disconnect (); | |
MasterServer.ClearHostList (); | |
MasterServer.RequestHostList (kGameNetworkName); | |
timeoutStart = Time.time; | |
Status = GameStatus.AwaitingMasterServer; | |
break; | |
case GameStatus.ReInitializing: | |
yield return new WaitForSeconds (3.0f); | |
Status = GameStatus.Initializing; | |
break; | |
case GameStatus.AwaitingMasterServer: | |
if (Time.time - timeoutStart > 5.0f) | |
{ | |
timeoutStart = Time.time; | |
if (MasterServer.PollHostList ().Length > 0) | |
{ | |
Network.Connect (MasterServer.PollHostList ()[0]); | |
//Status = GameStatus.Connecting; | |
} | |
else | |
{ | |
Network.InitializeSecurity (); | |
Network.InitializeServer(32, 25000, !Network.HavePublicAddress ()); | |
MasterServer.RegisterHost (kGameNetworkName, "default"); | |
//Status = GameStatus.Hosting; | |
} | |
} | |
break; | |
case GameStatus.Connecting: | |
if (Time.time - timeoutStart < 5.0f) | |
{ | |
Status = GameStatus.Initializing; | |
} | |
break; | |
case GameStatus.Hosting: | |
if (Time.time - timeoutStart < 5.0f) | |
{ | |
Status = GameStatus.Initializing; | |
} | |
break; | |
case GameStatus.Joining: | |
Destroy (GetComponentInChildren<Camera> ()); | |
Destroy (GetComponentInChildren<AudioListener> ()); | |
Player player = (Player)Network.Instantiate (playerPrefab, transform.position, transform.rotation, 0); | |
player.networkView.RPC ("Initialize", RPCMode.AllBuffered, PlayerName, PlayerColor.r, PlayerColor.g, PlayerColor.b); | |
Status = GameStatus.Running; | |
break; | |
} | |
} | |
} | |
void Disconnect () | |
{ | |
MasterServer.UnregisterHost (); | |
Network.Destroy (((PlayerController)FindObjectOfType (typeof (PlayerController))).gameObject); | |
Network.Disconnect (); | |
} | |
void OnApplicationQuit () | |
{ | |
if (Status == GameStatus.Running) | |
{ | |
Disconnect (); | |
} | |
} | |
void OnFailedToConnectToMasterServer () | |
{ | |
Debug.LogError ("Failed to connect to master server!"); | |
Status = GameStatus.ReInitializing; | |
} | |
void OnFailedToConnect () | |
{ | |
Debug.LogError ("Failed to connect to server"); | |
Status = GameStatus.Initializing; | |
} | |
void OnConnectedToServer () | |
{ | |
Debug.Log ("Connected to server"); | |
Status = GameStatus.Joining; | |
} | |
void OnServerInitialized () | |
{ | |
Debug.Log ("Server is live"); | |
Status = GameStatus.Joining; | |
} | |
void OnDisconnectedFromServer () | |
{ | |
if (disconnectHandled) | |
{ | |
return; | |
} | |
disconnectHandled = true; | |
Debug.Log ("Disconnected from server"); | |
Disconnect (); | |
Status = GameStatus.Initializing; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment