-
Notifications
You must be signed in to change notification settings - Fork 1
/
OnboradingFlow.cs
314 lines (267 loc) · 17.2 KB
/
OnboradingFlow.cs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using static Utility.ClassManager;
using static Utility.TilemapManager;
public class OnboradingFlow : MonoBehaviour {
//vscode is wrong, these fields cannot be made readonly since then they cant be serialized in editor
[SerializeField] private GameObject tutorialText;
[SerializeField] private GameObject interactionLegend;
[SerializeField] private GameObject skipButtonPrefab;
[SerializeField] private GameObject startTutorialDialog;
private readonly Dictionary<BuildingType, bool> buildingsPlaced = new(){
{BuildingType.Greenhouse, false},
{BuildingType.ShippingBin, false},
{BuildingType.House, false},
{BuildingType.PetBowl, false}
};
public bool IsInTutorial { get; private set; }
void Start() {
// PlayerPrefs.SetInt("HasDoneIntro", 0);
if (PlayerPrefs.GetInt("HasDoneIntro") == 0) {
GameObject dialog = Instantiate(startTutorialDialog, GameObject.FindGameObjectWithTag("Canvas").transform);
dialog.transform.Find("Buttons").Find("Yes").GetComponent<Button>().onClick.AddListener(() => {
StartOnboardingFlow();
Destroy(dialog);
});
dialog.transform.Find("Buttons").Find("No").GetComponent<Button>().onClick.AddListener(() => {
Destroy(dialog);
});
}
IsInTutorial = false;
}
public void StartOnboardingFlow() {
IEnumerator OnboardingFlow() {
BuildingController.DeleteAllBuildings(true);
float cameraMoveTime = 2f;
float delay = 0.25f;
CameraController.Instance.SetSizeSmooth(10, delay);
yield return new WaitForSecondsRealtime(delay);
CameraController.Instance.ToggleCameraLock();
CameraController.Instance.enforceBounds = false;
CameraController.Instance.SetPositionSmooth(GetGridTilemap().CellToWorld(MapController.Instance.GetGreenhousePosition() + new Vector3Int(3, 3, 0)), cameraMoveTime);
yield return new WaitForSecondsRealtime(cameraMoveTime);
BuildingController.PlaceGreenhouse();
buildingsPlaced[BuildingType.Greenhouse] = true;
yield return new WaitForSecondsRealtime(delay);
CameraController.Instance.SetPositionSmooth(GetGridTilemap().CellToWorld(MapController.Instance.GetShippingBinPosition() + new Vector3Int(1, 0, 0)), cameraMoveTime);
yield return new WaitForSecondsRealtime(cameraMoveTime);
BuildingController.PlaceBin();
buildingsPlaced[BuildingType.ShippingBin] = true;
yield return new WaitForSecondsRealtime(delay);
CameraController.Instance.SetPositionSmooth(GetGridTilemap().CellToWorld(MapController.Instance.GetHousePosition() + new Vector3Int(4, 5, 0)), cameraMoveTime);
yield return new WaitForSecondsRealtime(cameraMoveTime);
BuildingController.PlaceHouse();
buildingsPlaced[BuildingType.House] = true;
yield return new WaitForSecondsRealtime(delay);
CameraController.Instance.SetPositionSmooth(GetGridTilemap().CellToWorld(MapController.Instance.GetPetBowlPosition()), cameraMoveTime);
yield return new WaitForSecondsRealtime(cameraMoveTime);
BuildingController.PlacePetBowl();
buildingsPlaced[BuildingType.PetBowl] = true;
yield return new WaitForSecondsRealtime(delay);
CameraController.Instance.SetSizeSmooth(15, cameraMoveTime);
CameraController.Instance.SetPositionSmooth(new Vector3(45, 45, 0), cameraMoveTime);
yield return new WaitForSecondsRealtime(cameraMoveTime);
CameraController.Instance.enforceBounds = true;
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.GetComponentInChildren<TMP_Text>().text = "When creating a new farm, some buildings are placed automatically. Default buildings depend on the farm type.";
text.GetComponentInChildren<Button>().onClick.AddListener(() => {
Destroy(text);
ShowHowToMoveCamera();
});
CameraController.Instance.ToggleCameraLock();
}
MoveablePanel.CloseAllPanels?.Invoke();
IsInTutorial = true;
BuildingController.SetCurrentAction(Actions.DO_NOTHING);
InputHandler.Instance.keybindsShouldRegister = false;
if (DebugCoordinates.DebugModeinOn) DebugCoordinates.ToggleDebugMode();
GameObject skipButton = Instantiate(skipButtonPrefab, GameObject.FindGameObjectWithTag("Canvas").transform);
skipButton.GetComponent<Button>().onClick.AddListener(() => {
EndOnboardingFlow();
Destroy(skipButton);
});
MapController.Instance.SetMapAsync(MapController.MapTypes.Normal);
GetCanvasGameObject().transform.Find("TopRightButtons").gameObject.SetActive(false);
GetCanvasGameObject().transform.Find("NoBuilding").gameObject.SetActive(false);
GetCanvasGameObject().transform.Find("ToggleBuildingSelectButton").gameObject.SetActive(false);
StartCoroutine(OnboardingFlow());
}
public void ShowHowToMoveCamera() {
IEnumerator CheckCameraMoved() {
Vector3 cameraPosition = CameraController.Instance.GetPosition();
float cameraZoom = CameraController.Instance.GetZoom();
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.GetComponentInChildren<TMP_Text>().text = "Move the camera by holding middle mouse button and moving the mouse.\nZoom in and out by scrolling.\nTry both out!";
text.transform.GetChild(1).gameObject.GetComponent<Button>().interactable = false;
bool movedCamera = false, changedZoom = false;
while (true) {
if (Vector3.Distance(cameraPosition, CameraController.Instance.GetPosition()) >= 10) movedCamera = true;
if (Mathf.Abs(cameraZoom - CameraController.Instance.GetZoom()) >= 3) changedZoom = true;
if (movedCamera && changedZoom) break;
yield return null;
}
text.transform.GetChild(1).gameObject.GetComponent<Button>().interactable = true;
text.GetComponentInChildren<Button>().onClick.AddListener(() => {
Destroy(text);
ShowHowToPlaceBuilding();
});
}
StartCoroutine(CheckCameraMoved());
}
public void ShowHowToPlaceBuilding() {
GetCanvasGameObject().transform.Find("ToggleBuildingSelectButton").gameObject.SetActive(true);
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.GetComponentInChildren<TMP_Text>().text = "To select a building to place press the button on the top left corner. Try placing a building!";
text.transform.GetChild(1).gameObject.SetActive(false);
void onBuildingSelectOpen() {
Destroy(text);
GetCanvasGameObject().transform.Find("ToggleBuildingSelectButton").GetComponent<Button>().onClick.RemoveListener(onBuildingSelectOpen);
}
GetCanvasGameObject().transform.Find("ToggleBuildingSelectButton").GetComponent<Button>().onClick.AddListener(onBuildingSelectOpen);
BuildingController.anyBuildingPositionChanged += ShowActions;
}
public void ShowActions() {
BuildingController.anyBuildingPositionChanged -= ShowActions;
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
GameObject topRightButtons = GetCanvasGameObject().transform.Find("TopRightButtons").gameObject;
topRightButtons.SetActive(true);
topRightButtons.transform.Find("settingsButton").gameObject.SetActive(false);
topRightButtons.transform.Find("ShowTotalMaterials").gameObject.SetActive(false);
text.GetComponentInChildren<TMP_Text>().text = "Click the top right arrow to open the actions menu.\nFrom there you can select different actions to perform.";
text.transform.GetChild(1).gameObject.SetActive(false);
void onActionButtonClick() {
Destroy(text);
ShowWhatActionsDo();
topRightButtons.transform.Find("ActionButtons").Find("CloseMenuButton").GetComponent<Button>().onClick.RemoveListener(onActionButtonClick);
}
GetCanvasGameObject().transform.Find("NoBuilding").gameObject.SetActive(true);
topRightButtons.transform.Find("ActionButtons").Find("CloseMenuButton").GetComponent<Button>().onClick.AddListener(onActionButtonClick);
}
public void ShowWhatActionsDo() {
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.GetComponentInChildren<TMP_Text>().text = "The Actions you can do are:\n-Place\n-Pick Up\n-Delete\n-Copy\nTry deleting the building you just placed!";
text.transform.GetChild(1).gameObject.SetActive(false);
foreach (Transform actionButton in GetCanvasGameObject().transform.Find("TopRightButtons").transform.Find("ActionButtons")) {
if (actionButton.name != "DeleteButton") actionButton.GetComponent<Button>().interactable = false;
}
void onBuildingDeleted() {
foreach (Transform actionButton in GetCanvasGameObject().transform.Find("TopRightButtons").transform.Find("ActionButtons")) {
actionButton.GetComponent<Button>().interactable = true;
}
Destroy(text);
BuildingController.anyBuildingPositionChanged -= onBuildingDeleted;
ShowHowToInteractWithBuildings();
}
BuildingController.anyBuildingPositionChanged += onBuildingDeleted;
}
public void ShowHowToInteractWithBuildings() {
BuildingController.anyBuildingPositionChanged -= ShowHowToInteractWithBuildings;
CameraController.Instance.SetPositionSmooth(GetGridTilemap().CellToWorld(MapController.Instance.GetHousePosition() + new Vector3Int(4, 1, 0)), 1f);
CameraController.Instance.ToggleCameraLock();
CameraController.Instance.SetSize(7);
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.transform.GetChild(1).gameObject.SetActive(false);
text.GetComponentInChildren<TMP_Text>().text = "Some buildings can be interacted with.\nRight Click on the House to see what you can do with it!";
void onBuildingRightClick() {
Destroy(text);
InteractableBuildingComponent.BuildingWasRightClicked -= onBuildingRightClick;
ShowWhatInteractionsDo();
}
InteractableBuildingComponent.BuildingWasRightClicked += onBuildingRightClick;
}
public void ShowWhatInteractionsDo() {
GameObject legend = Instantiate(interactionLegend, GameObject.FindGameObjectWithTag("Canvas").transform);
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.GetComponentInChildren<TMP_Text>().text = "These are the all available interactions, different buildings have different interactions. Try entering the house!";
text.transform.GetChild(1).gameObject.SetActive(false);
Building house = BuildingController.GetBuildings().First(building => building.Base == MapController.Instance.GetHousePosition());
InteractableBuildingComponent interactableBuildingComponent = house.GetComponent<InteractableBuildingComponent>();
foreach (Transform interactionButton in interactableBuildingComponent.ButtonParentGameObject.transform) {
if (interactionButton.name == "ENTER") continue;
interactionButton.GetComponent<Button>().interactable = false;
}
void onBuildingInteraction() {
Destroy(text);
Destroy(legend);
foreach (Transform interactionButton in interactableBuildingComponent.ButtonParentGameObject.transform) {
interactionButton.GetComponent<Button>().interactable = true;
}
CameraController.Instance.ToggleCameraLock();
BuildingButtonController.anyActionWasClicked -= onBuildingInteraction;
ShowInteriorInteractions();
}
BuildingButtonController.anyActionWasClicked += onBuildingInteraction;
}
public void ShowInteriorInteractions() {
GameObject legend = Instantiate(interactionLegend, GameObject.FindGameObjectWithTag("Canvas").transform);
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.GetComponentInChildren<TMP_Text>().text = "Some building interactions can be performed while inside the building. You can see them on the top left corner of your screen. Try exiting the house!";
text.transform.GetChild(1).gameObject.SetActive(false);
Building house = BuildingController.GetBuildings().First(building => building.Base == MapController.Instance.GetHousePosition());
EnterableBuildingComponent enterableBuildingComponent = house.GetComponent<EnterableBuildingComponent>();
foreach (GameObject interactionButton in enterableBuildingComponent.interiorButtons) {
if (interactionButton.name == "ENTER") continue;
interactionButton.GetComponent<Button>().interactable = false;
}
void onHouseExit() {
Destroy(text);
BuildingController.isInsideBuilding.Value.interiorButtonClicked -= (type) => { if (type == ButtonTypes.ENTER) onHouseExit(); };
foreach (GameObject interactionButton in enterableBuildingComponent.interiorButtons) {
interactionButton.GetComponent<Button>().interactable = true;
}
ShowSettingsAndTotalMaterialCost();
}
BuildingController.isInsideBuilding.Value.interiorButtonClicked += (type) => { if (type == ButtonTypes.ENTER) onHouseExit(); };
}
public void ShowSettingsAndTotalMaterialCost() {
GameObject topRightButtons = GetCanvasGameObject().transform.Find("TopRightButtons").gameObject;
topRightButtons.transform.Find("settingsButton").gameObject.SetActive(true);
topRightButtons.transform.Find("ShowTotalMaterials").gameObject.SetActive(true);
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.GetComponentInChildren<TMP_Text>().text = "Lastly you can open the settings or see the total cost of your farm by pressing the buttons on the top right corner.";
text.GetComponentInChildren<Button>().onClick.AddListener(() => {
Destroy(text);
EndOnboardingFlow();
});
}
public void EndOnboardingFlow() {
StopAllCoroutines();
CameraController.Instance.UnlockCamera();
if (!buildingsPlaced[BuildingType.Greenhouse]) BuildingController.PlaceGreenhouse();
if (!buildingsPlaced[BuildingType.ShippingBin]) BuildingController.PlaceBin();
if (!buildingsPlaced[BuildingType.House]) BuildingController.PlaceHouse();
if (!buildingsPlaced[BuildingType.PetBowl]) BuildingController.PlacePetBowl();
GameObject topRightButtons = GetCanvasGameObject().transform.Find("TopRightButtons").gameObject;
topRightButtons.SetActive(true);
foreach (Transform actionButton in GetCanvasGameObject().transform.Find("TopRightButtons").transform.Find("ActionButtons")) {
actionButton.GetComponent<Button>().interactable = true;
}
Building house = BuildingController.GetBuildings().First(building => building.Base == MapController.Instance.GetHousePosition());
InteractableBuildingComponent interactableBuildingComponent = house.GetComponent<InteractableBuildingComponent>();
foreach (Transform interactionButton in interactableBuildingComponent.ButtonParentGameObject.transform) {
interactionButton.GetComponent<Button>().interactable = true;
}
EnterableBuildingComponent enterableBuildingComponent = house.GetComponent<EnterableBuildingComponent>();
foreach (GameObject interactionButton in enterableBuildingComponent.interiorButtons) {
interactionButton.GetComponent<Button>().interactable = false;
}
GetCanvasGameObject().transform.Find("NoBuilding").gameObject.SetActive(true);
PlayerPrefs.SetInt("HasDoneIntro", 1);
PlayerPrefs.Save();
topRightButtons.transform.Find("settingsButton").gameObject.SetActive(true);
topRightButtons.transform.Find("ShowTotalMaterials").gameObject.SetActive(true);
GetCanvasGameObject().transform.Find("ToggleBuildingSelectButton").gameObject.SetActive(true);
InputHandler.Instance.keybindsShouldRegister = true;
GameObject text = Instantiate(tutorialText, GameObject.FindGameObjectWithTag("Canvas").transform);
text.GetComponentInChildren<TMP_Text>().text = "That's all! Start building and experimenting with your farm! You can always redo the tutorial through the settings.";
text.GetComponentInChildren<Button>().onClick.AddListener(() => {
Destroy(text);
});
IsInTutorial = false;
}
}