No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

503 líneas
13 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using CIG;
  6. using CIG.Translation;
  7. using CIG3.ExtensionMethods;
  8. using CIGEnums;
  9. using SUISS.Core;
  10. using SUISSEngine;
  11. using UnityEngine;
  12. public sealed class CIGTutorialManager : SingletonMonobehaviour<CIGTutorialManager>
  13. {
  14. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  15. public event CIGTutorialManager.TutorialFinishedEventHandler TutorialFinishedEvent;
  16. private void FireTutorialFinishedEvent()
  17. {
  18. if (this.TutorialFinishedEvent != null)
  19. {
  20. this.TutorialFinishedEvent();
  21. }
  22. }
  23. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  24. public event CIGTutorialManager.HasDeserializedEventHandler HasDeserializedEvent;
  25. private void FireHasDeserializedEvent()
  26. {
  27. if (this.HasDeserializedEvent != null)
  28. {
  29. this.HasDeserializedEvent();
  30. }
  31. }
  32. public CIGTutorialState state
  33. {
  34. get
  35. {
  36. return this._state;
  37. }
  38. private set
  39. {
  40. if (this._state != value)
  41. {
  42. this._state = value;
  43. this._popupShown = false;
  44. this.serializing.Serialize();
  45. this._hudState.reference.CheckTutorialButton();
  46. }
  47. }
  48. }
  49. public bool IsFinished
  50. {
  51. get
  52. {
  53. return this._state > CIGTutorialState.Success;
  54. }
  55. }
  56. public bool HasDeserialized { get; private set; }
  57. protected override void Awake()
  58. {
  59. base.Awake();
  60. this._fsm = UnityEngine.Object.FindObjectOfType<FiniteStateMachine>();
  61. this._gameState = UnityEngine.Object.FindObjectOfType<CIGGameState>();
  62. this._running = false;
  63. }
  64. private void Start()
  65. {
  66. SingletonMonobehaviour<WorldMap>.Instance.VisibilityChangedEvent += this.OnWorldMapVisibilityChanged;
  67. this._tutorialRoutine = this.TutorialRoutine();
  68. }
  69. protected override void OnDestroy()
  70. {
  71. if (SingletonMonobehaviour<WorldMap>.IsAvailable)
  72. {
  73. SingletonMonobehaviour<WorldMap>.Instance.VisibilityChangedEvent -= this.OnWorldMapVisibilityChanged;
  74. }
  75. base.OnDestroy();
  76. }
  77. private void OnSerialize(Dictionary<string, object> values)
  78. {
  79. values["tutorialState"] = (int)this._state;
  80. values["cranePopupShown"] = this._cranePopupShown;
  81. }
  82. private void OnDeserialize(Dictionary<string, object> values)
  83. {
  84. this._state = CIGTutorialState.Welcome;
  85. if (values.ContainsKey("tutorialState"))
  86. {
  87. int num = (int)values["tutorialState"];
  88. if (num >= 0 && num < 10)
  89. {
  90. this._state = (CIGTutorialState)num;
  91. }
  92. }
  93. this._cranePopupShown = values.GetValue("cranePopupShown", false);
  94. }
  95. private void OnDeserialized()
  96. {
  97. this.HasDeserialized = true;
  98. this.FireHasDeserializedEvent();
  99. }
  100. public void QuitTutorial()
  101. {
  102. this._popupShown = true;
  103. this.state = CIGTutorialState.Finished;
  104. }
  105. public bool ShouldOverrideBuildingConstructionTime(CIGBuilding building)
  106. {
  107. return (this._state == CIGTutorialState.BuildHouse && building is CIGResidentialBuilding) || (this._state == CIGTutorialState.BuildFarm && building is CIGCommercialBuilding);
  108. }
  109. private void OnWorldMapVisibilityChanged(bool visible)
  110. {
  111. bool isVisiting = SingletonMonobehaviour<CIGIslandsManager>.Instance.IsVisiting;
  112. if (!visible && !isVisiting && this._state != CIGTutorialState.Finished && !this._running)
  113. {
  114. base.StartCoroutine(this._tutorialRoutine);
  115. }
  116. else if (visible || isVisiting)
  117. {
  118. base.StopCoroutine(this._tutorialRoutine);
  119. }
  120. }
  121. private IEnumerator TutorialRoutine()
  122. {
  123. yield return new WaitForSeconds(5f);
  124. for (;;)
  125. {
  126. this.CheckState();
  127. switch (this.state)
  128. {
  129. case CIGTutorialState.Initial:
  130. this.state = CIGTutorialState.Welcome;
  131. goto IL_23A;
  132. case CIGTutorialState.Welcome:
  133. this.ShowPopup(Localization.Key("tutorial_start"), Localization.Key("continue_message"), false, -1);
  134. goto IL_23A;
  135. case CIGTutorialState.BuildHouse:
  136. this.ShowTutorialDialog(Localization.Key("tutorial_build_house"), 0);
  137. goto IL_23A;
  138. case CIGTutorialState.SpeedupHouse:
  139. this.ShowTutorialDialog(Localization.Key("tutorial_speedup_house"), -1);
  140. goto IL_23A;
  141. case CIGTutorialState.Road:
  142. this.ShowTutorialDialog(Localization.Key("tutorial_build_road"), 2);
  143. goto IL_23A;
  144. case CIGTutorialState.BuildFarm:
  145. foreach (CIGCommercialBuilding cigbuilding in SingletonMonobehaviour<GameObjectManager>.Instance.Commercial)
  146. {
  147. if (!cigbuilding.island.IsValid() || cigbuilding.island == this._cityIsland.reference.island)
  148. {
  149. if (cigbuilding.IsUnlocked && !cigbuilding.activatable)
  150. {
  151. SpriteRenderer component = cigbuilding.GetComponent<SpriteRenderer>();
  152. if (component != null)
  153. {
  154. Sprite sprite = component.sprite;
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. this.ShowTutorialDialog(Localization.Key("tutorial_build_commercial"), 0);
  161. goto IL_23A;
  162. case CIGTutorialState.CityAdvisor:
  163. this.ShowTutorialDialog(Localization.Key("tutorial_city_advisor"), 5);
  164. goto IL_23A;
  165. case CIGTutorialState.DeprecatedSocialPopup:
  166. goto IL_1F5;
  167. case CIGTutorialState.Success:
  168. goto IL_1F5;
  169. case CIGTutorialState.Finished:
  170. goto IL_21B;
  171. }
  172. break;
  173. IL_23A:
  174. yield return new WaitForSeconds(2f);
  175. continue;
  176. IL_1F5:
  177. this.ShowPopup(Localization.Key("tutorial_complete"), Localization.Key("thank_you"), true, -1);
  178. goto IL_23A;
  179. }
  180. UnityEngine.Debug.LogWarning("TutorialRoutine: Tutorial is in invalid state.");
  181. yield break;
  182. IL_21B:
  183. this.FireTutorialFinishedEvent();
  184. yield break;
  185. yield break;
  186. }
  187. private void CheckState()
  188. {
  189. if (!this._popupShown)
  190. {
  191. return;
  192. }
  193. CIGTutorialState state = this.state;
  194. CIGBuilding focusBuilding = this._focusBuilding;
  195. switch (this.state)
  196. {
  197. case CIGTutorialState.Initial:
  198. this.state = CIGTutorialState.Welcome;
  199. goto IL_158;
  200. case CIGTutorialState.Welcome:
  201. this.state = CIGTutorialState.BuildHouse;
  202. break;
  203. case CIGTutorialState.BuildHouse:
  204. break;
  205. case CIGTutorialState.SpeedupHouse:
  206. goto IL_A1;
  207. case CIGTutorialState.Road:
  208. goto IL_AD;
  209. case CIGTutorialState.BuildFarm:
  210. goto IL_D3;
  211. case CIGTutorialState.CityAdvisor:
  212. goto IL_106;
  213. case CIGTutorialState.DeprecatedSocialPopup:
  214. goto IL_132;
  215. case CIGTutorialState.Success:
  216. goto IL_132;
  217. case CIGTutorialState.Finished:
  218. goto IL_13F;
  219. default:
  220. UnityEngine.Debug.LogWarning("CheckState: Tutorial is in invalid state.");
  221. goto IL_158;
  222. }
  223. CIGBuilding cigbuilding = this.BuildingOfTypeBuilt<CIGResidentialBuilding>(false);
  224. if (cigbuilding == null)
  225. {
  226. goto IL_158;
  227. }
  228. this.state = CIGTutorialState.SpeedupHouse;
  229. IL_A1:
  230. this.state = CIGTutorialState.Road;
  231. IL_AD:
  232. CIGBuilding x = this.BuildingOfTypeHasRoad<CIGResidentialBuilding>();
  233. if (x == null)
  234. {
  235. goto IL_158;
  236. }
  237. this.state = CIGTutorialState.BuildFarm;
  238. IL_D3:
  239. CIGBuilding cigbuilding2 = this.BuildingOfTypeBuilt<CIGCommercialBuilding>(false);
  240. if (cigbuilding2 == null)
  241. {
  242. goto IL_158;
  243. }
  244. this.state = CIGTutorialState.CityAdvisor;
  245. IL_106:
  246. if (!this._gameState.GetValue<bool>("cityAdvisorOpened", false))
  247. {
  248. goto IL_158;
  249. }
  250. this.state = CIGTutorialState.Success;
  251. goto IL_158;
  252. IL_132:
  253. this.state = CIGTutorialState.Finished;
  254. IL_13F:
  255. IL_158:
  256. if (state != this.state && this._focusBuilding != focusBuilding && focusBuilding != null)
  257. {
  258. focusBuilding.HideMovingArrow();
  259. }
  260. if (state != this.state && this.state != CIGTutorialState.Finished)
  261. {
  262. }
  263. }
  264. public void NotifyBuildCount(CIGBuilderManager manager, int currentCount)
  265. {
  266. if (this._cranePopupShown || currentCount < manager.MaxBuildCount)
  267. {
  268. return;
  269. }
  270. if (this._state != CIGTutorialState.Finished)
  271. {
  272. return;
  273. }
  274. Action action = delegate()
  275. {
  276. this._cranePopupShown = true;
  277. this.serializing.Serialize();
  278. };
  279. SingletonMonobehaviour<PopupManager>.Instance.RequestGenericPopup(UISpriteType.Crane, Localization.Key("tutorial"), Localization.Format(Localization.Key("tutorial_touch_crane_popup"), new ILocalizedString[]
  280. {
  281. Localization.Integer(manager.MaxBuildCount)
  282. }), Localization.Key("continue_message"), null, action, action, action, true);
  283. HUDState hudstate = UnityEngine.Object.FindObjectOfType<HUDState>();
  284. if (hudstate != null)
  285. {
  286. hudstate.ShowArrowAboveButtonIndex(7, 10);
  287. }
  288. }
  289. private void PopupClosed()
  290. {
  291. this._textSkipCount = Math.Max(0, this._textSkipCount);
  292. this.TutorialMessageShown();
  293. }
  294. private void ShowTutorialDialog(ILocalizedString bodyText, int targetedButton)
  295. {
  296. if (bodyText != this._lastShownText || !this._tutorialDialogActive)
  297. {
  298. State currentState = this._fsm.CurrentState;
  299. if (currentState is HUDState)
  300. {
  301. ((HUDState)currentState).ShowTutorialDialog(bodyText, new Action(this.HideTutorialDialog));
  302. this._arrowIndex = targetedButton;
  303. this._lastShownText = bodyText;
  304. this._popupShown = true;
  305. this._tutorialDialogActive = true;
  306. SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("tutorial_dialog");
  307. this.TutorialMessageShown();
  308. }
  309. }
  310. }
  311. private void HideTutorialDialog()
  312. {
  313. this._tutorialDialogActive = false;
  314. SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("none");
  315. }
  316. private void TutorialMessageShown()
  317. {
  318. if (this._arrowIndex >= 0)
  319. {
  320. this.ShowArrow(this._arrowIndex);
  321. }
  322. if (this._focusBuilding != null && this._focusBuilding.state == BuildingState.Constructing)
  323. {
  324. this._focusBuilding.ShowMovingArrow();
  325. }
  326. }
  327. private void ShowPopup(ILocalizedString bodyText, ILocalizedString buttonText, bool showFlares, int targetedButton)
  328. {
  329. if (!this.PreShowPopupCheck(bodyText, targetedButton))
  330. {
  331. return;
  332. }
  333. SingletonMonobehaviour<PopupManager>.Instance.RequestFirstPopup<TutorialPopupState>(delegate(State state)
  334. {
  335. ((TutorialPopupState)state).UpdateInfo(bodyText, buttonText, showFlares, new Action(this.PopupClosed));
  336. });
  337. this.PostShowPopup(bodyText);
  338. }
  339. private bool PreShowPopupCheck(ILocalizedString bodyText, int targetedButton)
  340. {
  341. if (bodyText == this._lastShownText)
  342. {
  343. if (++this._textSkipCount < 10)
  344. {
  345. return false;
  346. }
  347. }
  348. else
  349. {
  350. this._popupShown = false;
  351. }
  352. if (!(this._fsm.CurrentState is HUDState) || !SingletonMonobehaviour<PopupManager>.Instance.PopupsMayOpen)
  353. {
  354. return false;
  355. }
  356. this._arrowIndex = targetedButton;
  357. return true;
  358. }
  359. private void PostShowPopup(ILocalizedString bodyText)
  360. {
  361. SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("tutorial_dialog");
  362. this._lastShownText = bodyText;
  363. this._textSkipCount = -150;
  364. this._popupShown = true;
  365. }
  366. private void ShowArrow(int index)
  367. {
  368. State currentState = this._fsm.CurrentState;
  369. if (currentState is HUDState)
  370. {
  371. ((HUDState)currentState).ShowArrowAboveButtonIndex(index, 0);
  372. }
  373. }
  374. private CIGBuilding BuildingOfTypeBuilt<T>(bool checkFinished)
  375. {
  376. foreach (Building building in UnityEngine.Object.FindObjectsOfType<Building>())
  377. {
  378. if (building is CIGBuilding)
  379. {
  380. if (building.tile.status == GridTile.Status.Created)
  381. {
  382. CIGBuilding cigbuilding = (CIGBuilding)building;
  383. if (!cigbuilding.activatable || cigbuilding.Activated)
  384. {
  385. if (cigbuilding is T)
  386. {
  387. if (!checkFinished)
  388. {
  389. return cigbuilding;
  390. }
  391. if (cigbuilding.IsConstructed)
  392. {
  393. return cigbuilding;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. }
  400. return null;
  401. }
  402. private CIGBuilding BuildingOfTypeHasRoad<T>()
  403. {
  404. foreach (Building building in UnityEngine.Object.FindObjectsOfType<Building>())
  405. {
  406. if (building is CIGBuilding)
  407. {
  408. if (building.tile.status == GridTile.Status.Created)
  409. {
  410. CIGBuilding cigbuilding = (CIGBuilding)building;
  411. if (!cigbuilding.activatable)
  412. {
  413. if (cigbuilding != null && cigbuilding is T && (!cigbuilding.checkForRoad || cigbuilding.CheckForRoad()))
  414. {
  415. return cigbuilding;
  416. }
  417. }
  418. }
  419. }
  420. }
  421. return null;
  422. }
  423. private bool HaveWaitingCommercialBuilding()
  424. {
  425. foreach (CIGCommercialBuilding cigcommercialBuilding in UnityEngine.Object.FindObjectsOfType<CIGCommercialBuilding>())
  426. {
  427. if (cigcommercialBuilding.state == BuildingState.Normal && (!cigcommercialBuilding.checkForRoad || cigcommercialBuilding.CheckForRoad()) && cigcommercialBuilding.Employees * 100 >= cigcommercialBuilding.MaxEmployees * 90 && cigcommercialBuilding.ProfitTimeLeft >= 300)
  428. {
  429. return true;
  430. }
  431. }
  432. return false;
  433. }
  434. [SerializeField]
  435. [SelfReference]
  436. private Serializing serializing;
  437. public const string CityAdvisorOpenedKey = "cityAdvisorOpened";
  438. private MagicalReference<CityIsland> _cityIsland = new MagicalReference<CityIsland>();
  439. private MagicalReference<HUDState> _hudState = new MagicalReference<HUDState>();
  440. private CIGGameState _gameState;
  441. private CIGTutorialState _state;
  442. private FiniteStateMachine _fsm;
  443. private ILocalizedString _lastShownText;
  444. private bool _popupShown;
  445. private int _textSkipCount;
  446. private int _arrowIndex = -1;
  447. private CIGBuilding _focusBuilding;
  448. private bool _running;
  449. private IEnumerator _tutorialRoutine;
  450. private bool _cranePopupShown;
  451. private bool _tutorialDialogActive;
  452. public delegate void TutorialFinishedEventHandler();
  453. public delegate void HasDeserializedEventHandler();
  454. }