77 lines
2.4 KiB
C#

using System;
using State;
using State.Game;
using UnityEngine;
namespace Management
{
/// <summary>
/// Bootstrap is the first user added object that should be run.
/// It should be added to a game object in the Bootstrap scene, which should be the first
/// scene loaded when the game starts.
///
/// Bootstrap initializes the game state machine, and starts it based on
/// the value of "Start Type" selected in the Inspector.
/// </summary>
public class Bootstrap : MonoBehaviour
{
// Hold a static reference to this instance so we can enforce singleton
private static Bootstrap _instance;
// Determines what state the configure state machine will start in
private enum StartType
{
Splash,
MainMenu,
Game
}
[SerializeField] private StartType startType = StartType.Splash;
[SerializeField] private Machine gameStateMachine;
private void Awake()
{
// enforce singleton
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
// if we got this far, this object didn't exist, set the static instance to this and continue normally.
_instance = this;
// ensure object survives scene loads
DontDestroyOnLoad(gameObject);
}
private void Start()
{
// exit game early if we're misconfigured.
if (gameStateMachine == null)
{
Debug.LogError("no state machine for bootstrap");
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
return;
}
// determine what state to start machine in. Useful for quick testing.
// Should be "Splash" for release.
switch (startType)
{
case StartType.Splash:
case StartType.Game:
throw new NotImplementedException();
case StartType.MainMenu:
default:
gameStateMachine.ChangeState(new MainMenuState());
break;
}
}
}
}