1
1
using System ;
2
2
using UnityEngine ;
3
3
using System . Collections . Generic ;
4
- using System . Collections ;
5
- using UnityEngine . SceneManagement ;
6
4
7
5
public class ObjectiveController : MonoBehaviour
8
6
{
7
+ protected EventListeners < ObjectiveController > eventListeners ;
8
+
9
9
public event Action OnRequestNewRecipe ;
10
10
public event Action OnRequestPoisonRecipe ;
11
11
public event Action OnRequestNewCategories ;
@@ -26,9 +26,16 @@ public class ObjectiveController : MonoBehaviour
26
26
private bool poisonProvided ;
27
27
private bool firstValidIngredientFound ;
28
28
29
+
30
+ private void Awake ( )
31
+ {
32
+ eventListeners = new EventListeners < ObjectiveController > ( this ) ;
33
+ }
34
+
29
35
private void Start ( )
30
36
{
31
- StartCoroutine ( GameLoop ( ) ) ;
37
+ eventListeners . RegisterEvent ( new Objective1Event ( ) ) ;
38
+ //StartCoroutine(GameLoop());
32
39
}
33
40
34
41
public void SetCurrentRecipe ( RecipeData recipe )
@@ -72,98 +79,113 @@ public void NotifyIngredientCollected(ItemData item)
72
79
if ( firstValidIngredientFound ) return ;
73
80
74
81
firstValidIngredientFound = true ;
75
- OnRequestFirstIngredientDialog ? . Invoke ( level - 1 ) ;
76
- }
77
-
78
- private IEnumerator GameLoop ( )
79
- {
80
- Debug . Log ( "[GameLoop]: Requesting poison recipe" ) ;
81
- OnRequestPoisonRecipe ? . Invoke ( ) ;
82
-
83
- while ( true )
84
- {
85
- Debug . Log ( "[GameLoop]: Requesting recipe" ) ;
86
- OnRequestNewRecipe ? . Invoke ( ) ;
87
- OnRequestIntroDialog ? . Invoke ( level - 1 ) ;
88
-
89
- lastRecipeValid = false ;
90
- potionProvided = false ;
91
- poisonProvided = false ;
92
- firstValidIngredientFound = false ;
93
-
94
- yield return new WaitUntil ( ( ) => potionProvided ) ;
95
-
96
- Debug . Log ( "[GameLoop]: Evaluating recipe" ) ;
97
-
98
- // All levels
99
- if ( level < 5 )
100
- {
101
- if ( lastRecipeValid )
102
- {
103
- Debug . Log ( "OnRequestCorrectIngredientDialog Invoke" ) ;
104
- OnRequestCorrectIngredientDialog ? . Invoke ( level - 1 ) ;
105
- yield return SleepCutscene ( ) ;
106
- }
107
-
108
- else
109
- {
110
- OnRequestWrongIngredientDialog ? . Invoke ( level - 1 ) ;
111
- -- lives ;
112
- OnLivesCountChanged ? . Invoke ( lives ) ;
113
- if ( lives < 1 )
114
- {
115
-
116
- StartCoroutine ( EarlyGameOver ( ) ) ;
117
- yield break ;
118
- }
119
- }
120
- }
121
- // Last level
122
- else
123
- {
124
- if ( poisonProvided )
125
- {
126
- StartCoroutine ( PoisonCutscene ( ) ) ;
127
- yield break ;
128
- }
129
-
130
- lives = 0 ;
131
- OnLivesCountChanged ? . Invoke ( lives ) ;
132
-
133
- // You fail even if the recipe is "valid"
134
- StartCoroutine ( GameOver ( ) ) ;
135
- yield break ;
136
- }
137
-
138
- OnRequestNewCategories ? . Invoke ( ) ;
139
- level ++ ;
140
- }
141
- }
142
-
143
- private IEnumerator PoisonCutscene ( )
144
- {
145
- Debug . Log ( "[GameLoop]: You poisoned the witch!" ) ;
146
- SceneManager . LoadScene ( "VictoryScreen" ) ;
147
- yield return null ;
82
+ OnRequestFirstIngredientDialog ? . Invoke ( level - 1 ) ;
148
83
}
149
84
150
- private IEnumerator SleepCutscene ( )
85
+ //private IEnumerator GameLoop()
86
+ //{
87
+ // Debug.Log("[GameLoop]: Requesting poison recipe");
88
+ // OnRequestPoisonRecipe?.Invoke();
89
+
90
+ // while (true)
91
+ // {
92
+ // Debug.Log("[GameLoop]: Requesting recipe");
93
+ // OnRequestNewRecipe?.Invoke();
94
+ // OnRequestIntroDialog?.Invoke(level-1);
95
+
96
+ // lastRecipeValid = false;
97
+ // potionProvided = false;
98
+ // poisonProvided = false;
99
+ // firstValidIngredientFound = false;
100
+
101
+ // yield return new WaitUntil(() => potionProvided);
102
+
103
+ // Debug.Log("[GameLoop]: Evaluating recipe");
104
+
105
+ // // All levels
106
+ // if (level < 5)
107
+ // {
108
+ // if (lastRecipeValid)
109
+ // {
110
+ // Debug.Log("OnRequestCorrectIngredientDialog Invoke");
111
+ // OnRequestCorrectIngredientDialog?.Invoke(level-1);
112
+ // yield return SleepCutscene();
113
+ // }
114
+
115
+ // else
116
+ // {
117
+ // OnRequestWrongIngredientDialog?.Invoke(level-1);
118
+ // --lives;
119
+ // OnLivesCountChanged?.Invoke(lives);
120
+ // if (lives < 1)
121
+ // {
122
+
123
+ // StartCoroutine(EarlyGameOver());
124
+ // yield break;
125
+ // }
126
+ // }
127
+ // }
128
+ // // Last level
129
+ // else
130
+ // {
131
+ // if(poisonProvided)
132
+ // {
133
+ // StartCoroutine(PoisonCutscene());
134
+ // yield break;
135
+ // }
136
+
137
+ // lives = 0;
138
+ // OnLivesCountChanged?.Invoke(lives);
139
+
140
+ // // You fail even if the recipe is "valid"
141
+ // StartCoroutine(GameOver());
142
+ // yield break;
143
+ // }
144
+
145
+ // OnRequestNewCategories?.Invoke();
146
+ // level++;
147
+ // }
148
+ //}
149
+
150
+ //private IEnumerator PoisonCutscene()
151
+ //{
152
+ // Debug.Log("[GameLoop]: You poisoned the witch!");
153
+ // SceneManager.LoadScene("VictoryScreen");
154
+ // yield return null;
155
+ //}
156
+
157
+ //private IEnumerator SleepCutscene()
158
+ //{
159
+ // Debug.Log("[GameLoop]: The witch fell asleep...");
160
+ // yield return null;
161
+ //}
162
+
163
+ //private IEnumerator EarlyGameOver()
164
+ //{
165
+ // Debug.Log("[GameLoop]: The witch got angry and ate you!");
166
+ // SceneManager.LoadScene("DefeatScreen");
167
+ // yield return null;
168
+ //}
169
+
170
+ //private IEnumerator GameOver()
171
+ //{
172
+ // Debug.Log("[GameLoop]: You did well, but the witch still ate you!");
173
+ // SceneManager.LoadScene("DefeatScreen");
174
+ // yield return null;
175
+ //}
176
+
177
+ private bool started ;
178
+ private void Update ( )
151
179
{
152
- Debug . Log ( "[GameLoop]: The witch fell asleep..." ) ;
153
- yield return null ;
180
+ if ( started && ! Input . GetKeyDown ( KeyCode . O ) ) return ;
181
+ eventListeners . RaiseEvent ( new Objective1Event ( ) ) ;
182
+ started = true ;
154
183
}
155
184
156
- private IEnumerator EarlyGameOver ( )
185
+ public class Objective1Event : IEvent < ObjectiveController >
157
186
{
158
- Debug . Log ( "[GameLoop]: The witch got angry and ate you!" ) ;
159
- SceneManager . LoadScene ( "DefeatScreen" ) ;
160
- yield return null ;
161
- }
187
+ public string GetName ( ) => "Objective1Event" ;
162
188
163
- private IEnumerator GameOver ( )
164
- {
165
- Debug . Log ( "[GameLoop]: You did well, but the witch still ate you!" ) ;
166
- SceneManager . LoadScene ( "DefeatScreen" ) ;
167
- yield return null ;
189
+ public dynamic GetPayload ( ) => null ;
168
190
}
169
191
}
0 commit comments