게임 매니저를 통해 게임을 관리해보자. 게임 매니저 스크립트를 작성하기 전에 게임 상태를 유지하기 위해 Enum에 GameState Enum형을 추가해준다.
public enum GameState
{
gameStarted,
playingLevel,
engagingEnemies,
bossStage,
engagingBoss,
levelCompleted,
gameWon,
gameLost,
gamePaused,
dungeonOverviewMap,
restartGame
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class GameManager : SingletonMonoBehaviour<GameManager>
{
#region Header DUNGEON LEVELS
[Space(10)]
[Header("DUNGEON LEVELS")]
#endregion Header DUNGEON LEVELS
#region Tooltip
[Tooltip("Populate with the dungeon level scriptable objects")]
#endregion Tooltip
[SerializeField] private List<DungeonLevelSO> dungeonLevelList;
#region Tooltip
[Tooltip("Populate with the starting dungeon level for testing , first level = 0")]
#endregion Tooltip
[SerializeField] private int currentDungeonLevelListIndex = 0;
[HideInInspector] public GameState gameState;
// Start is called before the first frame update
private void Start()
{
gameState = GameState.gameStarted;
}
// Update is called once per frame
private void Update()
{
HandleGameState();
// For testing
if (Input.GetKeyDown(KeyCode.R))
{
gameState = GameState.gameStarted;
}
}
/// <summary>
/// Handle game state
/// </summary>
private void HandleGameState()
{
// Handle game state
switch (gameState)
{
case GameState.gameStarted:
// Play first level
PlayDungeonLevel(currentDungeonLevelListIndex);
break;
}
}
private void PlayDungeonLevel(int dungeonLevelListIndex)
{
}
#region Validation
#if UNITY_EDITOR
private void OnValidate()
{
HelperUtilities.ValidateCheckEnumerableValues(this, nameof(dungeonLevelList), dungeonLevelList);
}
#endif
#endregion Validation
}
GameManager 컴포넌트를 추가해서 GameManager 오브젝트를 만들고 Scriptable Object에서 Levels를 1~6까지 추가해준다.