You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

878 lines
29 KiB

  1. using CIG.Translation;
  2. using CIGEnums;
  3. using SUISS.Core;
  4. using SUISSEngine;
  5. using System;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. public abstract class CIGBuilding : HappinessBuilding
  9. {
  10. public const string FreeBuildingTypeKey = "building_name";
  11. private const int TutorialOverrideConstructionTime = 5;
  12. public Currencies constructionXP;
  13. public int[] unlockLevels;
  14. public int maxFacilities;
  15. public int buildMenuOrdering;
  16. public int strictMax = -1;
  17. public Island island;
  18. public GameObject upgradeSignPrefab;
  19. public Currencies baseUpgradeCost;
  20. public Vehicle.VehicleType spawnsVehicleType = Vehicle.VehicleType.None;
  21. private OverlayButtons _overlayButtons;
  22. private OverlayTitle _overlayTitle;
  23. [SelfReference(true)]
  24. public Animator animator;
  25. [ChildReference(true)]
  26. public Animator bottomAnimator;
  27. private readonly int[] _upgradeTimeFactor = new int[21]
  28. {
  29. 100,
  30. 25,
  31. 33,
  32. 43,
  33. 56,
  34. 73,
  35. 95,
  36. 124,
  37. 161,
  38. 209,
  39. 272,
  40. 326,
  41. 391,
  42. 469,
  43. 563,
  44. 676,
  45. 811,
  46. 973,
  47. 1168,
  48. 1402,
  49. 1682
  50. };
  51. private CIGIslandState _islandState;
  52. private static bool errorSent;
  53. private CIGGameState _gameState;
  54. private CIGGameStats _gameStats;
  55. protected CIGGameConstants constants;
  56. protected GameObject prefab;
  57. protected GameObject _upgradeSign;
  58. public override Currencies PurchasePrice
  59. {
  60. get
  61. {
  62. IOngoingReward ongoingReward = Singleton<OngoingRewardManager>.Instance.FindActiveReward((IOngoingReward reward) => reward is FreeBuildingReward && ((FreeBuildingReward)reward).BuildingName == base.name);
  63. if ((ongoingReward != null && ongoingReward.IsActive) || SingletonMonobehaviour<CIGPurchasedBuildingsManager>.Instance.IsUnconsumed(this))
  64. {
  65. return new Currencies("Gold", 0.0m);
  66. }
  67. if (constants == null)
  68. {
  69. constants = SingletonMonobehaviour<CIGGameConstants>.Instance;
  70. }
  71. Currencies currencies = base.PurchasePrice;
  72. if (unlockLevels.Length == 0)
  73. {
  74. return currencies;
  75. }
  76. int num = GameStats.CashCount(base.name);
  77. int num2 = GameStats.NumberOf(base.name);
  78. int b = num2 - num;
  79. if (!currencies.ContainsPositive("Gold"))
  80. {
  81. int num3 = Array.BinarySearch(unlockLevels, GameState.Level);
  82. int num4 = num3 + 1;
  83. if (num3 < 0)
  84. {
  85. num4 = ~num3;
  86. }
  87. if (num >= num4)
  88. {
  89. currencies = new Currencies("Gold", goldCostAtMax);
  90. }
  91. }
  92. if (currencies.ContainsPositive("Gold"))
  93. {
  94. decimal value = ConstructionCostGoldPenalty * (decimal)Mathf.Max(0, b);
  95. currencies += new Currencies("Gold", value);
  96. }
  97. return currencies;
  98. }
  99. }
  100. public override bool IsUnlocked
  101. {
  102. get
  103. {
  104. if (!base.IsUnlocked)
  105. {
  106. return false;
  107. }
  108. if (unlockLevels.Length > 0)
  109. {
  110. return unlockLevels[0] <= GameState.Level;
  111. }
  112. return true;
  113. }
  114. }
  115. public int NextUnlockLevel
  116. {
  117. get
  118. {
  119. if (!IsUnlocked)
  120. {
  121. return unlockLevels[0];
  122. }
  123. int num = Array.BinarySearch(unlockLevels, GameState.Level);
  124. num = ((num >= 0) ? (num + 1) : (~num));
  125. if (num < unlockLevels.Length)
  126. {
  127. return unlockLevels[num];
  128. }
  129. return int.MaxValue;
  130. }
  131. }
  132. public override Currencies UpgradeCost
  133. {
  134. get
  135. {
  136. if (activatable && !Activated)
  137. {
  138. return base.UpgradeCost;
  139. }
  140. decimal value = baseUpgradeCost.GetValue("Cash");
  141. value *= (decimal)base.CurrentLevel + 1m;
  142. value /= (decimal)GetMaxLevel();
  143. value = Math.Ceiling(value);
  144. return new Currencies("Cash", value);
  145. }
  146. }
  147. public override int UpgradeTime
  148. {
  149. get
  150. {
  151. if (activatable && !Activated)
  152. {
  153. return base.UpgradeTime;
  154. }
  155. int num = constructionTime;
  156. if (activatable)
  157. {
  158. num = activationTime;
  159. }
  160. int num2 = activatable ? (-1) : 0;
  161. return _upgradeTimeFactor[base.CurrentLevel + 1 + num2] * num / 100;
  162. }
  163. }
  164. public virtual decimal SpeedupCostCash
  165. {
  166. get
  167. {
  168. long seconds = Math.Max(60L, GetSecondsLeft());
  169. return GetSpeedupCostCashForSeconds(seconds);
  170. }
  171. }
  172. public virtual decimal SpeedupCostGold
  173. {
  174. get
  175. {
  176. long secondsLeft = GetSecondsLeft();
  177. return GetSpeedupCostGoldForSeconds(secondsLeft);
  178. }
  179. }
  180. public virtual Currencies SpeedupProfit => Currencies.Zero;
  181. public GameObject Prefab
  182. {
  183. get
  184. {
  185. if (prefab == null)
  186. {
  187. prefab = base.gameObject;
  188. }
  189. return prefab;
  190. }
  191. }
  192. protected CIGIslandState IslandState
  193. {
  194. get
  195. {
  196. if (_islandState == null)
  197. {
  198. _islandState = GetComponentInParent<CIGIslandState>();
  199. }
  200. return _islandState;
  201. }
  202. }
  203. protected virtual decimal ConstructionCostGoldPenalty => SingletonMonobehaviour<CIGVariables>.Instance.ConstructionCostGoldPenalty;
  204. public override List<BuildingProperty> ShownProperties
  205. {
  206. get
  207. {
  208. List<BuildingProperty> shownProperties = base.ShownProperties;
  209. if (base.state == BuildingState.Preview && constructionXP.ContainsPositive("XP"))
  210. {
  211. shownProperties.Add(BuildingProperty.ConstructionXp);
  212. }
  213. return shownProperties;
  214. }
  215. }
  216. public override bool InfoRequiresFrequentRefresh
  217. {
  218. get
  219. {
  220. if (base.IsUpgrading || base.state == BuildingState.Constructing || base.state == BuildingState.Demolishing)
  221. {
  222. return true;
  223. }
  224. if ((activatable && !Activated) || base.state == BuildingState.Preview)
  225. {
  226. return false;
  227. }
  228. return base.InfoRequiresFrequentRefresh;
  229. }
  230. }
  231. public override bool CanSpeedup
  232. {
  233. get
  234. {
  235. if (base.IsUpgrading || base.state == BuildingState.Constructing || base.state == BuildingState.Demolishing)
  236. {
  237. return true;
  238. }
  239. if ((activatable && !Activated) || base.state == BuildingState.Preview)
  240. {
  241. return false;
  242. }
  243. return base.CanSpeedup;
  244. }
  245. }
  246. public override int Happiness
  247. {
  248. get
  249. {
  250. return base.Happiness;
  251. }
  252. protected set
  253. {
  254. int num = value - base.Happiness;
  255. base.Happiness = value;
  256. if (num != 0)
  257. {
  258. IslandState.AddHappiness(num);
  259. }
  260. }
  261. }
  262. protected virtual bool UseGreyShader => activatable && base.CurrentLevel == 0;
  263. protected CIGGameStats GameStats
  264. {
  265. get
  266. {
  267. if (_gameStats == null)
  268. {
  269. _gameStats = SingletonMonobehaviour<CIGGameStats>.Instance;
  270. }
  271. return _gameStats;
  272. }
  273. }
  274. protected CIGGameState GameState
  275. {
  276. get
  277. {
  278. if (_gameState == null)
  279. {
  280. _gameState = SingletonMonobehaviour<CIGGameState>.Instance;
  281. }
  282. return _gameState;
  283. }
  284. }
  285. protected void OnEnable()
  286. {
  287. GameEvents.Subscribe<UnemiShouldCloseEvent>(OnUnemiShouldCloseEvent);
  288. }
  289. protected void OnDisable()
  290. {
  291. GameEvents.Unsubscribe<UnemiShouldCloseEvent>(OnUnemiShouldCloseEvent);
  292. }
  293. public override int UpgradeHappiness(int toLevel)
  294. {
  295. if (GetMaxLevel() != 0)
  296. {
  297. int num = Math.Max(0, toLevel - base.CurrentLevel);
  298. int val = baseHappinessValue + toLevel * baseHappinessValue / 10 - Happiness;
  299. if (baseHappinessValue < 0)
  300. {
  301. return Math.Min(-num, val);
  302. }
  303. return Math.Max(num, val);
  304. }
  305. return 0;
  306. }
  307. public long GetSecondsLeft()
  308. {
  309. if (base.state == BuildingState.Constructing)
  310. {
  311. return 1 + (long)base.ConstructionTimeLeft;
  312. }
  313. if (base.IsUpgrading)
  314. {
  315. return 1 + (long)base.UpgradeTimeLeft;
  316. }
  317. if (base.state == BuildingState.Demolishing)
  318. {
  319. return 1 + (long)base.DemolishTimeLeft;
  320. }
  321. if (base.state == BuildingState.WaitingForDemolishing)
  322. {
  323. return demolishTime;
  324. }
  325. return 0L;
  326. }
  327. public static Currencies GetImmediateConstructionCost(GameObject prefab)
  328. {
  329. CIGBuilding component = prefab.GetComponent<CIGBuilding>();
  330. if (component == null)
  331. {
  332. throw new ArgumentException($"Prefab {prefab.name} does not contain CIGBuilding component.");
  333. }
  334. Currencies currencies = component.PurchasePrice;
  335. if (!currencies.ContainsPositive("Gold"))
  336. {
  337. currencies = new Currencies("Gold", component.goldCostAtMax);
  338. }
  339. return (currencies + new Currencies("Gold", 2m + GetSpeedupCostGoldForSeconds(component.constructionTime))).Round();
  340. }
  341. public static decimal GetSpeedupCostGoldForSeconds(long seconds)
  342. {
  343. decimal d = Math.Ceiling((decimal)seconds / 60m);
  344. if (d <= 15m)
  345. {
  346. return 1m;
  347. }
  348. return Math.Floor((1m + d / 50m) * SingletonMonobehaviour<CIGWebService>.Instance.Multipliers.UpspeedCostGoldMultiplier);
  349. }
  350. public static decimal GetSpeedupCostCashForSeconds(long seconds)
  351. {
  352. CIGGameStats instance = SingletonMonobehaviour<CIGGameStats>.Instance;
  353. CIGGameConstants instance2 = SingletonMonobehaviour<CIGGameConstants>.Instance;
  354. decimal d = Math.Max(10000m, instance.GlobalProfitPerHour.GetValue("Cash"));
  355. return Math.Max(instance2.minimumSpeedupCostCash, Math.Ceiling(instance2.factorForSpeedupCostCash * d * (decimal)seconds / 3600m / instance2.speedupCashCostFactor * SingletonMonobehaviour<CIGWebService>.Instance.Multipliers.UpspeedCostCashMultiplier));
  356. }
  357. private void OnUnemiShouldCloseEvent(UnemiShouldCloseEvent e)
  358. {
  359. RemoveButtonsAndTitle();
  360. }
  361. protected override void ClickHandler(GameObject target)
  362. {
  363. base.ClickHandler(target);
  364. if (activatable && !Activated && !base.IsUpgrading)
  365. {
  366. SingletonMonobehaviour<PopupManager>.Instance.RequestFirstPopup<BuildingInfoPopupState>(delegate (State state)
  367. {
  368. ((BuildingInfoPopupState)state).SetBuildingAndContent(this, BuildingPopupContent.Activate);
  369. });
  370. }
  371. else if (base.state != BuildingState.Demolishing && base.state != BuildingState.WaitingForDemolishing)
  372. {
  373. ToggleButtonsAndTitle();
  374. }
  375. }
  376. private void ToggleButtonsAndTitle()
  377. {
  378. if (CanSpeedup)
  379. {
  380. RemoveButtonsAndTitle();
  381. SingletonMonobehaviour<PopupManager>.Instance.RequestFirstPopup<BuildingSpeedupPopupState>(delegate (State state)
  382. {
  383. ((BuildingSpeedupPopupState)state).SetBuilding(this);
  384. });
  385. }
  386. else if (_overlayTitle == null && _overlayButtons == null)
  387. {
  388. GameEvents.Invoke(new UnemiShouldCloseEvent(this));
  389. _overlayTitle = OverlayTitle.Get(this);
  390. _overlayTitle.ShowOpenAnimation();
  391. ILocalizedString subtitleText = Localization.EmptyLocalizedString;
  392. if (CanUpgrade || DisplayLevel > 1)
  393. {
  394. subtitleText = Localization.Format("{0} {1}", Localization.Key("level"), Localization.Integer(DisplayLevel));
  395. }
  396. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.ButtonClick);
  397. _overlayTitle.Initialize(base.LocalName, subtitleText);
  398. _overlayButtons = OverlayButtons.Get(this);
  399. _overlayButtons.ShowOpenAnimation();
  400. _overlayButtons.DisableAllButtons();
  401. if (base.state != BuildingState.Normal)
  402. {
  403. return;
  404. }
  405. if (ShownProperties.Count > 0)
  406. {
  407. _overlayButtons.InfoButton.EnableButton(InfoAction);
  408. }
  409. if (!base.IsUpgrading)
  410. {
  411. if (movable && Activated)
  412. {
  413. _overlayButtons.MoveButton.EnableButton(MoveBuildingAction);
  414. }
  415. if (CanUpgrade)
  416. {
  417. _overlayButtons.UpgradeButton.EnableButton(UpgradeAction);
  418. }
  419. }
  420. }
  421. else
  422. {
  423. RemoveButtonsAndTitle();
  424. }
  425. }
  426. private void InfoAction()
  427. {
  428. SingletonMonobehaviour<PopupManager>.Instance.RequestFirstPopup<BuildingInfoPopupState>(delegate (State state)
  429. {
  430. ((BuildingInfoPopupState)state).SetBuildingAndContent(this, BuildingPopupContent.Info);
  431. });
  432. }
  433. private void MoveBuildingAction()
  434. {
  435. CityIsland.Current.StartMoving(base.gameObject);
  436. }
  437. private void UpgradeAction()
  438. {
  439. BuildingPopupContent content = BuildingPopupContent.Upgrade;
  440. if (activatable && !Activated)
  441. {
  442. content = BuildingPopupContent.Activate;
  443. }
  444. SingletonMonobehaviour<PopupManager>.Instance.RequestFirstPopup<BuildingInfoPopupState>(delegate (State state)
  445. {
  446. ((BuildingInfoPopupState)state).SetBuildingAndContent(this, content);
  447. });
  448. }
  449. protected void RemoveButtonsAndTitle()
  450. {
  451. if ((bool)_overlayButtons)
  452. {
  453. OverlayButtons overlayButtonsTemp = _overlayButtons;
  454. overlayButtonsTemp.ShowCloseAnimation(delegate
  455. {
  456. UnityEngine.Object.Destroy(overlayButtonsTemp.gameObject);
  457. });
  458. _overlayButtons = null;
  459. }
  460. if ((bool)_overlayTitle)
  461. {
  462. OverlayTitle overlayTitleTemp = _overlayTitle;
  463. overlayTitleTemp.ShowCloseAnimation(delegate
  464. {
  465. UnityEngine.Object.Destroy(overlayTitleTemp.gameObject);
  466. });
  467. _overlayTitle = null;
  468. }
  469. }
  470. protected override void OnConstructionStarted()
  471. {
  472. CIGTutorialManager instanceIfAvailable = SingletonMonobehaviour<CIGTutorialManager>.InstanceIfAvailable;
  473. if (instanceIfAvailable != null && instanceIfAvailable.ShouldOverrideBuildingConstructionTime(this))
  474. {
  475. constructionTime = 5;
  476. }
  477. base.OnConstructionStarted();
  478. IOngoingReward ongoingReward = Singleton<OngoingRewardManager>.Instance.FindActiveReward((IOngoingReward reward) => reward is FreeBuildingReward && ((FreeBuildingReward)reward).BuildingName == base.name);
  479. if (ongoingReward != null && ongoingReward.IsActive)
  480. {
  481. ((FreeBuildingReward)ongoingReward).Claim();
  482. }
  483. if (activatable)
  484. {
  485. return;
  486. }
  487. if (constructionXP.ContainsPositive("XP"))
  488. {
  489. GameState.EarnCurrencies(constructionXP, _currencyAnimationObject);
  490. Pling pling = _plingManager.Show(PlingType.XP, Vector3.zero, Clip.Ping);
  491. pling.Show(Localization.Integer(constructionXP.GetValue("XP")));
  492. }
  493. if (tile.gridSizeU * tile.gridSizeV > 1)
  494. {
  495. GameStats.AddGlobalBuildingsBuilt(1);
  496. if (initialPurchasePrice.ContainsPositive("Gold"))
  497. {
  498. GameStats.AddGoldBuilding();
  499. }
  500. }
  501. }
  502. protected override void OnConstructionCompleted()
  503. {
  504. base.OnConstructionCompleted();
  505. HideMovingArrow();
  506. CheckUpgradePossible();
  507. CIGVehicleManager cIGVehicleManager = UnityEngine.Object.FindObjectOfType<CIGVehicleManager>();
  508. if (cIGVehicleManager != null)
  509. {
  510. cIGVehicleManager.BuildingStateChanged(tile);
  511. }
  512. }
  513. protected override void OnDemolishStarted()
  514. {
  515. base.OnDemolishStarted();
  516. CIGVehicleManager cIGVehicleManager = UnityEngine.Object.FindObjectOfType<CIGVehicleManager>();
  517. if (cIGVehicleManager != null)
  518. {
  519. cIGVehicleManager.BuildingStateChanged(tile);
  520. }
  521. }
  522. protected override void OnDemolishCancelled()
  523. {
  524. base.OnDemolishCancelled();
  525. CIGVehicleManager cIGVehicleManager = UnityEngine.Object.FindObjectOfType<CIGVehicleManager>();
  526. if (cIGVehicleManager != null)
  527. {
  528. cIGVehicleManager.BuildingStateChanged(tile);
  529. }
  530. }
  531. public override ILocalizedString InfoText()
  532. {
  533. if (base.IsUpgrading)
  534. {
  535. double upgradeTimeLeft = base.UpgradeTimeLeft;
  536. int num = activatable ? (-1) : 0;
  537. return Localization.Concat(Localization.Format(Localization.Key("upgrading_to_level"), Localization.Integer(base.CurrentLevel + 1 + num)), Localization.LiteralNewLineString, Localization.Format(Localization.Key("upgrade_time_left"), Localization.TimeSpan(TimeSpan.FromSeconds(upgradeTimeLeft), hideSecondPartWhenZero: false)), Localization.LiteralDoubleNewLineString, Localization.Format(Localization.Key("building_speedup"), Localization.Integer(SpeedupCostGold), Localization.Integer(SpeedupCostCash)));
  538. }
  539. if (base.state == BuildingState.Constructing)
  540. {
  541. double constructionTimeLeft = base.ConstructionTimeLeft;
  542. return Localization.Concat(Localization.Format(Localization.Key("construction_time_left"), Localization.TimeSpan(TimeSpan.FromSeconds(constructionTimeLeft), hideSecondPartWhenZero: false)), Localization.LiteralDoubleNewLineString, Localization.Format(Localization.Key("building_speedup"), Localization.Integer(SpeedupCostGold), Localization.Integer(SpeedupCostCash)));
  543. }
  544. if (base.state == BuildingState.Demolishing)
  545. {
  546. double demolishTimeLeft = base.DemolishTimeLeft;
  547. return Localization.Concat(Localization.Format(Localization.Key("destroy_time_left"), Localization.TimeSpan(TimeSpan.FromSeconds(demolishTimeLeft), hideSecondPartWhenZero: false)), Localization.LiteralDoubleNewLineString, Localization.Format(Localization.Key("building_speedup"), Localization.Integer(SpeedupCostGold), Localization.Integer(SpeedupCostCash)));
  548. }
  549. if ((activatable && !Activated) || base.state == BuildingState.Preview)
  550. {
  551. ILocalizedString localizedString = Localization.EmptyLocalizedString;
  552. if (tile != null)
  553. {
  554. int requiredGridType = tile.requiredGridType;
  555. if (requiredGridType > 0 && requiredGridType < 10)
  556. {
  557. SurfaceType surfaceType = (SurfaceType)requiredGridType;
  558. string key = "surfacetype_" + surfaceType.ToString().ToLower().Replace("driedswamp", "swamp");
  559. localizedString = Localization.Concat(Localization.Format(Localization.Key("must_be_built_on"), Localization.Key(key)), Localization.LiteralNewLineString);
  560. }
  561. }
  562. localizedString = ((!activatable) ? Localization.Concat(localizedString, Localization.Key("construction_time"), Localization.LiteralSemiColonSpaceString, Localization.TimeSpan(TimeSpan.FromSeconds(constructionTime), hideSecondPartWhenZero: false), Localization.LiteralNewLineString) : Localization.Concat(localizedString, Localization.Format(Localization.Key("activation_cost"), UpgradeCost.LocalizedString(), Localization.Integer(activationTime)), Localization.LiteralNewLineString));
  563. if (!activatable && maxFacilities > 0)
  564. {
  565. int num2 = SingletonMonobehaviour<CIGGameStats>.Instance.NumberOf(base.CachedName);
  566. if (PurchasePrice.Contains("Cash"))
  567. {
  568. localizedString = Localization.Concat(localizedString, Localization.LiteralNewLineString, Localization.Format(Localization.Key("building_max_facilities"), Localization.Integer(maxFacilities)));
  569. }
  570. else if (num2 > 0)
  571. {
  572. localizedString = Localization.Concat(localizedString, Localization.LiteralNewLineString, Localization.Format(Localization.Key("building_costs_gold_max_facilities"), Localization.Integer(num2)));
  573. }
  574. }
  575. if (base.state == BuildingState.Preview && tile != null)
  576. {
  577. localizedString = Localization.Concat(localizedString, Localization.LiteralNewLineString, Localization.Key("size"), Localization.LiteralSemiColonSpaceString, Localization.Integer(tile.gridSizeU), Localization.Literal("x"), Localization.Integer(tile.gridSizeV));
  578. localizedString = Localization.Concat(localizedString, Localization.LiteralNewLineString, Localization.Key("size"), Localization.LiteralSemiColonSpaceString, Localization.Integer(tile.gridSizeU), Localization.Literal("x"), Localization.Integer(tile.gridSizeV));
  579. }
  580. if (constructionXP.ContainsPositive("XP"))
  581. {
  582. localizedString = Localization.Concat(localizedString, Localization.LiteralNewLineString, Localization.Key("experience"), Localization.Literal(": "), constructionXP.LocalizedString());
  583. }
  584. return localizedString;
  585. }
  586. return base.InfoText();
  587. }
  588. public override bool StartUpgrade()
  589. {
  590. Currencies cost = UpgradeCost;
  591. decimal extraCost = 0m;
  592. if (CanUpgrade)
  593. {
  594. Action action = delegate
  595. {
  596. cost += new Currencies("Gold", extraCost);
  597. GameState.SpendCurrencies(cost, true, delegate (bool succes, Currencies spent)
  598. {
  599. if (succes)
  600. {
  601. base.StartUpgrade();
  602. }
  603. });
  604. };
  605. if (SingletonMonobehaviour<CIGBuilderManager>.Instance.CurrentBuildCount < SingletonMonobehaviour<CIGBuilderManager>.Instance.MaxBuildCount)
  606. {
  607. action();
  608. return true;
  609. }
  610. extraCost = 1m;
  611. Currencies extraCost2 = new Currencies("Gold", extraCost);
  612. SingletonMonobehaviour<PopupManager>.Instance.ShowTooMuchParallelBuildsPopup(action, null, extraCost2);
  613. return false;
  614. }
  615. return false;
  616. }
  617. protected override void OnUpgradeStarted()
  618. {
  619. if (activatable && !Activated)
  620. {
  621. if (constructionXP.ContainsPositive("XP"))
  622. {
  623. GameState.EarnCurrencies(constructionXP, _currencyAnimationObject);
  624. Pling pling = _plingManager.Show(PlingType.XP, Vector3.zero, Clip.Ping);
  625. pling.Show(Localization.Integer(constructionXP.GetValue("XP")));
  626. }
  627. }
  628. else
  629. {
  630. GameStats.AddNumberOfUpgrades(1);
  631. }
  632. base.OnUpgradeStarted();
  633. CheckUpgradePossible();
  634. }
  635. protected override void OnUpgradeCompleted(double upgradedTime)
  636. {
  637. base.OnUpgradeCompleted(upgradedTime);
  638. if (base.CurrentLevel == 10)
  639. {
  640. GameStats.AddNumberOfLevel10Upgrades(1);
  641. }
  642. if (base.CurrentLevel == GetMaxLevel() && GetMaxLevel() > 1)
  643. {
  644. GameStats.GetCurrentIslandStats().MaxLevelBuildingCount++;
  645. }
  646. if (activatable && base.CurrentLevel == 1)
  647. {
  648. SetGreyShader(UseGreyShader);
  649. }
  650. CheckUpgradePossible();
  651. }
  652. private void SetGreyShader(bool enabled)
  653. {
  654. Material material;
  655. if (enabled)
  656. {
  657. material = new Material(Shader.Find("Sprites/Greyscale"));
  658. if (animator != null)
  659. {
  660. animator.enabled = false;
  661. }
  662. if (bottomAnimator != null)
  663. {
  664. bottomAnimator.enabled = false;
  665. }
  666. }
  667. else
  668. {
  669. material = new Material(Shader.Find("Sprites/Default"));
  670. if (animator != null)
  671. {
  672. animator.enabled = true;
  673. }
  674. if (bottomAnimator != null)
  675. {
  676. bottomAnimator.enabled = true;
  677. }
  678. }
  679. tile.spriteRenderer.material = material;
  680. if (tile.bottomRenderer != null)
  681. {
  682. tile.bottomRenderer.material = material;
  683. }
  684. }
  685. protected void ShowUpgradeSign()
  686. {
  687. if (_upgradeSign == null)
  688. {
  689. if (upgradeSignPrefab == null)
  690. {
  691. if (!errorSent)
  692. {
  693. string arg = (!(this != null)) ? "(null)" : base.name;
  694. UnityEngine.Debug.LogError($"Building {arg} has no upgradeSignPrefab");
  695. errorSent = true;
  696. }
  697. return;
  698. }
  699. _upgradeSign = UnityEngine.Object.Instantiate(upgradeSignPrefab);
  700. _upgradeSign.transform.parent = base.transform;
  701. tile.DidUpdateTransformEvent += DidUpdateTileTransform;
  702. }
  703. _upgradeSign.SetActive(value: true);
  704. _upgradeSign.transform.localPosition = Vector3.zero;
  705. SpriteRenderer component = _upgradeSign.GetComponent<SpriteRenderer>();
  706. if (component != null)
  707. {
  708. component.sortingOrder = tile.spriteRenderer.sortingOrder + 1;
  709. }
  710. }
  711. protected void HideUpgradeSign()
  712. {
  713. if (_upgradeSign != null)
  714. {
  715. _upgradeSign.SetActive(value: false);
  716. }
  717. }
  718. protected void DidUpdateTileTransform(GridTile tile)
  719. {
  720. if (_upgradeSign != null)
  721. {
  722. SpriteRenderer component = _upgradeSign.GetComponent<SpriteRenderer>();
  723. if (component != null)
  724. {
  725. component.sortingOrder = tile.spriteRenderer.sortingOrder + 1;
  726. }
  727. }
  728. }
  729. protected void OnGameStateValueChanged(string key, object oldValue, object newValue)
  730. {
  731. if (key == "EarnedBalance" || key == "GiftedBalance")
  732. {
  733. CheckUpgradePossible();
  734. }
  735. }
  736. protected void CheckUpgradePossible()
  737. {
  738. if (!SingletonMonobehaviour<CIGGameState>.IsAvailable || GameState.Level < 10)
  739. {
  740. HideUpgradeSign();
  741. return;
  742. }
  743. bool flag = false;
  744. if (base.state == BuildingState.Normal && (!activatable || Activated) && base.CurrentLevel < GetMaxLevel() && !base.IsUpgrading && CanUpgrade)
  745. {
  746. decimal value = GameState.Balance.GetValue("Cash");
  747. decimal value2 = UpgradeCost.GetValue("Cash");
  748. flag = (value2 <= value);
  749. }
  750. if (flag)
  751. {
  752. ShowUpgradeSign();
  753. }
  754. else
  755. {
  756. HideUpgradeSign();
  757. }
  758. }
  759. public void ShowMovingArrow()
  760. {
  761. _gridTileIconManager.SetIcon<GridTileIcon>(GridTileIconType.MovingArrow);
  762. }
  763. public void HideMovingArrow()
  764. {
  765. _gridTileIconManager.RemoveIcon(GridTileIconType.MovingArrow);
  766. }
  767. protected override void Awake()
  768. {
  769. base.Awake();
  770. constants = SingletonMonobehaviour<CIGGameConstants>.Instance;
  771. GameState.ValueChangedEvent += OnGameStateValueChanged;
  772. }
  773. protected override void OnDestroy()
  774. {
  775. base.OnDestroy();
  776. if (SingletonMonobehaviour<CIGGameState>.IsAvailable)
  777. {
  778. GameState.ValueChangedEvent -= OnGameStateValueChanged;
  779. }
  780. }
  781. protected override void OnGridTileStatusChanged(GridTile.Status oldStatus)
  782. {
  783. base.OnGridTileStatusChanged(oldStatus);
  784. if (tile.status == GridTile.Status.Moving || tile.status == GridTile.Status.Preview)
  785. {
  786. ShowMovingArrow();
  787. }
  788. else if (oldStatus == GridTile.Status.Moving || oldStatus == GridTile.Status.Preview)
  789. {
  790. HideMovingArrow();
  791. }
  792. }
  793. protected override void OnDeserialized()
  794. {
  795. base.OnDeserialized();
  796. if (UseGreyShader)
  797. {
  798. SetGreyShader(enabled: true);
  799. }
  800. CheckUpgradePossible();
  801. }
  802. }