fr.jeanf.questsystem 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,240 +4,258 @@ using jeanf.EventSystem;
4
4
  using UnityEngine;
5
5
  using UnityEngine.Serialization;
6
6
 
7
- public class QuestManager : MonoBehaviour, IDebugBehaviour
7
+ namespace jeanf.questsystem
8
8
  {
9
- public bool isDebug
10
- {
11
- get => _isDebug;
12
- set => _isDebug = value;
13
- }
14
- [SerializeField] private bool _isDebug = false;
15
- [FormerlySerializedAs("loadQuestState")]
16
- [Header("Config")]
17
- [SerializeField] private bool loadSavedQuestState = true;
18
- [Header("Broadcasting on:")] [SerializeField] private StringEventChannelSO questStatusUpdateChannel;
19
- [Header("Broadcasting on:")] [SerializeField] private StringFloatEventChannelSO questProgress;
9
+ public class QuestManager : MonoBehaviour, IDebugBehaviour
10
+ {
11
+ public bool isDebug
12
+ {
13
+ get => _isDebug;
14
+ set => _isDebug = value;
15
+ }
20
16
 
21
- private Dictionary<string, Quest> questMap;
17
+ [SerializeField] private bool _isDebug = false;
22
18
 
23
- // quest start requirements
24
- private int currentPlayerLevel;
19
+ [FormerlySerializedAs("loadQuestState")] [Header("Config")] [SerializeField]
20
+ private bool loadSavedQuestState = true;
25
21
 
26
- private void Awake()
27
- {
28
- questMap = CreateQuestMap();
29
- }
22
+ [Header("Broadcasting on:")] [SerializeField]
23
+ private StringEventChannelSO questStatusUpdateChannel;
30
24
 
31
- private void OnEnable()
32
- {
33
- GameEventsManager.instance.questEvents.onStartQuest += StartQuest;
34
- GameEventsManager.instance.questEvents.onAdvanceQuest += AdvanceQuest;
35
- GameEventsManager.instance.questEvents.onFinishQuest += FinishQuest;
25
+ [Header("Broadcasting on:")] [SerializeField]
26
+ private StringFloatEventChannelSO questProgress;
36
27
 
37
- GameEventsManager.instance.questEvents.onQuestStepStateChange += QuestStepStateChange;
28
+ private Dictionary<string, Quest> questMap;
38
29
 
39
- GameEventsManager.instance.playerEvents.onPlayerLevelChange += PlayerLevelChange;
40
- }
30
+ // quest start requirements
31
+ private int currentPlayerLevel;
41
32
 
42
- private void OnDisable()
43
- {
44
- GameEventsManager.instance.questEvents.onStartQuest -= StartQuest;
45
- GameEventsManager.instance.questEvents.onAdvanceQuest -= AdvanceQuest;
46
- GameEventsManager.instance.questEvents.onFinishQuest -= FinishQuest;
33
+ private void Awake()
34
+ {
35
+ questMap = CreateQuestMap();
36
+ }
47
37
 
48
- GameEventsManager.instance.questEvents.onQuestStepStateChange -= QuestStepStateChange;
38
+ private void OnEnable()
39
+ {
40
+ GameEventsManager.instance.questEvents.onStartQuest += StartQuest;
41
+ GameEventsManager.instance.questEvents.onAdvanceQuest += AdvanceQuest;
42
+ GameEventsManager.instance.questEvents.onFinishQuest += FinishQuest;
49
43
 
50
- GameEventsManager.instance.playerEvents.onPlayerLevelChange -= PlayerLevelChange;
51
- }
44
+ GameEventsManager.instance.questEvents.onQuestStepStateChange += QuestStepStateChange;
52
45
 
53
- private void Start()
54
- {
55
-
56
- foreach (Quest quest in questMap.Values)
57
- {
58
- // initialize any loaded quest steps
59
- if (quest.state == QuestState.IN_PROGRESS)
60
- {
61
- quest.InstantiateCurrentQuestStep(this.transform);
62
- }
63
- // broadcast the initial state of all quests on startup
64
- GameEventsManager.instance.questEvents.QuestStateChange(quest);
46
+ GameEventsManager.instance.playerEvents.onPlayerLevelChange += PlayerLevelChange;
65
47
  }
66
- }
67
48
 
68
- private void ChangeQuestState(string id, QuestState state)
69
- {
70
- Quest quest = GetQuestById(id);
71
- quest.state = state;
72
- GameEventsManager.instance.questEvents.QuestStateChange(quest);
73
- }
49
+ private void OnDisable()
50
+ {
51
+ GameEventsManager.instance.questEvents.onStartQuest -= StartQuest;
52
+ GameEventsManager.instance.questEvents.onAdvanceQuest -= AdvanceQuest;
53
+ GameEventsManager.instance.questEvents.onFinishQuest -= FinishQuest;
74
54
 
75
- private void PlayerLevelChange(int level)
76
- {
77
- currentPlayerLevel = level;
78
- }
55
+ GameEventsManager.instance.questEvents.onQuestStepStateChange -= QuestStepStateChange;
79
56
 
80
- private bool CheckRequirementsMet(Quest quest)
81
- {
82
- // check player level requirements
83
- var meetsRequirements = !(currentPlayerLevel < quest.info.levelRequirement);
57
+ GameEventsManager.instance.playerEvents.onPlayerLevelChange -= PlayerLevelChange;
58
+ }
84
59
 
85
- // check quest prerequisites for completion
86
- foreach (QuestInfoSO prerequisiteQuestInfo in quest.info.questPrerequisites)
60
+ private void Start()
87
61
  {
88
- if (GetQuestById(prerequisiteQuestInfo.id).state != QuestState.FINISHED)
62
+
63
+ foreach (Quest quest in questMap.Values)
89
64
  {
90
- meetsRequirements = false;
65
+ // initialize any loaded quest steps
66
+ if (quest.state == QuestState.IN_PROGRESS)
67
+ {
68
+ quest.InstantiateCurrentQuestStep(this.transform);
69
+ }
70
+
71
+ // broadcast the initial state of all quests on startup
72
+ GameEventsManager.instance.questEvents.QuestStateChange(quest);
91
73
  }
92
74
  }
93
75
 
94
- return meetsRequirements;
95
- }
76
+ private void ChangeQuestState(string id, QuestState state)
77
+ {
78
+ Quest quest = GetQuestById(id);
79
+ quest.state = state;
80
+ GameEventsManager.instance.questEvents.QuestStateChange(quest);
81
+ }
96
82
 
97
- private void Update()
98
- {
99
- // loop through ALL quests
100
- foreach (Quest quest in questMap.Values)
83
+ private void PlayerLevelChange(int level)
101
84
  {
102
- // if we're now meeting the requirements, switch over to the CAN_START state
103
- if (quest.state == QuestState.REQUIREMENTS_NOT_MET && CheckRequirementsMet(quest))
104
- {
105
- ChangeQuestState(quest.info.id, QuestState.CAN_START);
106
- }
85
+ currentPlayerLevel = level;
107
86
  }
108
- }
109
87
 
110
- private void StartQuest(string id)
111
- {
112
- Quest quest = GetQuestById(id);
113
- quest.InstantiateCurrentQuestStep(this.transform);
114
- ChangeQuestState(quest.info.id, QuestState.IN_PROGRESS);
115
- SaveQuest(quest);
116
- }
88
+ private bool CheckRequirementsMet(Quest quest)
89
+ {
90
+ // check player level requirements
91
+ var meetsRequirements = !(currentPlayerLevel < quest.info.levelRequirement);
117
92
 
118
- private void AdvanceQuest(string id)
119
- {
120
- Quest quest = GetQuestById(id);
93
+ // check quest prerequisites for completion
94
+ foreach (QuestInfoSO prerequisiteQuestInfo in quest.info.questPrerequisites)
95
+ {
96
+ if (GetQuestById(prerequisiteQuestInfo.id).state != QuestState.FINISHED)
97
+ {
98
+ meetsRequirements = false;
99
+ }
100
+ }
121
101
 
122
- // move on to the next step
123
- quest.MoveToNextStep();
124
- if(isDebug) Debug.Log($"[{quest.info.id}]quest state: {quest.state} - {quest.currentStep} over {quest.info.questStepPrefabs.Length} steps done", this);
125
- questStatusUpdateChannel.RaiseEvent($"[{quest.info.id}]quest state: {quest.state} - {quest.currentStep} over {quest.info.questStepPrefabs.Length} steps done");
126
- var progress = (float) quest.currentStep / quest.info.questStepPrefabs.Length;
127
- if(isDebug) Debug.Log($"[{quest.info.id}] progress: {progress*100}%", this);
128
- questProgress.RaiseEvent(quest.info.id, progress);
102
+ return meetsRequirements;
103
+ }
129
104
 
130
- // if there are more steps, instantiate the next one
131
- if (quest.CurrentStepExists())
105
+ private void Update()
132
106
  {
133
- quest.InstantiateCurrentQuestStep(this.transform);
107
+ // loop through ALL quests
108
+ foreach (Quest quest in questMap.Values)
109
+ {
110
+ // if we're now meeting the requirements, switch over to the CAN_START state
111
+ if (quest.state == QuestState.REQUIREMENTS_NOT_MET && CheckRequirementsMet(quest))
112
+ {
113
+ ChangeQuestState(quest.info.id, QuestState.CAN_START);
114
+ }
115
+ }
134
116
  }
135
- // if there are no more steps, then we've finished all of them for this quest
136
- else
117
+
118
+ private void StartQuest(string id)
137
119
  {
138
- ChangeQuestState(quest.info.id, QuestState.CAN_FINISH);
120
+ Quest quest = GetQuestById(id);
121
+ quest.InstantiateCurrentQuestStep(this.transform);
122
+ ChangeQuestState(quest.info.id, QuestState.IN_PROGRESS);
123
+ SaveQuest(quest);
139
124
  }
140
- SaveQuest(quest);
141
- }
142
125
 
143
- private void FinishQuest(string id)
144
- {
145
- Quest quest = GetQuestById(id);
146
- ClaimRewards(quest);
147
- ChangeQuestState(quest.info.id, QuestState.FINISHED);
148
- questStatusUpdateChannel.RaiseEvent($"[{quest.info.id}] quest is finished.");
149
- questProgress.RaiseEvent(quest.info.id,1);
150
- SaveQuest(quest);
151
- }
152
-
153
- private void ClaimRewards(Quest quest)
154
- {
155
- GameEventsManager.instance.scenarioEvents.ScenarioUnlocked(quest.info.unlockedScenario);
156
- }
126
+ private void AdvanceQuest(string id)
127
+ {
128
+ Quest quest = GetQuestById(id);
157
129
 
158
- private void QuestStepStateChange(string id, int stepIndex, QuestStepState questStepState)
159
- {
160
- Quest quest = GetQuestById(id);
161
- quest.StoreQuestStepState(questStepState, stepIndex);
162
- ChangeQuestState(id, quest.state);
163
- }
130
+ // move on to the next step
131
+ quest.MoveToNextStep();
132
+ if (isDebug)
133
+ Debug.Log(
134
+ $"[{quest.info.id}]quest state: {quest.state} - {quest.currentStep} over {quest.info.questStepPrefabs.Length} steps done",
135
+ this);
136
+ questStatusUpdateChannel.RaiseEvent(
137
+ $"[{quest.info.id}]quest state: {quest.state} - {quest.currentStep} over {quest.info.questStepPrefabs.Length} steps done");
138
+ var progress = (float)quest.currentStep / quest.info.questStepPrefabs.Length;
139
+ if (isDebug) Debug.Log($"[{quest.info.id}] progress: {progress * 100}%", this);
140
+ questProgress.RaiseEvent(quest.info.id, progress);
164
141
 
165
- private Dictionary<string, Quest> CreateQuestMap()
166
- {
167
- // loads all QuestInfoSO Scriptable Objects under the Assets/Resources/Quests folder
168
- QuestInfoSO[] allQuests = Resources.LoadAll<QuestInfoSO>("Quests");
169
- // Create the quest map
170
- Dictionary<string, Quest> idToQuestMap = new Dictionary<string, Quest>();
171
- foreach (QuestInfoSO questInfo in allQuests)
172
- {
173
- if (idToQuestMap.ContainsKey(questInfo.id))
142
+ // if there are more steps, instantiate the next one
143
+ if (quest.CurrentStepExists())
174
144
  {
175
- Debug.LogWarning("Duplicate ID found when creating quest map: " + questInfo.id);
145
+ quest.InstantiateCurrentQuestStep(this.transform);
176
146
  }
177
- idToQuestMap.Add(questInfo.id, LoadQuest(questInfo));
147
+ // if there are no more steps, then we've finished all of them for this quest
148
+ else
149
+ {
150
+ ChangeQuestState(quest.info.id, QuestState.CAN_FINISH);
151
+ }
152
+
153
+ SaveQuest(quest);
178
154
  }
179
- return idToQuestMap;
180
- }
181
155
 
182
- private Quest GetQuestById(string id)
183
- {
184
- Quest quest = questMap[id];
185
- if (quest == null)
156
+ private void FinishQuest(string id)
186
157
  {
187
- Debug.LogError("ID not found in the Quest Map: " + id);
158
+ Quest quest = GetQuestById(id);
159
+ ClaimRewards(quest);
160
+ ChangeQuestState(quest.info.id, QuestState.FINISHED);
161
+ questStatusUpdateChannel.RaiseEvent($"[{quest.info.id}] quest is finished.");
162
+ questProgress.RaiseEvent(quest.info.id, 1);
163
+ SaveQuest(quest);
188
164
  }
189
- return quest;
190
- }
191
165
 
192
- private void OnApplicationQuit()
193
- {
194
- foreach (Quest quest in questMap.Values)
166
+ private void ClaimRewards(Quest quest)
195
167
  {
196
- SaveQuest(quest);
168
+ GameEventsManager.instance.scenarioEvents.ScenarioUnlocked(quest.info.unlockedScenario);
197
169
  }
198
- }
199
170
 
200
- private void SaveQuest(Quest quest)
201
- {
202
- try
171
+ private void QuestStepStateChange(string id, int stepIndex, QuestStepState questStepState)
203
172
  {
204
- QuestData questData = quest.GetQuestData();
205
- // serialize using JsonUtility, but use whatever you want here (like JSON.NET)
206
- string serializedData = JsonUtility.ToJson(questData);
207
- Debug.Log($"saved data {serializedData}");
208
- // saving to PlayerPrefs is just a quick example for this tutorial video,
209
- // you probably don't want to save this info there long-term.
210
- // instead, use an actual Save & Load system and write to a file, the cloud, etc..
211
- PlayerPrefs.SetString(quest.info.id, serializedData);
173
+ Quest quest = GetQuestById(id);
174
+ quest.StoreQuestStepState(questStepState, stepIndex);
175
+ ChangeQuestState(id, quest.state);
212
176
  }
213
- catch (System.Exception e)
177
+
178
+ private Dictionary<string, Quest> CreateQuestMap()
214
179
  {
215
- Debug.LogError("Failed to save quest with id " + quest.info.id + ": " + e);
180
+ // loads all QuestInfoSO Scriptable Objects under the Assets/Resources/Quests folder
181
+ QuestInfoSO[] allQuests = Resources.LoadAll<QuestInfoSO>("Quests");
182
+ // Create the quest map
183
+ Dictionary<string, Quest> idToQuestMap = new Dictionary<string, Quest>();
184
+ foreach (QuestInfoSO questInfo in allQuests)
185
+ {
186
+ if (idToQuestMap.ContainsKey(questInfo.id))
187
+ {
188
+ Debug.LogWarning("Duplicate ID found when creating quest map: " + questInfo.id);
189
+ }
190
+
191
+ idToQuestMap.Add(questInfo.id, LoadQuest(questInfo));
192
+ }
193
+
194
+ return idToQuestMap;
216
195
  }
217
- }
218
196
 
219
- private Quest LoadQuest(QuestInfoSO questInfo)
220
- {
221
- Quest quest = null;
222
- try
197
+ private Quest GetQuestById(string id)
198
+ {
199
+ Quest quest = questMap[id];
200
+ if (quest == null)
201
+ {
202
+ Debug.LogError("ID not found in the Quest Map: " + id);
203
+ }
204
+
205
+ return quest;
206
+ }
207
+
208
+ private void OnApplicationQuit()
209
+ {
210
+ foreach (Quest quest in questMap.Values)
211
+ {
212
+ SaveQuest(quest);
213
+ }
214
+ }
215
+
216
+ private void SaveQuest(Quest quest)
223
217
  {
224
- // load quest from saved data
225
- if (PlayerPrefs.HasKey(questInfo.id) && loadSavedQuestState)
218
+ try
226
219
  {
227
- string serializedData = PlayerPrefs.GetString(questInfo.id);
228
- QuestData questData = JsonUtility.FromJson<QuestData>(serializedData);
229
- quest = new Quest(questInfo, questData.state, questData.questStepIndex, questData.questStepStates);
220
+ QuestData questData = quest.GetQuestData();
221
+ // serialize using JsonUtility, but use whatever you want here (like JSON.NET)
222
+ string serializedData = JsonUtility.ToJson(questData);
223
+ Debug.Log($"saved data {serializedData}");
224
+ // saving to PlayerPrefs is just a quick example for this tutorial video,
225
+ // you probably don't want to save this info there long-term.
226
+ // instead, use an actual Save & Load system and write to a file, the cloud, etc..
227
+ PlayerPrefs.SetString(quest.info.id, serializedData);
230
228
  }
231
- // otherwise, initialize a new quest
232
- else
229
+ catch (System.Exception e)
233
230
  {
234
- quest = new Quest(questInfo);
231
+ Debug.LogError("Failed to save quest with id " + quest.info.id + ": " + e);
235
232
  }
236
233
  }
237
- catch (System.Exception e)
234
+
235
+ private Quest LoadQuest(QuestInfoSO questInfo)
238
236
  {
239
- Debug.LogError("Failed to load quest with id " + quest.info.id + ": " + e);
237
+ Quest quest = null;
238
+ try
239
+ {
240
+ // load quest from saved data
241
+ if (PlayerPrefs.HasKey(questInfo.id) && loadSavedQuestState)
242
+ {
243
+ string serializedData = PlayerPrefs.GetString(questInfo.id);
244
+ QuestData questData = JsonUtility.FromJson<QuestData>(serializedData);
245
+ quest = new Quest(questInfo, questData.state, questData.questStepIndex, questData.questStepStates);
246
+ }
247
+ // otherwise, initialize a new quest
248
+ else
249
+ {
250
+ quest = new Quest(questInfo);
251
+ }
252
+ }
253
+ catch (System.Exception e)
254
+ {
255
+ Debug.LogError("Failed to load quest with id " + quest.info.id + ": " + e);
256
+ }
257
+
258
+ return quest;
240
259
  }
241
- return quest;
242
260
  }
243
- }
261
+ }
@@ -2,11 +2,14 @@ using System.Collections;
2
2
  using System.Collections.Generic;
3
3
  using UnityEngine;
4
4
 
5
- public enum QuestState
5
+ namespace jeanf.questsystem
6
6
  {
7
- REQUIREMENTS_NOT_MET,
8
- CAN_START,
9
- IN_PROGRESS,
10
- CAN_FINISH,
11
- FINISHED
12
- }
7
+ public enum QuestState
8
+ {
9
+ REQUIREMENTS_NOT_MET,
10
+ CAN_START,
11
+ IN_PROGRESS,
12
+ CAN_FINISH,
13
+ FINISHED
14
+ }
15
+ }
@@ -1,38 +1,48 @@
1
1
  using System.Collections;
2
2
  using System.Collections.Generic;
3
+ using jeanf.EventSystem;
4
+ using jeanf.propertyDrawer ;
3
5
  using UnityEngine;
6
+ using jeanf.propertyDrawer;
4
7
 
5
- public abstract class QuestStep : MonoBehaviour
8
+ namespace jeanf.questsystem
6
9
  {
7
- private bool isFinished = false;
8
- private string questId;
9
- private int stepIndex;
10
- private float questStepProgress = 0;
11
-
12
- public void InitializeQuestStep(string questId, int stepIndex, string questStepState)
10
+ public abstract class QuestStep : MonoBehaviour
13
11
  {
14
- this.questId = questId;
15
- this.stepIndex = stepIndex;
16
- if (questStepState != null && questStepState != "")
12
+ private bool isFinished = false;
13
+ private string questId;
14
+ private int stepIndex;
15
+ private float questStepProgress = 0;
16
+
17
+ [SerializeField] private bool sendEventOnInit = false;
18
+ //[DrawIf("sendEventOnInit", true, ComparisonType.Equals)]
19
+
20
+ public void InitializeQuestStep(string questId, int stepIndex, string questStepState)
17
21
  {
18
- SetQuestStepState(questStepState);
22
+ this.questId = questId;
23
+ this.stepIndex = stepIndex;
24
+ if (questStepState != null && questStepState != "")
25
+ {
26
+ SetQuestStepState(questStepState);
27
+ }
19
28
  }
20
- }
21
29
 
22
- protected void FinishQuestStep()
23
- {
24
- if (!isFinished)
30
+ protected void FinishQuestStep()
25
31
  {
26
- isFinished = true;
27
- GameEventsManager.instance.questEvents.AdvanceQuest(questId);
28
- Destroy(this.gameObject);
32
+ if (!isFinished)
33
+ {
34
+ isFinished = true;
35
+ GameEventsManager.instance.questEvents.AdvanceQuest(questId);
36
+ Destroy(this.gameObject);
37
+ }
29
38
  }
30
- }
31
39
 
32
- protected void ChangeState(string newState)
33
- {
34
- GameEventsManager.instance.questEvents.QuestStepStateChange(questId, stepIndex, new QuestStepState(newState));
35
- }
40
+ protected void ChangeState(string newState)
41
+ {
42
+ GameEventsManager.instance.questEvents.QuestStepStateChange(questId, stepIndex,
43
+ new QuestStepState(newState));
44
+ }
36
45
 
37
- protected abstract void SetQuestStepState(string state);
38
- }
46
+ protected abstract void SetQuestStepState(string state);
47
+ }
48
+ }
@@ -2,18 +2,21 @@ using System.Collections;
2
2
  using System.Collections.Generic;
