132 lines
4.9 KiB
C#
132 lines
4.9 KiB
C#
using System.Collections;
|
|
using FishNet;
|
|
using UnityEngine;
|
|
using Utils;
|
|
|
|
namespace UI
|
|
{
|
|
public class NewGameMenuUIController : MonoBehaviour
|
|
{
|
|
public GameObject newGameMenuPanel;
|
|
public GameObject startButton;
|
|
public GameObject closeButton;
|
|
|
|
public GameObject sceneLoaderPrefab;
|
|
|
|
private void Awake()
|
|
{
|
|
newGameMenuPanel = gameObject;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
}
|
|
|
|
// Update is called once per frame
|
|
private void Update()
|
|
{
|
|
}
|
|
|
|
public void OnClick_CloseButton()
|
|
{
|
|
GameLogger.Log("Close button");
|
|
// Close the panel
|
|
newGameMenuPanel.SetActive(false);
|
|
}
|
|
|
|
public void OnClick_StartButton()
|
|
{
|
|
GameLogger.Log("NewGameMenuUIController: Start button");
|
|
|
|
if (LoadingScreenUIController.Instance == null)
|
|
{
|
|
GameLogger.Log("NewGameMenuUIController: Creating LoadingScreenUIController");
|
|
var loadingScreenUIController =
|
|
new GameObject("LoadingScreenUIController").AddComponent<LoadingScreenUIController>();
|
|
loadingScreenUIController.transform.SetParent(newGameMenuPanel.transform);
|
|
}
|
|
|
|
LoadingScreenUIController.Instance.TriggerLoadingScreen();
|
|
// NewGameMenuPanel.SetActive(false);
|
|
|
|
// enable the multiplayer host and client
|
|
if (NetworkUIController.Instance == null)
|
|
{
|
|
//create a new network ui controller
|
|
GameLogger.Log("NewGameMenuUIController: Creating NetworkUIController");
|
|
var networkUIController = new GameObject("NetworkUIController").AddComponent<NetworkUIController>();
|
|
networkUIController.transform.SetParent(newGameMenuPanel.transform);
|
|
}
|
|
|
|
NetworkUIController.Instance.OnClick_HostNew();
|
|
|
|
// todo: eventually, we want to make sure that when we play in single player mode, that it is using the right
|
|
// transport method. FishNet supports multiple transport methods, but we only want to use one at a time. Specifically,
|
|
// we will want to use the one that is for a single player game that will allow me to still use the same code.
|
|
|
|
var sceneLoaderObj = Instantiate(sceneLoaderPrefab, newGameMenuPanel.transform);
|
|
var sceneLoader = sceneLoaderObj.GetComponent<SceneLoader>();
|
|
|
|
GameLogger.Log("NewGameMenuUIController: Spawning scene loader");
|
|
if (sceneLoader == null)
|
|
{
|
|
GameLogger.LogError("NewGameMenuUIController: SceneLoader not found");
|
|
return;
|
|
}
|
|
|
|
// create a new scene loader
|
|
sceneLoader.sceneName = "FarmMap";
|
|
sceneLoader.spawnPosition = new Vector3(30, -30, 0);
|
|
|
|
|
|
GameLogger.Log(" Waiting for server to start...");
|
|
StartCoroutine(WaitForServerAndSpawnSceneLoader(sceneLoaderObj));
|
|
// InstanceFinder.ServerManager.Spawn(sceneLoaderObj);
|
|
// var clientId = InstanceFinder.ClientManager.Connection.ClientId;
|
|
|
|
// var sceneLoader = sceneLoaderObj.GetComponent<SceneLoader>();
|
|
// if (sceneLoader != null) sceneLoader.RequestSceneLoadServerRpc(clientId);
|
|
|
|
// spawn the scene loader
|
|
// InstanceFinder.ServerManager.Spawn(sceneLoaderObj);
|
|
|
|
|
|
// var clientId = InstanceFinder.ClientManager.Connection.ClientId;
|
|
// if (sceneLoader != null) sceneLoader.RequestSceneLoadServerRpc(clientId);
|
|
}
|
|
|
|
private IEnumerator WaitForServerAndSpawnSceneLoader(GameObject sceneLoaderObj)
|
|
{
|
|
GameLogger.Log("NewGameMenuUIController: Waiting for server to start...");
|
|
while (!InstanceFinder.IsServerStarted)
|
|
{
|
|
GameLogger.Log("Waiting for server to start...");
|
|
yield return new WaitForSeconds(0.1f);
|
|
}
|
|
|
|
GameLogger.Log("Server started, waiting for clients to connect...");
|
|
while (InstanceFinder.ClientManager.Connection == null || !InstanceFinder.ClientManager.Started)
|
|
{
|
|
GameLogger.LogInfo("Waiting for clients to connect...");
|
|
yield return new WaitForSeconds(0.1f);
|
|
}
|
|
|
|
GameLogger.Log("Server started, spawning scene loader");
|
|
InstanceFinder.ServerManager.Spawn(sceneLoaderObj);
|
|
|
|
yield return new WaitForSeconds(1f);
|
|
|
|
GameLogger.Log("Clients connected, spawning scene loader");
|
|
|
|
var clientId = InstanceFinder.ClientManager.Connection.ClientId;
|
|
var sceneLoader = sceneLoaderObj.GetComponent<SceneLoader>();
|
|
|
|
if (sceneLoader != null)
|
|
sceneLoader.RequestSceneLoadServerRpc(clientId);
|
|
else
|
|
GameLogger.LogError("SceneLoader not found");
|
|
|
|
GameLogger.Log("Completed spawning scene loader");
|
|
}
|
|
}
|
|
} |