261 lines
8.7 KiB
C#
261 lines
8.7 KiB
C#
//#define _DIFFICULTYDEBUG
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
|
|
public class DifficultyEstablisher : MonoBehaviour
|
|
{
|
|
DungeonGen dung;
|
|
|
|
[Header("Difficulty Rates")]
|
|
[Space(5)]
|
|
public bool smartEdit = false;
|
|
[Range(0, 100)]
|
|
public float easyRate = 0;
|
|
float easyRateOriginal;
|
|
[Range(0, 100)]
|
|
public float mediumRate = 0;
|
|
float mediumRateOriginal;
|
|
[Range(0, 100)]
|
|
public float hardRate = 0;
|
|
float hardRateOriginal;
|
|
|
|
[Header("Difficulty pity progress")]
|
|
[Space(5)]
|
|
public float easyPityProgress = 0;
|
|
public float mediumPityProgress = 0;
|
|
public float hardPityProgress = 0;
|
|
|
|
[Space(10)]
|
|
[MinMaxCustomSlider]
|
|
public ValueRange mediumRoomsMargin;
|
|
|
|
[Space(10)]
|
|
[MinMaxCustomSlider]
|
|
public ValueRange hardRoomsMargin;
|
|
|
|
//Room amount
|
|
[SerializeField]
|
|
List<GameObject> easyRooms;
|
|
[SerializeField]
|
|
List<GameObject> medRooms;
|
|
[SerializeField]
|
|
List<GameObject> hardRooms;
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (smartEdit)
|
|
{
|
|
float totalRate = easyRate + mediumRate + hardRate;
|
|
if (totalRate != 100)
|
|
{
|
|
easyRate = (easyRate * 100) / (totalRate);
|
|
mediumRate = (mediumRate * 100) / (totalRate);
|
|
hardRate = (hardRate * 100) / (totalRate);
|
|
}
|
|
}
|
|
|
|
#region MinMaxSlider to int
|
|
mediumRoomsMargin.MaxValue = Mathf.Round(mediumRoomsMargin.MaxValue);
|
|
mediumRoomsMargin.MinValue = Mathf.Round(mediumRoomsMargin.MinValue);
|
|
|
|
mediumRoomsMargin.MaxLimit = Mathf.Round(mediumRoomsMargin.MaxLimit);
|
|
mediumRoomsMargin.MinLimit = Mathf.Round(mediumRoomsMargin.MinLimit);
|
|
|
|
hardRoomsMargin.MaxValue = Mathf.Round(hardRoomsMargin.MaxValue);
|
|
hardRoomsMargin.MinValue = Mathf.Round(hardRoomsMargin.MinValue);
|
|
|
|
hardRoomsMargin.MaxLimit = Mathf.Round(hardRoomsMargin.MaxLimit);
|
|
hardRoomsMargin.MinLimit = Mathf.Round(hardRoomsMargin.MinLimit);
|
|
#endregion
|
|
|
|
mediumRoomsMargin.Name = "Medium room limits";
|
|
hardRoomsMargin.Name = "Hard room limits";
|
|
}
|
|
|
|
public static DifficultyEstablisher instance;
|
|
private void Awake()
|
|
{
|
|
instance = this;
|
|
dung = GetComponent<DungeonGen>();
|
|
|
|
easyRateOriginal = easyRate;
|
|
mediumRateOriginal = mediumRate;
|
|
hardRateOriginal = hardRate;
|
|
|
|
easyRooms = new List<GameObject>();
|
|
medRooms = new List<GameObject>();
|
|
hardRooms = new List<GameObject>();
|
|
|
|
}
|
|
|
|
[ContextMenu("Progress Med")]
|
|
private void ProgressMediumRate()
|
|
{
|
|
mediumRate += mediumPityProgress * Utils.difficultyFormula(GameMaster.Instance.LevelNumber, GameMaster.Instance.Difficulty);
|
|
easyRate = 100 - mediumRate - hardRate;
|
|
|
|
}
|
|
|
|
[ContextMenu("Progress Hard")]
|
|
private void ProgressHardRate()
|
|
{
|
|
hardRate += hardPityProgress * Utils.difficultyFormula(GameMaster.Instance.LevelNumber, GameMaster.Instance.Difficulty);
|
|
easyRate = 100 - mediumRate - hardRate;
|
|
}
|
|
|
|
Difficulty getRandomDifficulty()
|
|
{
|
|
float rate = Random.value;
|
|
if (rate <= easyRate / 100)
|
|
{
|
|
ProgressMediumRate();
|
|
ProgressHardRate();
|
|
return Difficulty.Easy;
|
|
}
|
|
else if (rate <= (mediumRate / 100 + easyRate / 100))
|
|
{
|
|
mediumRate = mediumRateOriginal;
|
|
ProgressHardRate();
|
|
|
|
//if (medRooms >= mediumRoomsMargin.MaxValue)
|
|
//{
|
|
// mediumRate = 0;
|
|
// float totalRate = easyRate + hardRooms >= hardRoomsMargin.MaxValue ? 0 : hardRooms;
|
|
// easyRate = (easyRate * 100) / (totalRate);
|
|
|
|
// if (hardRooms < hardRoomsMargin.MaxValue)
|
|
// hardRate = (hardRate * 100) / (totalRate);
|
|
|
|
//}
|
|
return Difficulty.Medium;
|
|
}
|
|
else
|
|
{
|
|
hardRate = hardRateOriginal;
|
|
ProgressMediumRate();
|
|
|
|
//if (hardRooms >= hardRoomsMargin.MaxValue)
|
|
//{
|
|
// hardRate = 0;
|
|
// float totalRate = easyRate + medRooms >= mediumRoomsMargin.MaxValue ? 0 : medRooms;
|
|
// easyRate = (easyRate * 100) / (totalRate);
|
|
|
|
// if(medRooms < mediumRoomsMargin.MaxValue)
|
|
// mediumRate = (mediumRate * 100) / (totalRate);
|
|
|
|
//}
|
|
return Difficulty.Hard;
|
|
}
|
|
}
|
|
|
|
void setDungeonDifficulty()
|
|
{
|
|
|
|
foreach (GameObject r in dung.Dungeon)
|
|
{
|
|
Room localRoom = r.GetComponent<Room>();
|
|
|
|
if (localRoom.Difficulty == Difficulty.None)
|
|
{
|
|
localRoom.debugDiff.easyRate = easyRate;
|
|
localRoom.debugDiff.mediumRate = mediumRate;
|
|
localRoom.debugDiff.hardRate = hardRate;
|
|
localRoom.debugDiff.hardEstablished = false;
|
|
|
|
localRoom.Difficulty = getRandomDifficulty();
|
|
switch (localRoom.Difficulty)
|
|
{
|
|
case Difficulty.Easy:
|
|
easyRooms.Add(r);
|
|
break;
|
|
case Difficulty.Medium:
|
|
medRooms.Add(r);
|
|
break;
|
|
case Difficulty.Hard:
|
|
hardRooms.Add(r);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
#if _DIFFICULTYDEBUG
|
|
Utils.Print("Difficulty scalar: " + Utils.difficultyFormula(GameMaster.Instance.LevelNumber, GameMaster.Instance.Difficulty));
|
|
|
|
Utils.Print("Before:");
|
|
Utils.Print("Easy rooms: " + easyRooms.Count);
|
|
Utils.Print("Medium rooms: " + medRooms.Count);
|
|
Utils.Print("Hard rooms: " + hardRooms.Count);
|
|
#endif
|
|
//Medium rooms margin fix
|
|
if (medRooms.Count<mediumRoomsMargin.MinValue)
|
|
{
|
|
int difference = (int)mediumRoomsMargin.MinValue - medRooms.Count;
|
|
for (int i=0; i<difference; ++i)
|
|
{
|
|
GameObject transferedObject = easyRooms[Random.Range(0, Mathf.Max(easyRooms.Count - 1, 0))];
|
|
transferedObject.GetComponent<Room>().Difficulty = Difficulty.Medium;
|
|
transferedObject.GetComponent<Room>().debugDiff.hardEstablished = true;
|
|
medRooms.Add(transferedObject);
|
|
easyRooms.Remove(transferedObject);
|
|
}
|
|
}
|
|
else if(medRooms.Count > mediumRoomsMargin.MaxValue)
|
|
{
|
|
int difference = medRooms.Count - (int)mediumRoomsMargin.MaxValue;
|
|
for (int i = 0; i < difference; ++i)
|
|
{
|
|
GameObject transferedObject = medRooms[Random.Range(0, Mathf.Max(medRooms.Count - 1, 0))];
|
|
transferedObject.GetComponent<Room>().Difficulty = Difficulty.Easy;
|
|
transferedObject.GetComponent<Room>().debugDiff.hardEstablished = true;
|
|
easyRooms.Add(transferedObject);
|
|
medRooms.Remove(transferedObject);
|
|
}
|
|
}
|
|
|
|
//Hard rooms margin fix
|
|
if (hardRooms.Count < hardRoomsMargin.MinValue)
|
|
{
|
|
int difference = (int)hardRoomsMargin.MinValue - hardRooms.Count;
|
|
for (int i = 0; i < difference; ++i)
|
|
{
|
|
GameObject transferedObject = easyRooms[Random.Range(0, Mathf.Max(easyRooms.Count - 1, 0))];
|
|
transferedObject.GetComponent<Room>().Difficulty = Difficulty.Hard;
|
|
transferedObject.GetComponent<Room>().debugDiff.hardEstablished = true;
|
|
hardRooms.Add(transferedObject);
|
|
easyRooms.Remove(transferedObject);
|
|
}
|
|
}
|
|
else if (hardRooms.Count > hardRoomsMargin.MaxValue)
|
|
{
|
|
int difference = hardRooms.Count - (int)hardRoomsMargin.MaxValue;
|
|
for (int i = 0; i < difference; ++i)
|
|
{
|
|
GameObject transferedObject = hardRooms[Random.Range(0, Mathf.Max(hardRooms.Count - 1, 0))];
|
|
transferedObject.GetComponent<Room>().Difficulty = Difficulty.Easy;
|
|
transferedObject.GetComponent<Room>().debugDiff.hardEstablished = true;
|
|
easyRooms.Add(transferedObject);
|
|
hardRooms.Remove(transferedObject);
|
|
}
|
|
}
|
|
|
|
#if _DIFFICULTYDEBUG
|
|
Utils.Print("After:");
|
|
Utils.Print("Easy rooms: " + easyRooms.Count);
|
|
Utils.Print("Medium rooms: " + medRooms.Count);
|
|
Utils.Print("Hard rooms: " + hardRooms.Count);
|
|
#endif
|
|
|
|
easyRate = easyRateOriginal;
|
|
mediumRate = mediumRateOriginal;
|
|
hardRate = hardRateOriginal;
|
|
}
|
|
|
|
public void SetDifficulty()
|
|
{
|
|
//Invoke(nameof(setDungeonDifficulty), 0.2f);
|
|
setDungeonDifficulty();
|
|
}
|
|
}
|