SanAndreasUnity/Assets/Scripts/UI/StartGameWindow.cs

121 lines
3.2 KiB
C#
Raw Normal View History

2019-04-23 20:19:26 +00:00
using System.Collections.Generic;
2021-02-19 01:22:03 +00:00
using System.Linq;
2021-02-19 19:36:07 +00:00
using SanAndreasUnity.GameModes;
2019-04-23 20:19:26 +00:00
using UnityEngine;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Net;
namespace SanAndreasUnity.UI
{
public class StartGameWindow : PauseMenuWindow
{
2019-06-25 20:54:28 +00:00
string m_portStr = NetManager.defaultListenPortNumber.ToString();
2019-04-23 20:19:26 +00:00
bool m_dontListen = false;
bool m_dedicatedServer = false;
2019-06-25 20:54:03 +00:00
string m_maxNumPlayersStr = "40";
2019-06-25 21:03:03 +00:00
[SerializeField] string[] m_availableScenes = new string[0];
2021-02-19 01:22:03 +00:00
int m_selectedModeIndex = 0;
bool m_registerAtMasterServer = false;
2019-04-23 20:19:26 +00:00
2021-01-31 21:44:15 +00:00
public int width = 550;
public int height = 400;
public int minStartButtonWidth = 80;
public int minStartButtonHeight = 30;
public int minMapButtonHeight = 20;
public int mapSelectionGridXCount = 4;
2019-04-23 20:19:26 +00:00
StartGameWindow()
{
// set default parameters
this.windowName = "Start Game";
this.useScrollView = true;
}
void Start ()
{
// adjust rect
2021-01-31 21:44:15 +00:00
this.windowRect = GUIUtils.GetCenteredRect(new Vector2(this.width, this.height));
2019-04-23 20:19:26 +00:00
}
void Update()
{
if (PauseMenu.IsOpened)
this.IsOpened = false;
}
2019-04-23 20:19:26 +00:00
protected override void OnWindowGUI ()
{
GUILayout.Label ("Port:");
2019-06-25 20:54:28 +00:00
m_portStr = GUILayout.TextField(m_portStr, GUILayout.Width(100));
2019-04-23 20:19:26 +00:00
m_dontListen = GUILayout.Toggle(m_dontListen, "Don't listen");
2019-04-23 20:19:26 +00:00
m_dedicatedServer = GUILayout.Toggle(m_dedicatedServer, "Dedicated server");
2019-04-23 20:19:26 +00:00
2019-06-25 20:54:03 +00:00
GUILayout.Label("Max num players:");
m_maxNumPlayersStr = GUILayout.TextField(m_maxNumPlayersStr, GUILayout.Width(100));
2021-02-19 01:22:03 +00:00
GUILayout.Label("Game mode:");
m_selectedModeIndex = GUILayout.SelectionGrid(
m_selectedModeIndex,
m_availableScenes.Concat(GameModeManager.Instance.GameModes.Select(gm => gm.Name)).ToArray(),
this.mapSelectionGridXCount,
GUILayout.MinHeight(this.minMapButtonHeight));
GUILayout.Space(5);
m_registerAtMasterServer = GUILayout.Toggle(m_registerAtMasterServer, "Register at master server");
2019-04-23 20:19:26 +00:00
}
2019-04-23 20:19:26 +00:00
protected override void OnWindowGUIAfterContent()
{
GUILayout.Space(40);
2019-04-23 20:19:26 +00:00
GUI.enabled = ! NetStatus.IsServer;
2021-01-31 21:44:15 +00:00
if (GUIUtils.ButtonWithCalculatedSize("Start", this.minStartButtonWidth, this.minStartButtonHeight))
StartGame();
GUI.enabled = true;
2021-01-31 21:44:15 +00:00
GUILayout.Space(10);
2019-04-23 20:19:26 +00:00
}
void StartGame()
{
try
{
2019-06-25 20:56:13 +00:00
ushort port = ushort.Parse(m_portStr);
2021-02-19 01:22:03 +00:00
string scene = m_selectedModeIndex < m_availableScenes.Length
? m_availableScenes[m_selectedModeIndex]
: m_availableScenes[0];
2019-06-25 20:54:03 +00:00
ushort maxNumPlayers = ushort.Parse(m_maxNumPlayersStr);
2019-04-23 20:19:26 +00:00
MasterServerClient.Instance.IsServerRegistrationEnabled = m_registerAtMasterServer;
// select game mode if it is selected in UI
2021-02-19 01:22:03 +00:00
if (m_selectedModeIndex >= m_availableScenes.Length)
{
var gm = GameModeManager.Instance.GameModes[m_availableScenes.Length - m_selectedModeIndex];
GameModeManager.Instance.SelectGameMode(gm);
2021-02-19 01:22:03 +00:00
}
NetManager.StartServer(port, scene, maxNumPlayers, m_dedicatedServer, m_dontListen);
2019-04-23 20:19:26 +00:00
}
catch (System.Exception ex)
{
2019-06-25 20:56:59 +00:00
Debug.LogException(ex);
2019-04-23 20:19:26 +00:00
MessageBox.Show("Error", ex.ToString());
}
}
}
}