94 lines
No EOL
2.6 KiB
C#
94 lines
No EOL
2.6 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
public class MANAGER_QuestProgress : MonoBehaviour
|
|
{
|
|
public static MANAGER_QuestProgress Instance { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
|
|
PlayerPrefs.DeleteAll(); // DELME!!!!
|
|
if (Instance == null){
|
|
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
}
|
|
|
|
else Destroy(gameObject);
|
|
}
|
|
|
|
|
|
private void OnEnable() => MANAGER_GameEvents.OnQuestAction += HandleAction;
|
|
private void OnDisable() => MANAGER_GameEvents.OnQuestAction -= HandleAction;
|
|
|
|
|
|
private void HandleAction(string actionType, string targetId, int amount)
|
|
{
|
|
// 1. Исправлено обращение: используем currentData
|
|
if (MANAGER_RemoteData.Instance == null || MANAGER_RemoteData.Instance.currentData == null) return;
|
|
|
|
// 2. Берем список квестов из актуальных данных
|
|
foreach (var quest in MANAGER_RemoteData.Instance.currentData.quests)
|
|
{
|
|
if (IsQuestCompleted(quest.id)) continue;
|
|
|
|
if (quest.actionType == actionType && quest.targetId == targetId)
|
|
{
|
|
AddProgress(quest.id, amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddProgress(string questId, int amount)
|
|
{
|
|
// Здесь тоже используем currentData
|
|
var quest = MANAGER_RemoteData.Instance.currentData.quests.Find(q => q.id == questId);
|
|
if (quest == null) return;
|
|
|
|
int current = PlayerPrefs.GetInt("Quest_" + questId, 0);
|
|
if (current >= quest.requiredAmount) return;
|
|
|
|
current += amount;
|
|
current = Mathf.Clamp(current, 0, quest.requiredAmount);
|
|
|
|
PlayerPrefs.SetInt("Quest_" + questId, current);
|
|
PlayerPrefs.Save();
|
|
|
|
// Оповещаем UI, что нужно перерисовать прогресс
|
|
MANAGER_GameEvents.TriggerUIUpdate();
|
|
}
|
|
|
|
public int GetProgress(string questId)
|
|
{
|
|
return PlayerPrefs.GetInt("Quest_" + questId, 0);
|
|
}
|
|
|
|
|
|
public bool IsQuestReady(QuestData quest)
|
|
{
|
|
return GetProgress(quest.id) >= quest.requiredAmount;
|
|
}
|
|
|
|
|
|
public bool IsQuestCompleted(string questId)
|
|
{
|
|
return PlayerPrefs.GetInt("QuestCompleted_" + questId, 0) == 1;
|
|
}
|
|
|
|
|
|
public void ClaimReward(QuestData quest)
|
|
{
|
|
if (IsQuestReady(quest) && !IsQuestCompleted(quest.id))
|
|
{
|
|
|
|
PlayerPrefs.SetInt("QuestCompleted_" + quest.id, 1);
|
|
PlayerPrefs.Save();
|
|
|
|
|
|
|
|
MANAGER_GameEvents.OnUIUpdateNeeded?.Invoke();
|
|
}
|
|
}
|
|
} |