Repository navigation
Expand file tree
/
Copy pathRuntimeQuest.cs
More file actions
87 lines (66 loc) · 2.46 KB
/
Copy pathRuntimeQuest.cs
File metadata and controls
87 lines (66 loc) · 2.46 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
using System.Collections.Generic;
namespace UnityRPG.Quest
{
public sealed class RuntimeQuest
{
private readonly List<RuntimeQuestObjective> objectives = new();
public QuestDefinition Definition { get; }
public IReadOnlyList<RuntimeQuestObjective> Objectives => objectives;
public QuestState State { get; private set; } = QuestState.Active;
public RuntimeQuest(QuestDefinition definition)
{
Definition = definition;
for (int i = 0; i < definition.Objectives.Count; i++)
objectives.Add(new RuntimeQuestObjective(definition.Objectives[i]));
}
public bool AddProgress(QuestObjectiveType type, string targetId, int amount)
{
if (State != QuestState.Active)
return false;
bool changed = false;
for (int i = 0; i < objectives.Count; i++)
{
RuntimeQuestObjective objective = objectives[i];
if (objective.Matches(type, targetId) && objective.AddProgress(amount))
changed = true;
}
if (changed && AreAllObjectivesCompleted())
State = QuestState.ReadyToTurnIn;
return changed;
}
public bool Complete()
{
if (State != QuestState.ReadyToTurnIn)
return false;
State = QuestState.Completed;
return true;
}
internal bool TryRestoreObjectiveProgress(int objectiveIndex, int amount)
{
if (objectiveIndex < 0 || objectiveIndex >= objectives.Count)
return false;
return objectives[objectiveIndex].TryRestoreProgress(amount);
}
internal bool TryRestoreState(QuestState state)
{
bool allCompleted = AreAllObjectivesCompleted();
if (state == QuestState.Active && allCompleted)
return false;
if ((state == QuestState.ReadyToTurnIn || state == QuestState.Completed) && !allCompleted)
return false;
State = state;
return true;
}
private bool AreAllObjectivesCompleted()
{
if (objectives.Count == 0)
return false;
for (int i = 0; i < objectives.Count; i++)
{
if (!objectives[i].IsCompleted)
return false;
}
return true;
}
}
}