RiftMayhem/Assets/Scripts/Difficulty/GameDifficultyController.cs
Pedro Gomes c458ae4e8f Job Update
- refactored job information and what selecting a job means in terms of code/data
- job templates
- networked job selection & job activation state
2024-06-12 22:07:33 +01:00

152 lines
4.2 KiB
C#

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameDifficultyController : MonoBehaviour, IPunObservable
{
[Header("Settings:")]
[SerializeField] private List<UnitDifficultySettings> difficultySettings = new List<UnitDifficultySettings>();
[Header("Events:")]
[SerializeField] private GameEvent onDifficultyChanged;
#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);
InitializeDifficultyLevel();
}
// Start is called before the first frame update
void Start()
{
if (!PhotonNetwork.IsMasterClient) return;
InitializeDifficultyLevel();
}
private void Update()
{
if (!PhotonNetwork.IsMasterClient) return;
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 + 1);
}
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;
}
int receivedDifficulty = 0;
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(currentDifficultyIndex);
}
if (stream.IsReading)
{
receivedDifficulty = (int)stream.ReceiveNext();
if(receivedDifficulty != currentDifficultyIndex)
{
currentDifficultyIndex = receivedDifficulty;
onDifficultyChanged.Raise();
}
Debug.Log($"Networked Difficulty: Set to {difficultySettings[currentDifficultyIndex].name}");
}
}
}