mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-12-12 13:42:32 +00:00
86 lines
2.5 KiB
C#
86 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using SanAndreasUnity.Net;
|
|
using UnityEngine;
|
|
|
|
namespace SanAndreasUnity.GameModes
|
|
{
|
|
public class GameModeManager : MonoBehaviour
|
|
{
|
|
public static GameModeManager Instance { get; private set; }
|
|
|
|
public class GameModeInfo
|
|
{
|
|
public string Name { get; }
|
|
public string Description { get; }
|
|
public System.Action ActivationCallback { get; }
|
|
|
|
public GameModeInfo(string name, string description, Action activationCallback)
|
|
{
|
|
Name = name;
|
|
Description = description;
|
|
ActivationCallback = activationCallback;
|
|
}
|
|
}
|
|
|
|
List<GameModeInfo> m_gameModes = new List<GameModeInfo>();
|
|
public IReadOnlyList<GameModeInfo> GameModes => m_gameModes;
|
|
|
|
private GameModeInfo m_activeGameMode;
|
|
|
|
private GameModeInfo m_selectedGameMode;
|
|
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
NetManager.Instance.onServerStatusChanged += OnServerStatusChanged;
|
|
}
|
|
|
|
private void OnServerStatusChanged()
|
|
{
|
|
if (!NetStatus.IsServer)
|
|
return;
|
|
|
|
if (null == m_selectedGameMode)
|
|
return;
|
|
|
|
ActivateGameMode(m_selectedGameMode);
|
|
}
|
|
|
|
public void RegisterGameMode(GameModeInfo gameModeInfo)
|
|
{
|
|
m_gameModes.Add(gameModeInfo);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Select game mode which should be activated when the server starts.
|
|
/// </summary>
|
|
public void SelectGameMode(GameModeInfo gameModeInfo)
|
|
{
|
|
if (m_activeGameMode != null)
|
|
{
|
|
throw new Exception("Can not select game mode if there is an active game mode");
|
|
}
|
|
|
|
m_selectedGameMode = gameModeInfo;
|
|
}
|
|
|
|
private void ActivateGameMode(GameModeInfo gameModeInfo)
|
|
{
|
|
if (m_activeGameMode != null)
|
|
{
|
|
if (m_activeGameMode == gameModeInfo)
|
|
throw new Exception($"Can not activate game mode '{gameModeInfo.Name}' because it is already activated");
|
|
throw new Exception($"Can not activate game mode '{gameModeInfo.Name}' because another one ('{m_activeGameMode.Name}') is already activated");
|
|
}
|
|
|
|
m_activeGameMode = gameModeInfo;
|
|
gameModeInfo.ActivationCallback();
|
|
}
|
|
}
|
|
}
|