121 lines
4.0 KiB
C#
121 lines
4.0 KiB
C#
using UnityEngine;
|
|
|
|
public static class DifficultyManager
|
|
{
|
|
private const string ChanceKey = "BotChance"; // òåêóùèé øàíñ (õðàíèì êàê string)
|
|
private const string MaxChanceKey = "BotMaxChance"; // êëþ÷ äëÿ ìàêñèìóìà (òîæå string)
|
|
private const int DefaultMaxChance = 1000; // äåôîëòíûé ìàêñèìóì
|
|
|
|
|
|
|
|
// Ïîäïèøåìñÿ íà ñîáûòèå TextInput.OnTextChanged ïðè ïåðâîé çàãðóçêå êëàññà
|
|
static DifficultyManager()
|
|
{
|
|
// Çàùèòà: TextInput ìîæåò îòñóòñòâîâàòü â ðàíòàéìå (íàïðèìåð ïðè ñáîðêå áåç UI) — èñïîëüçóåì try/catch
|
|
try
|
|
{
|
|
TextInput.OnTextChanged += OnTextInputChanged;
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
// Îáðàáîò÷èê ñîáûòèÿ: åñëè òåêóùèé Chance > Max — ïîäãîíÿåì
|
|
private static void OnTextInputChanged()
|
|
{
|
|
int current = GetChance();
|
|
int max = GetMaxChance();
|
|
if (current > max)
|
|
{
|
|
PlayerPrefs.SetString(ChanceKey, max.ToString());
|
|
PlayerPrefs.Save();
|
|
Debug.Log($"DifficultyManager: adjusted Chance from {current} to Max {max} due to TextInput change.");
|
|
}
|
|
}
|
|
|
|
// Ïîëó÷àåì MaxChance — ÷èòàåì êàê ñòðîêó, ïàðñèì â int, çàùèùàåìñÿ îò íåêîððåêòíûõ çíà÷åíèé
|
|
public static int GetMaxChance()
|
|
{
|
|
string stored = PlayerPrefs.GetString(MaxChanceKey, DefaultMaxChance.ToString());
|
|
if (int.TryParse(stored, out int max))
|
|
{
|
|
if (max < 0) max = DefaultMaxChance;
|
|
return max;
|
|
}
|
|
// Íåêîððåêòíîå çíà÷åíèå — âîññòàíîâèì äåôîëò è âåðí¸ì åãî
|
|
PlayerPrefs.SetString(MaxChanceKey, DefaultMaxChance.ToString());
|
|
PlayerPrefs.Save();
|
|
return DefaultMaxChance;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Ïîëó÷àåì òåêóùåå çíà÷åíèå øàíñà ïîáåäû.
|
|
/// ×èòàåì ñòðîêó, ïàðñèì â int; ïîääåðæèâàåì ìèãðàöèþ ñî ñòàðûõ ôîðìàòîâ (int/ñòðîêà).
|
|
/// Îãðàíè÷èâàåì ñâåðõó GetMaxChance().
|
|
/// </summary>
|
|
public static int GetChance()
|
|
{
|
|
int max = GetMaxChance();
|
|
|
|
// Ñíà÷àëà ïîïûòàåìñÿ ïðî÷èòàòü êàê ñòðîêó
|
|
string storedStr = PlayerPrefs.GetString(ChanceKey, "");
|
|
if (!string.IsNullOrEmpty(storedStr))
|
|
{
|
|
if (int.TryParse(storedStr, out int parsed))
|
|
{
|
|
parsed = Mathf.Clamp(parsed, 0, max);
|
|
// Ñîõðàíÿåì êàê ñòðîêó (íà ñëó÷àé, åñëè ôîðìàò áûë íåêîíñèñòåíòåí)
|
|
PlayerPrefs.SetString(ChanceKey, parsed.ToString());
|
|
PlayerPrefs.Save();
|
|
return parsed;
|
|
}
|
|
// Åñëè ñòðîêà íå ïàðñèòñÿ, ïîïûòàåìñÿ ñ÷èòàòü int íà âñÿêèé ñëó÷àé (ìèãðàöèÿ èç ïðåäûäóùèõ âåðñèé)
|
|
}
|
|
|
|
// Ïîïûòêà ïðî÷èòàòü êàê int (íà ñëó÷àé, åñëè ðàíåå èñïîëüçîâàëè GetInt)
|
|
int storedInt = PlayerPrefs.GetInt(ChanceKey, max);
|
|
storedInt = Mathf.Clamp(storedInt, 0, max);
|
|
// Ñîõðàíèì êàê ñòðîêó äëÿ åäèíîîáðàçèÿ
|
|
PlayerPrefs.SetString(ChanceKey, storedInt.ToString());
|
|
PlayerPrefs.Save();
|
|
return storedInt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Óìåíüøàåì øàíñ (ïîñëå ñîáûòèÿ)
|
|
/// </summary>
|
|
public static void DecreaseChance()
|
|
{
|
|
int current = GetChance();
|
|
if (current > 0)
|
|
{
|
|
current--;
|
|
PlayerPrefs.SetString(ChanceKey, current.ToString());
|
|
PlayerPrefs.Save();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ñáðàñûâàåì øàíñ äî òåêóùåãî MaxChance
|
|
/// </summary>
|
|
public static void ResetChance()
|
|
{
|
|
int max = GetMaxChance();
|
|
PlayerPrefs.SetString(ChanceKey, max.ToString());
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ïðèíóäèòåëüíî ñèíõðîíèçèðóåò òåêóùèé øàíñ ñ ìàêñèìàëüíûì: BotChance := BotMaxChance
|
|
/// Èñïîëüçóåòñÿ, êîãäà íóæíî îáíîâèòü çíà÷åíèå èçâíå ñðàçó.
|
|
/// </summary>
|
|
public static void SyncChanceToMax()
|
|
{
|
|
int max = GetMaxChance();
|
|
PlayerPrefs.SetString(ChanceKey, max.ToString());
|
|
PlayerPrefs.Save();
|
|
Debug.Log($"DifficultyManager: Synced BotChance to BotMaxChance = {max}");
|
|
}
|
|
}
|