Skip to content

Commit 72f986e

Browse files
committed
Update ObjectiveController with events
1 parent 56528ed commit 72f986e

File tree

1 file changed

+111
-89
lines changed

1 file changed

+111
-89
lines changed
+111-89
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using UnityEngine;
33
using System.Collections.Generic;
4-
using System.Collections;
5-
using UnityEngine.SceneManagement;
64

75
public class ObjectiveController : MonoBehaviour
86
{
7+
protected EventListeners<ObjectiveController> eventListeners;
8+
99
public event Action OnRequestNewRecipe;
1010
public event Action OnRequestPoisonRecipe;
1111
public event Action OnRequestNewCategories;
@@ -26,9 +26,16 @@ public class ObjectiveController : MonoBehaviour
2626
private bool poisonProvided;
2727
private bool firstValidIngredientFound;
2828

29+
30+
private void Awake()
31+
{
32+
eventListeners = new EventListeners<ObjectiveController>(this);
33+
}
34+
2935
private void Start()
3036
{
31-
StartCoroutine(GameLoop());
37+
eventListeners.RegisterEvent(new Objective1Event());
38+
//StartCoroutine(GameLoop());
3239
}
3340

3441
public void SetCurrentRecipe(RecipeData recipe)
@@ -72,98 +79,113 @@ public void NotifyIngredientCollected(ItemData item)
7279
if (firstValidIngredientFound) return;
7380

7481
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);
14883
}
14984

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()
151179
{
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;
154183
}
155184

156-
private IEnumerator EarlyGameOver()
185+
public class Objective1Event : IEvent<ObjectiveController>
157186
{
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";
162188

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;
168190
}
169191
}

0 commit comments

Comments
 (0)