3
3
  using UnityEngine;
4
4
 
5
- [System.Serializable]
6
- public class QuestStepState
5
+ namespace jeanf.questsystem
7
6
  {
8
- public string state;
9
-
10
- public QuestStepState(string state)
7
+ [System.Serializable]
8
+ public class QuestStepState
11
9
  {
12
- this.state = state;
13
- }
10
+ public string state;
14
11
 
15
- public QuestStepState()
16
- {
17
- this.state = "";
12
+ public QuestStepState(string state)
13
+ {
14
+ this.state = state;
15
+ }
16
+
17
+ public QuestStepState()
18
+ {
19
+ this.state = "";
20
+ }
18
21
  }
19
- }
22
+ }
@@ -1,29 +1,33 @@
1
1
  using System;
2
2
  using UnityEngine;
3
3
 
4
- public class GameEventsManager : MonoBehaviour
4
+ namespace jeanf.questsystem
5
5
  {
6
- public static GameEventsManager instance { get; private set; }
6
+ public class GameEventsManager : MonoBehaviour
7
+ {
8
+ public static GameEventsManager instance { get; private set; }
7
9
 
8
- public InputEvents inputEvents;
9
- public PlayerEvents playerEvents;
10
- public ScenarioEvents scenarioEvents;
11
- public MiscEvents miscEvents;
12
- public QuestEvents questEvents;
10
+ public InputEvents inputEvents;
11
+ public PlayerEvents playerEvents;
12
+ public ScenarioEvents scenarioEvents;
13
+ public MiscEvents miscEvents;
14
+ public QuestEvents questEvents;
13
15
 
14
- private void Awake()
15
- {
16
- if (instance != null)
16
+ private void Awake()
17
17
  {
18
- Debug.LogError("Found more than one Game Events Manager in the scene.");
19
- }
20
- instance = this;
18
+ if (instance != null)
19
+ {
20
+ Debug.LogError("Found more than one Game Events Manager in the scene.");
21
+ }
21
22
 
22
- // initialize all events
23
- inputEvents = new InputEvents();
24
- playerEvents = new PlayerEvents();
25
- scenarioEvents = new ScenarioEvents();
26
- miscEvents = new MiscEvents();
27
- questEvents = new QuestEvents();
23
+ instance = this;
24
+
25
+ // initialize all events
26
+ inputEvents = new InputEvents();
27
+ playerEvents = new PlayerEvents();
28
+ scenarioEvents = new ScenarioEvents();
29
+ miscEvents = new MiscEvents();
30
+ questEvents = new QuestEvents();
31
+ }
28
32
  }
29
33
  }