Repository navigation
Expand file tree
/
Copy pathPlayerQuestLog.cs
More file actions
97 lines (76 loc) · 2.7 KB
/
Copy pathPlayerQuestLog.cs
File metadata and controls
97 lines (76 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityRPG.Quest
{
[DisallowMultipleComponent]
public sealed class PlayerQuestLog : MonoBehaviour
{
private readonly List<RuntimeQuest> quests = new();
public event Action<RuntimeQuest> QuestAccepted;
public event Action<RuntimeQuest> QuestProgressChanged;
public event Action<RuntimeQuest> QuestReadyToTurnIn;
public event Action<RuntimeQuest> QuestCompleted;
public event Action QuestsRestored;
public IReadOnlyList<RuntimeQuest> Quests => quests;
public bool TryAcceptQuest(QuestDefinition definition)
{
if (definition == null || FindQuest(definition.QuestId) != null)
return false;
RuntimeQuest quest = new RuntimeQuest(definition);
quests.Add(quest);
QuestAccepted?.Invoke(quest);
return true;
}
public bool AddProgress(QuestObjectiveType type, string targetId, int amount = 1)
{
if (string.IsNullOrWhiteSpace(targetId) || amount <= 0)
return false;
bool changed = false;
for (int i = 0; i < quests.Count; i++)
{
RuntimeQuest quest = quests[i];
QuestState previousState = quest.State;
if (!quest.AddProgress(type, targetId, amount))
continue;
changed = true;
QuestProgressChanged?.Invoke(quest);
if (previousState != QuestState.ReadyToTurnIn && quest.State == QuestState.ReadyToTurnIn)
QuestReadyToTurnIn?.Invoke(quest);
}
return changed;
}
public bool TryCompleteQuest(string questId)
{
RuntimeQuest quest = FindQuest(questId);
if (quest == null || !quest.Complete())
return false;
QuestCompleted?.Invoke(quest);
return true;
}
internal void ClearForRestore()
{
quests.Clear();
}
internal bool TryAddRestoredQuest(RuntimeQuest quest)
{
if (quest == null || FindQuest(quest.Definition.QuestId) != null)
return false;
quests.Add(quest);
return true;
}
public RuntimeQuest FindQuest(string questId)
{
for (int i = 0; i < quests.Count; i++)
{
if (quests[i].Definition.QuestId == questId)
return quests[i];
}
return null;
}
internal void NotifyRestoreCompleted()
{
QuestsRestored?.Invoke();
}
}
}