-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestObjectiveInstance.cs
More file actions
89 lines (78 loc) · 3.12 KB
/
Copy pathQuestObjectiveInstance.cs
File metadata and controls
89 lines (78 loc) · 3.12 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
using System;
using UnityEngine;
using UnityEngine.Serialization;
namespace QuestSystem
{
/**
* <summary>
* Represents the state of a quest objective, tracking its completion status and how many times it has been completed.
* We use a separate instance class to manage the state of quest objectives independently from their definitions
* so that multiple players or sessions can have different states for the same quest objective.
* </summary>
*/
[Serializable]
public class QuestObjectiveInstance
{
public QuestObjective objective;
public int totalCompletions; // Tracks the total number of times the objective has been completed
public int
conditionsMetCount; // Tracks how many times the objective's conditions have been met without being completed
public int maxCompletions;
private readonly object lockObject = new object();
public bool ConditionsMet(int times = 1) => conditionsMetCount >= times;
public QuestObjectiveInstance(QuestObjective objective)
{
this.objective = objective;
this.maxCompletions = objective.maxCompletions;
}
/**
* <summary>
* Marks the objective as completed, incrementing the completion count.
* We want this to just increase the count, as some objectives may be repeatable, so
* if there is another objective that requires this one to be completed some amount, we should track that.
* </summary>
*
* <returns>True if the conditions were marked as met, false if already at max completions.</returns>
*/
public bool MarkConditionsMet()
{
if (conditionsMetCount >= maxCompletions && maxCompletions > 0)
{
return false;
}
conditionsMetCount =
Mathf.Clamp(conditionsMetCount + 1, 0, maxCompletions > 0 ? maxCompletions : int.MaxValue);
return true;
}
/**
* <summary>
* Marks the objective as completed by the receiver, incrementing the receiver completion count.
* This is used when the objective's receiver successfully executes the objective.
* </summary>
* <returns>True if the objective was completed by the receiver, false if there are no pending executions.</returns>
*/
public bool TryCompleteByReceiver()
{
lock (lockObject)
{
if (conditionsMetCount < 1 || (totalCompletions >= maxCompletions && maxCompletions > 0))
{
return false;
}
totalCompletions++;
conditionsMetCount = Mathf.Clamp(conditionsMetCount - 1, 0,
maxCompletions > 0 ? maxCompletions : int.MaxValue);
return true;
}
}
/**
* <summary>
* Marks the objective as never completed, resetting the completion counts.
* </summary>
*/
public void ResetCompletions()
{
conditionsMetCount = 0;
}
}
}