SanAndreasUnity/Assets/Scripts/GameModes/GameModeManager.cs

55 lines
1.6 KiB
C#
Raw Normal View History

2021-02-19 01:22:03 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
2021-02-19 19:36:07 +00:00
namespace SanAndreasUnity.GameModes
2021-02-19 01:22:03 +00:00
{
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;
void Awake()
{
Instance = this;
}
public void RegisterGameMode(GameModeInfo gameModeInfo)
{
m_gameModes.Add(gameModeInfo);
}
public 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();
}
}
}