Created
August 6, 2017 23:33
-
-
Save theWill/18417d640e0ea1ead6305edcbfa5e94b to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
using System.IO; | |
using BeardedManStudios.Forge.Networking.Generated; | |
using BeardedManStudios.Forge.Networking; | |
using System; | |
using BeardedManStudios.Forge.Networking.Unity; | |
using UnityEngine.SceneManagement; | |
using System.Collections.Generic; | |
//This manager is to run across all scenes; Never to be destroyed. | |
public class GameManager : MonoBehaviour | |
{ | |
static GameManager instance; | |
public string curDifficulty; | |
public GameObject networkManagerPrefab; | |
public NetworkManager mgr = null; | |
public const string IP_ADDRESS = "127.0.0.1"; | |
public static ushort PORT_NUM = 15937; | |
public static bool isSpectator = false; | |
public bool hasTutorialRun = false; | |
public bool shallBeHost = false; | |
public GameObject loadingScreenPrefab; | |
//public const string default_networking_config = "port: 4578"; | |
void Awake() | |
{ | |
if (instance) | |
Destroy(gameObject); | |
else | |
{ | |
instance = this; | |
DontDestroyOnLoad(gameObject); | |
} | |
} | |
void Start() | |
{ | |
// Do any firewall opening requests on the operating system | |
NetWorker.PingForFirewall(); | |
Rpc.MainThreadRunner = MainThreadManager.Instance; | |
} | |
public void connectAsClient(String ipToConnect = IP_ADDRESS) | |
{ | |
//NetworkManager.Instance.Networker.serverAccepted += PlayerServerAccepted; | |
if(Network.player.ipAddress == ipToConnect) | |
{ | |
ipToConnect = IP_ADDRESS; | |
Debug.Log("LocalHost detected."); | |
} | |
Debug.Log("Connecting as Client to: " + ipToConnect + ":" + PORT_NUM); | |
NetWorker client = new UDPClient(); | |
((UDPClient)client).Connect(ipToConnect, PORT_NUM); | |
Connected(client); | |
} | |
public void Host() | |
{ | |
NetWorker server= new UDPServer(64); | |
Debug.Log("Hosting at: " + IP_ADDRESS + ":" + PORT_NUM); | |
((UDPServer)server).Connect(IP_ADDRESS, PORT_NUM); | |
server.playerTimeout += (player) => | |
{ | |
Debug.Log("Player " + player.NetworkId + " timed out"); | |
}; | |
Connected(server); | |
} | |
public void Connected(NetWorker networker) | |
{ | |
if (!networker.IsBound) | |
{ | |
Debug.LogError("NetWorker failed to bind"); | |
return; | |
} | |
if (mgr == null) | |
mgr = Instantiate(networkManagerPrefab).GetComponent<NetworkManager>(); | |
mgr.Initialize(networker); | |
if (networker is IServer) | |
{ | |
NetworkObject.Flush(networker); //Called because we are already in the correct scene! | |
} | |
} | |
private void OnApplicationQuit() | |
{ | |
if(NetworkManager.Instance != null) | |
NetworkManager.Instance.Disconnect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment