150 lines
3.9 KiB
C#
150 lines
3.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class GameDifficultyController : MonoBehaviour
|
|
{
|
|
[Header("Settings:")]
|
|
[SerializeField] private List<UnitDifficultySettings> difficultySettings = new List<UnitDifficultySettings>();
|
|
[Header("UI")]
|
|
[SerializeField] private Button gameDifficultySettingsButton;
|
|
[SerializeField] private List<GameObject> difficultyCornerInfo = new List<GameObject>();
|
|
[Header("Events:")]
|
|
[SerializeField] private GameEvent onDifficultyChanged;
|
|
[Header("Listeners:")]
|
|
[SerializeField] private GameEventListener onJoinedRoom;
|
|
|
|
#region Singleton
|
|
private static GameDifficultyController _instance;
|
|
|
|
// Public reference to the singleton instance
|
|
public static GameDifficultyController Instance
|
|
{
|
|
get
|
|
{
|
|
// If the instance doesn't exist, try to find it in the scene
|
|
if (_instance == null)
|
|
{
|
|
_instance = FindObjectOfType<GameDifficultyController>();
|
|
|
|
// If it's still null, create a new GameObject and add the component
|
|
if (_instance == null)
|
|
{
|
|
GameObject singletonObject = new GameObject(typeof(GameDifficultyController).Name);
|
|
_instance = singletonObject.AddComponent<GameDifficultyController>();
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
int currentDifficultyIndex;
|
|
UnitDifficultySettings currentUnitDifficultySettings;
|
|
|
|
// Optional: Implement any methods or properties you need for your singleton
|
|
|
|
protected void Awake()
|
|
{
|
|
// Ensure there's only one instance
|
|
if (_instance != null && _instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
// If this is the first instance, set it as the singleton
|
|
_instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
onJoinedRoom.Response.AddListener(Init);
|
|
InitializeDifficultyLevel();
|
|
}
|
|
|
|
|
|
private void Init()
|
|
{
|
|
gameDifficultySettingsButton.interactable = true;
|
|
|
|
InitializeDifficultyLevel();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
/*
|
|
if (Input.GetKeyDown(KeyCode.Keypad0)) // Rank E
|
|
{
|
|
SetDifficultyLevel(0);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Keypad1)) // Rank D
|
|
{
|
|
SetDifficultyLevel(1);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Keypad2)) // Rank C
|
|
{
|
|
SetDifficultyLevel(2);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Keypad3)) // Rank B
|
|
{
|
|
SetDifficultyLevel(3);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Keypad4)) // Rank A
|
|
{
|
|
SetDifficultyLevel(4);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.Keypad5)) // Rank S
|
|
{
|
|
SetDifficultyLevel(5);
|
|
}*/
|
|
}
|
|
|
|
public int GetCurrentDifficultyLevel()
|
|
{
|
|
return currentDifficultyIndex;
|
|
}
|
|
|
|
public void InitializeDifficultyLevel()
|
|
{
|
|
currentDifficultyIndex = 0;
|
|
currentUnitDifficultySettings = difficultySettings[currentDifficultyIndex];
|
|
onDifficultyChanged.Raise();
|
|
|
|
}
|
|
public void SetDifficultyLevel(int index)
|
|
{
|
|
if (index == currentDifficultyIndex) return;
|
|
|
|
currentDifficultyIndex = index;
|
|
currentUnitDifficultySettings = difficultySettings[currentDifficultyIndex];
|
|
onDifficultyChanged.Raise();
|
|
}
|
|
public void SetDifficultyLevel(UnitDifficultySettings settings)
|
|
{
|
|
currentUnitDifficultySettings = settings;
|
|
currentDifficultyIndex = difficultySettings.IndexOf(currentUnitDifficultySettings);
|
|
onDifficultyChanged.Raise();
|
|
}
|
|
public UnitDifficultySettings GetCurrentDifficultySettings()
|
|
{
|
|
return currentUnitDifficultySettings;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public enum DifficultyLevels
|
|
{
|
|
E = 0,
|
|
D,
|
|
C,
|
|
B,
|
|
A,
|
|
S,
|
|
SS
|
|
} |