您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

372 行
9.6 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 CIGEnums;
  8. using SUISS.Core;
  9. using SUISS.Scheduling;
  10. using UnityEngine;
  11. namespace SUISSEngine
  12. {
  13. public class UpgradableBuilding : Building
  14. {
  15. public static bool LevelIconsHidden
  16. {
  17. get
  18. {
  19. return UpgradableBuilding.levelIconsHidden;
  20. }
  21. }
  22. public static void SetLevelIconsHidden(bool hidden, IsometricGrid grid)
  23. {
  24. if (hidden != UpgradableBuilding.levelIconsHidden)
  25. {
  26. UpgradableBuilding.levelIconsHidden = hidden;
  27. if (grid != null)
  28. {
  29. foreach (GridTile gridTile in grid.Tiles)
  30. {
  31. UpgradableBuilding component = gridTile.GetComponent<UpgradableBuilding>();
  32. if (component != null)
  33. {
  34. component.UpdateLevelIcon(component.CurrentLevel);
  35. }
  36. }
  37. }
  38. }
  39. }
  40. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  41. public event Action UpgradeStartedEvent;
  42. protected virtual void FireUpgradeStartedEvent()
  43. {
  44. if (this.UpgradeStartedEvent != null)
  45. {
  46. this.UpgradeStartedEvent();
  47. }
  48. }
  49. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  50. public event Building.TimedBuildingEventHandler UpgradeCompletedEvent;
  51. protected virtual void FireUpgradeCompletedEvent(double upgradeCompletedTime)
  52. {
  53. if (this.UpgradeCompletedEvent != null)
  54. {
  55. this.UpgradeCompletedEvent(upgradeCompletedTime);
  56. }
  57. }
  58. public virtual Currencies UpgradeCost
  59. {
  60. get
  61. {
  62. return this.initialPurchasePrice;
  63. }
  64. }
  65. protected override void Awake()
  66. {
  67. base.Awake();
  68. this.currentLevel = this.initialLevel;
  69. this.IsUpgrading = false;
  70. }
  71. protected override void OnDestroy()
  72. {
  73. this.StopUpgradeRoutine();
  74. base.OnDestroy();
  75. }
  76. public int CurrentLevel
  77. {
  78. get
  79. {
  80. return this.currentLevel;
  81. }
  82. }
  83. public virtual int DisplayLevel
  84. {
  85. get
  86. {
  87. return this.CurrentLevel;
  88. }
  89. }
  90. public virtual bool CanUpgrade
  91. {
  92. get
  93. {
  94. return this.currentLevel < this.GetMaxLevel();
  95. }
  96. }
  97. public bool IsUpgrading { get; private set; }
  98. public virtual int UpgradeTime
  99. {
  100. get
  101. {
  102. int num = Math.Max(5, this.constructionTime / 60);
  103. return 60 * (int)((float)num * (0.75f + 0.15f * (float)this.CurrentLevel));
  104. }
  105. }
  106. public double UpgradeTimeLeft
  107. {
  108. get
  109. {
  110. if (this._upgradeEnumerator != null)
  111. {
  112. return Scheduler.TimeLeft(this._upgradeEnumerator);
  113. }
  114. return 0.0;
  115. }
  116. }
  117. public int GetMaxLevel()
  118. {
  119. if (SingletonMonobehaviour<CIGGameState>.Instance.MaxBuildingLevel10 && this.maximumLevel > 10)
  120. {
  121. return 10;
  122. }
  123. return this.maximumLevel;
  124. }
  125. public virtual bool StartUpgrade()
  126. {
  127. if (!this.CanUpgrade)
  128. {
  129. UnityEngine.Debug.LogWarning(string.Format("{0}: Cannot upgrade, since current level is {1} and maximum level is {2}", base.name, this.currentLevel, this.GetMaxLevel()));
  130. return false;
  131. }
  132. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.Upgrade);
  133. this.StartUpgradeRoutine();
  134. base.SetProgressBar((double)this.UpgradeTime, (double)this.UpgradeTime);
  135. this._gridTileIconManager.SetIcon<GridTileIcon>(GridTileIconType.UpgradeArrow);
  136. return true;
  137. }
  138. public virtual void FinishUpgrade()
  139. {
  140. if (this._upgradeEnumerator != null)
  141. {
  142. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.Fairy);
  143. this.upgradedTime = Timing.time;
  144. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._upgradeEnumerator);
  145. this._upgradeEnumerator = null;
  146. base.BroadcastMessage("OnSpeedupPurchased", SendMessageOptions.DontRequireReceiver);
  147. }
  148. }
  149. public virtual ILocalizedString ReasonWhyCantUpgrade(out ILocalizedString title)
  150. {
  151. title = Localization.EmptyLocalizedString;
  152. if (this.CanUpgrade)
  153. {
  154. return null;
  155. }
  156. return Localization.Key("object_upgraded_to_maximum_level");
  157. }
  158. protected virtual void UpdateLevelIcon(int level)
  159. {
  160. if (UpgradableBuilding.levelIconsHidden)
  161. {
  162. this._gridTileIconManager.RemoveIcon(GridTileIconType.Level);
  163. }
  164. else if (this.maximumLevel != 0 && base.state != BuildingState.Constructing)
  165. {
  166. LevelGridTileIcon levelGridTileIcon = this._gridTileIconManager.SetIcon<LevelGridTileIcon>(GridTileIconType.Level);
  167. levelGridTileIcon.SetLevel(level);
  168. }
  169. }
  170. private IEnumerator WaitForUpgrade()
  171. {
  172. if (this.upgradedTime < 0.0)
  173. {
  174. UnityEngine.Debug.LogWarning("upgradedTime shouldn't be less then 0");
  175. this.upgradedTime = Timing.time + (double)this.UpgradeTime;
  176. }
  177. yield return this.upgradedTime;
  178. yield break;
  179. }
  180. private IEnumerator UpgradeBehaviour()
  181. {
  182. if (!this.IsUpgrading)
  183. {
  184. this.IsUpgrading = true;
  185. this.upgradedTime = Timing.Instance.GameTime + (double)this.UpgradeTime;
  186. base.BroadcastMessage("OnUpgradeStarted", SendMessageOptions.DontRequireReceiver);
  187. this.serializing.Serialize();
  188. SingletonMonobehaviour<CIGBuilderManager>.Instance.StartTracking(this, this.upgradedTime);
  189. }
  190. else if (SingletonMonobehaviour<CIGBuilderManager>.Instance.IsSpeedupped(this))
  191. {
  192. this.upgradedTime = Timing.Instance.GameTime;
  193. }
  194. yield return this._upgradeEnumerator = this.WaitForUpgrade();
  195. this._upgradeEnumerator = null;
  196. if (this._upgradeCancelled)
  197. {
  198. this._upgradeCancelled = false;
  199. this.IsUpgrading = false;
  200. SingletonMonobehaviour<CIGBuilderManager>.Instance.FinishTracking(this);
  201. yield break;
  202. }
  203. this.PerformUpgrade();
  204. this._upgradeBehaviourRoutine = null;
  205. yield break;
  206. }
  207. protected void PerformUpgrade()
  208. {
  209. ActivatableBuilding component = base.GetComponent<ActivatableBuilding>();
  210. SingletonMonobehaviour<CIGGameStats>.Instance.AddBuildingAction();
  211. this.currentLevel++;
  212. this.IsUpgrading = false;
  213. this.UpdateLevelIcon(this.currentLevel);
  214. base.BroadcastMessage("OnUpgradeCompleted", this.upgradedTime, SendMessageOptions.DontRequireReceiver);
  215. this.serializing.Serialize();
  216. SingletonMonobehaviour<CIGBuilderManager>.Instance.FinishTracking(this);
  217. }
  218. protected override void OnSerialize(Dictionary<string, object> values)
  219. {
  220. base.OnSerialize(values);
  221. values["level"] = this.currentLevel;
  222. if (this.IsUpgrading)
  223. {
  224. if (this.upgradedTime >= 0.0)
  225. {
  226. values["upgradedTime"] = this.upgradedTime;
  227. }
  228. else if (this._upgradeEnumerator != null)
  229. {
  230. UnityEngine.Debug.LogWarning("upgradedTime shouldn't be less then 0");
  231. values["upgradedTime"] = (double)this._upgradeEnumerator.Current;
  232. }
  233. else if (this.UpgradeTime > 0)
  234. {
  235. UnityEngine.Debug.LogWarning("upgradedTime shouldn't be less then 0");
  236. values["upgradedTime"] = Timing.time + (double)this.UpgradeTime;
  237. }
  238. else
  239. {
  240. UnityEngine.Debug.LogWarning("Cannot find what to store for 'upgradedTime'");
  241. }
  242. }
  243. else if (values.ContainsKey("upgradedTime"))
  244. {
  245. values.Remove("upgradedTime");
  246. }
  247. }
  248. protected override void OnDeserialize(Dictionary<string, object> values)
  249. {
  250. base.OnDeserialize(values);
  251. if (values.ContainsKey("level"))
  252. {
  253. this.currentLevel = (int)values["level"];
  254. }
  255. if (values.ContainsKey("upgradedTime"))
  256. {
  257. this.upgradedTime = (double)values["upgradedTime"];
  258. this.IsUpgrading = true;
  259. }
  260. else
  261. {
  262. this.upgradedTime = -1.0;
  263. this.IsUpgrading = false;
  264. }
  265. }
  266. protected override void OnDeserialized()
  267. {
  268. base.OnDeserialized();
  269. this.UpdateLevelIcon(this.currentLevel);
  270. if (this.IsUpgrading)
  271. {
  272. this.StartUpgradeRoutine();
  273. base.SetProgressBar(this.UpgradeTimeLeft, (double)this.UpgradeTime);
  274. this._gridTileIconManager.SetIcon<GridTileIcon>(GridTileIconType.UpgradeArrow);
  275. }
  276. }
  277. protected override void OnConstructionCompleted()
  278. {
  279. base.OnConstructionCompleted();
  280. this.UpdateLevelIcon(this.currentLevel);
  281. }
  282. protected override void OnDemolishStarted()
  283. {
  284. base.HideProgressBar();
  285. base.OnDemolishStarted();
  286. if (this.IsUpgrading)
  287. {
  288. this._upgradeCancelled = true;
  289. if (this._upgradeEnumerator != null)
  290. {
  291. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._upgradeEnumerator);
  292. this._upgradeEnumerator = null;
  293. }
  294. }
  295. this._gridTileIconManager.RemoveIcon(GridTileIconType.Level);
  296. }
  297. protected virtual void OnUpgradeStarted()
  298. {
  299. this.FireUpgradeStartedEvent();
  300. }
  301. protected virtual void OnUpgradeCompleted(double upgradedTime)
  302. {
  303. this.FireUpgradeCompletedEvent(upgradedTime);
  304. base.HideProgressBar();
  305. this._gridTileIconManager.RemoveIcon(GridTileIconType.UpgradeArrow);
  306. }
  307. private void StartUpgradeRoutine()
  308. {
  309. this.StopUpgradeRoutine();
  310. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._upgradeBehaviourRoutine = this.UpgradeBehaviour(), base.gameObject);
  311. }
  312. private void StopUpgradeRoutine()
  313. {
  314. if (SingletonMonobehaviour<Scheduler>.IsAvailable && this._upgradeBehaviourRoutine != null)
  315. {
  316. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._upgradeBehaviourRoutine);
  317. this._upgradeBehaviourRoutine = null;
  318. this._upgradeEnumerator = null;
  319. }
  320. }
  321. protected static bool levelIconsHidden = true;
  322. public int initialLevel;
  323. [SerializeField]
  324. private int maximumLevel = 10;
  325. private int currentLevel;
  326. private IEnumerator _upgradeBehaviourRoutine;
  327. private IEnumerator _upgradeEnumerator;
  328. private bool _upgradeCancelled;
  329. protected double upgradedTime = -1.0;
  330. }
  331. }