Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

446 rader
11 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using CIG;
  6. using CIGEnums;
  7. using SUISS.Core;
  8. using SUISS.Scheduling;
  9. using SUISS.Storage;
  10. using SUISSEngine;
  11. using UnityEngine;
  12. public sealed class CIGBuilderManager : SingletonMonobehaviour<CIGBuilderManager>
  13. {
  14. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  15. public event CIGBuilderManager.HasDeserializedEventHandler HasDeserializedEvent;
  16. private void FireHasDeserializedEvent()
  17. {
  18. if (this.HasDeserializedEvent != null)
  19. {
  20. this.HasDeserializedEvent();
  21. }
  22. }
  23. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  24. public event CIGBuilderManager.BuildCountChangedEventHandler BuildCountChangedEvent;
  25. private void FireBuildCountChangedEvent()
  26. {
  27. if (this.BuildCountChangedEvent != null)
  28. {
  29. this.BuildCountChangedEvent(this.CurrentBuildCount, this.MaxBuildCount);
  30. }
  31. }
  32. private void OnSerialize(Dictionary<string, object> parentValues)
  33. {
  34. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  35. foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
  36. {
  37. string key = dictionary.Count.ToString();
  38. dictionary[key] = buildInfo.Serialize();
  39. }
  40. parentValues["builderManagerList"] = dictionary;
  41. parentValues["craneCount"] = this._craneCount;
  42. parentValues["goldCraneCount"] = this._goldCraneCount;
  43. parentValues["iapCraneCount"] = this._iapCraneCount;
  44. }
  45. private void OnDeserialize(Dictionary<string, object> parentValues)
  46. {
  47. this._buildings.Clear();
  48. if (parentValues.ContainsKey("builderManagerList"))
  49. {
  50. Dictionary<string, object> dictionary = (Dictionary<string, object>)parentValues["builderManagerList"];
  51. foreach (KeyValuePair<string, object> keyValuePair in dictionary)
  52. {
  53. this._buildings.Add(new CIGBuilderManager.BuildInfo((Dictionary<string, object>)keyValuePair.Value));
  54. }
  55. }
  56. this._craneCount = parentValues.GetInt("craneCount", this.initialCraneCount);
  57. this._goldCraneCount = parentValues.GetInt("goldCraneCount", 0);
  58. this._iapCraneCount = parentValues.GetInt("iapCraneCount", 0);
  59. }
  60. private void OnDeserialized()
  61. {
  62. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._updateRoutine = this.UpdateBadgeRoutine());
  63. this.HasDeserialized = true;
  64. this.FireHasDeserializedEvent();
  65. }
  66. protected override void Awake()
  67. {
  68. base.Awake();
  69. this._craneCount = this.initialCraneCount;
  70. }
  71. protected override void OnDestroy()
  72. {
  73. base.OnDestroy();
  74. if (this._updateRoutine != null && SingletonMonobehaviour<Scheduler>.IsAvailable)
  75. {
  76. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._updateRoutine);
  77. }
  78. }
  79. private IEnumerator WaitForUpdateRoutine(double t)
  80. {
  81. yield return Timing.time + t;
  82. this._waitRoutine = null;
  83. yield break;
  84. }
  85. private IEnumerator UpdateBadgeRoutine()
  86. {
  87. for (;;)
  88. {
  89. this.UpdateHUDBadge();
  90. double t = 60.0;
  91. foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
  92. {
  93. if (!buildInfo.IsFinished && buildInfo.Timeleft < t)
  94. {
  95. t = buildInfo.Timeleft;
  96. }
  97. }
  98. yield return this._waitRoutine = this.WaitForUpdateRoutine(t + 0.1);
  99. }
  100. yield break;
  101. }
  102. private void ForceUpdate()
  103. {
  104. if (this._waitRoutine != null)
  105. {
  106. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._waitRoutine);
  107. this._waitRoutine = null;
  108. }
  109. }
  110. private void UpdateHUDBadge()
  111. {
  112. HUDState hudstate = UnityEngine.Object.FindObjectOfType<HUDState>();
  113. if (hudstate != null)
  114. {
  115. hudstate.UpdateBuildQueueButton();
  116. }
  117. CIGTutorialManager instanceIfAvailable = SingletonMonobehaviour<CIGTutorialManager>.InstanceIfAvailable;
  118. if (instanceIfAvailable != null)
  119. {
  120. instanceIfAvailable.NotifyBuildCount(this, this.CurrentBuildCount);
  121. }
  122. }
  123. public void StartTracking(Building b, double finishTime)
  124. {
  125. this._buildings.Add(new CIGBuilderManager.BuildInfo(b, finishTime));
  126. this.serializing.Serialize();
  127. this.ForceUpdate();
  128. this.FireBuildCountChangedEvent();
  129. }
  130. public void FinishTracking(Building b)
  131. {
  132. for (int i = 0; i < this._buildings.Count; i++)
  133. {
  134. if (b.serializing.StorageKey == this._buildings[i].serializationKey)
  135. {
  136. this._buildings.RemoveAt(i);
  137. this.serializing.Serialize();
  138. this.ForceUpdate();
  139. this.FireBuildCountChangedEvent();
  140. return;
  141. }
  142. }
  143. }
  144. public void SpeedupAll()
  145. {
  146. Dictionary<string, Building> dictionary = new Dictionary<string, Building>();
  147. foreach (Building building in UnityEngine.Object.FindObjectsOfType<Building>())
  148. {
  149. dictionary[building.serializing.StorageKey] = building;
  150. }
  151. bool flag = false;
  152. for (int j = this._buildings.Count - 1; j >= 0; j--)
  153. {
  154. CIGBuilderManager.BuildInfo buildInfo = this._buildings[j];
  155. if (!buildInfo.IsFinished)
  156. {
  157. if (dictionary.ContainsKey(buildInfo.serializationKey))
  158. {
  159. Building building2 = dictionary[buildInfo.serializationKey];
  160. if (building2.state == BuildingState.Constructing)
  161. {
  162. building2.FinishConstruction();
  163. }
  164. else
  165. {
  166. UpgradableBuilding upgradableBuilding = building2 as UpgradableBuilding;
  167. if (upgradableBuilding != null)
  168. {
  169. if (upgradableBuilding.IsUpgrading)
  170. {
  171. upgradableBuilding.FinishUpgrade();
  172. }
  173. else
  174. {
  175. UnityEngine.Debug.LogWarning(string.Format("Building {0} is not constructing, but it's also not upgrading!", building2.name));
  176. }
  177. }
  178. else
  179. {
  180. UnityEngine.Debug.LogWarning(string.Format("Building {0} is not constructing, but it's not an UpgradableBuilding!", building2.name));
  181. }
  182. }
  183. }
  184. else
  185. {
  186. buildInfo.speedupped = true;
  187. }
  188. }
  189. }
  190. if (flag)
  191. {
  192. this.serializing.Serialize();
  193. }
  194. this.ForceUpdate();
  195. this.FireBuildCountChangedEvent();
  196. }
  197. public void AddCrane(bool iap)
  198. {
  199. int craneCount = this._craneCount;
  200. this._craneCount++;
  201. if (iap)
  202. {
  203. this._iapCraneCount++;
  204. }
  205. else
  206. {
  207. this._goldCraneCount++;
  208. }
  209. this.serializing.Serialize();
  210. Storage.SaveLifecycle(this.serializing.StorageLifecycle, true);
  211. SingletonMonobehaviour<CIGGameState>.Instance.FireValueChangedHandler("craneCount", craneCount, this._craneCount);
  212. }
  213. public bool IsSpeedupped(Building b)
  214. {
  215. foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
  216. {
  217. if (b.serializing.StorageKey == buildInfo.serializationKey)
  218. {
  219. return buildInfo.speedupped;
  220. }
  221. }
  222. return false;
  223. }
  224. public int MaxBuildCount
  225. {
  226. get
  227. {
  228. return this._craneCount;
  229. }
  230. }
  231. public int GoldCraneCount
  232. {
  233. get
  234. {
  235. return this._goldCraneCount;
  236. }
  237. }
  238. public int IapCraneCount
  239. {
  240. get
  241. {
  242. return this._iapCraneCount;
  243. }
  244. }
  245. public int AvailableBuildCount
  246. {
  247. get
  248. {
  249. return this.MaxBuildCount - this.CurrentBuildCount;
  250. }
  251. }
  252. public int CurrentBuildCount
  253. {
  254. get
  255. {
  256. int num = 0;
  257. foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
  258. {
  259. if (!buildInfo.IsFinished)
  260. {
  261. num++;
  262. }
  263. }
  264. return num;
  265. }
  266. }
  267. public List<CIGBuilderManager.BuildInfo> BuildList
  268. {
  269. get
  270. {
  271. List<CIGBuilderManager.BuildInfo> list = new List<CIGBuilderManager.BuildInfo>();
  272. foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
  273. {
  274. if (!buildInfo.IsFinished)
  275. {
  276. list.Add(buildInfo);
  277. }
  278. }
  279. return list;
  280. }
  281. }
  282. public bool HasDeserialized { get; private set; }
  283. public decimal CurrentCraneGoldCost
  284. {
  285. get
  286. {
  287. if (this._craneCount >= 15)
  288. {
  289. return -1m;
  290. }
  291. switch (this._goldCraneCount)
  292. {
  293. case 0:
  294. return 30m;
  295. case 1:
  296. return 50m;
  297. case 2:
  298. return 70m;
  299. case 3:
  300. return 100m;
  301. default:
  302. return 150m;
  303. }
  304. }
  305. }
  306. public CIG3StoreProduct CurrentCraneIAP
  307. {
  308. get
  309. {
  310. if (this._craneCount >= 15)
  311. {
  312. return null;
  313. }
  314. int iapCount = this._iapCraneCount + 1;
  315. return SingletonMonobehaviour<CIGStoreManager>.Instance.Store.FindProduct((CIG3StoreProduct p) => p.Category == StoreProductCategory.Cranes && p.From <= iapCount && p.To >= iapCount);
  316. }
  317. }
  318. public bool CanPurchaseCrane
  319. {
  320. get
  321. {
  322. return this.CurrentCraneIAP != null || this.CurrentCraneGoldCost > 0m;
  323. }
  324. }
  325. private const string NameKey = "name";
  326. private const string SerializationKeyKey = "serializationKey";
  327. private const string FinishTimeKey = "finishTime";
  328. private const string SpeeduppedKey = "speedupped";
  329. private const int MaxCranes = 15;
  330. [SelfReference]
  331. public Serializing serializing;
  332. public int initialCraneCount = 5;
  333. private const string ListKey = "builderManagerList";
  334. public const string CraneCountKey = "craneCount";
  335. private const string GoldCraneCountKey = "goldCraneCount";
  336. private const string IapCraneCountKey = "iapCraneCount";
  337. private List<CIGBuilderManager.BuildInfo> _buildings = new List<CIGBuilderManager.BuildInfo>();
  338. private IEnumerator _updateRoutine;
  339. private IEnumerator _waitRoutine;
  340. private int _craneCount;
  341. private int _goldCraneCount;
  342. private int _iapCraneCount;
  343. public class BuildInfo
  344. {
  345. public BuildInfo(Building building, double finishTime)
  346. {
  347. this.prefab = SingletonMonobehaviour<GameObjectManager>.Instance.GetPrefabWithName(building.name);
  348. this.serializationKey = building.serializing.StorageKey;
  349. this.finishTime = finishTime;
  350. this.speedupped = false;
  351. }
  352. public BuildInfo(Dictionary<string, object> values)
  353. {
  354. this.prefab = SingletonMonobehaviour<GameObjectManager>.Instance.GetPrefabWithName((string)values["name"]);
  355. this.serializationKey = (string)values["serializationKey"];
  356. this.finishTime = (double)values["finishTime"];
  357. this.speedupped = (bool)values["speedupped"];
  358. }
  359. public Dictionary<string, object> Serialize()
  360. {
  361. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  362. dictionary["name"] = this.prefab.name;
  363. dictionary["serializationKey"] = this.serializationKey;
  364. dictionary["finishTime"] = this.finishTime;
  365. dictionary["speedupped"] = this.speedupped;
  366. return dictionary;
  367. }
  368. public double Timeleft
  369. {
  370. get
  371. {
  372. return this.finishTime - Timing.time;
  373. }
  374. }
  375. public bool IsFinished
  376. {
  377. get
  378. {
  379. return this.speedupped || this.finishTime <= Timing.time;
  380. }
  381. }
  382. public GameObject prefab;
  383. public string serializationKey;
  384. public double finishTime;
  385. public bool speedupped;
  386. }
  387. public delegate void HasDeserializedEventHandler();
  388. public delegate void BuildCountChangedEventHandler(int used, int total);
  389. }