您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 

204 行
5.0 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CIG.Translation;
  5. using CIG3.ExtensionMethods;
  6. using CIGEnums;
  7. using SUISS.Core;
  8. using SUISS.Scheduling;
  9. using SUISS.Storage;
  10. using SUISSEngine;
  11. using UnityEngine;
  12. public class CIGPort : CIGCommercialBuilding
  13. {
  14. protected static double GetGoldSpawnTimestamp()
  15. {
  16. Dictionary<string, object> dictionary = Storage.Get(StorageLifecycle.Game).GetDictionary("RabbitHole");
  17. if (dictionary.ContainsKey("BunnyTime"))
  18. {
  19. return dictionary.GetValue("BunnyTime", Timing.UtcNow) + 28800.0;
  20. }
  21. double utcNow = Timing.UtcNow;
  22. dictionary["BunnyTime"] = utcNow;
  23. return utcNow + 28800.0;
  24. }
  25. protected static void SetGoldSpawnTimestamp()
  26. {
  27. Storage.Get(StorageLifecycle.Game).GetDictionary("RabbitHole")["BunnyTime"] = Timing.UtcNow;
  28. }
  29. protected override void OnSerialize(Dictionary<string, object> values)
  30. {
  31. base.OnSerialize(values);
  32. values["LastCollectTime"] = this._lastCollectTime;
  33. values["Spawned"] = this._haveGold;
  34. }
  35. protected override void OnDeserialize(Dictionary<string, object> values)
  36. {
  37. base.OnDeserialize(values);
  38. this._lastCollectTime = values.GetValue("LastCollectTime", 0.0);
  39. this._haveGold = values.GetValue("Spawned", false);
  40. }
  41. protected override void OnDeserialized()
  42. {
  43. base.OnDeserialized();
  44. this.CreateVehicle();
  45. if ((!this.activatable || this.Activated) && Timing.UtcNow - this._lastCollectTime < 60.0)
  46. {
  47. this.Launch();
  48. }
  49. }
  50. protected override void OnUpgradeCompleted(double upgradedTime)
  51. {
  52. base.OnUpgradeCompleted(upgradedTime);
  53. if (this.activatable && base.CurrentLevel == 1)
  54. {
  55. this.Launch();
  56. }
  57. }
  58. protected override IEnumerator WaitForProfit()
  59. {
  60. yield return null;
  61. while (this._vehicle == null || !this._vehicle.IsIdle())
  62. {
  63. yield return Timing.time + 0.5;
  64. }
  65. yield break;
  66. }
  67. protected override void ShowProfitIcon(bool maxed)
  68. {
  69. if (!this._haveGold && UnityEngine.Random.value > 0.3f && CIGPort.GetGoldSpawnTimestamp() < Timing.UtcNow)
  70. {
  71. CIGPort.SetGoldSpawnTimestamp();
  72. this._haveGold = true;
  73. this.serializing.Serialize();
  74. }
  75. if (this._haveGold)
  76. {
  77. ButtonGridTileIcon buttonGridTileIcon = this._gridTileIconManager.SetIcon<ButtonGridTileIcon>(GridTileIconType.GoldProfit);
  78. buttonGridTileIcon.Init(delegate
  79. {
  80. base.ClickProfitIcon(base.gameObject);
  81. });
  82. }
  83. else
  84. {
  85. base.ShowProfitIcon(maxed);
  86. }
  87. }
  88. protected override void CollectProfit(Currencies profit, bool maxed)
  89. {
  90. this.Launch();
  91. if (this._haveGold)
  92. {
  93. string text = "XP";
  94. if (profit.ContainsPositive(text))
  95. {
  96. profit = new Currencies(new object[]
  97. {
  98. "Gold",
  99. 1m,
  100. text,
  101. profit.GetValue(text)
  102. });
  103. }
  104. else
  105. {
  106. profit = new Currencies("Gold", 1m);
  107. }
  108. if (SingletonMonobehaviour<CIGGameStats>.IsAvailable)
  109. {
  110. SingletonMonobehaviour<CIGGameStats>.Instance.IncrementGoldEarned_Ports();
  111. }
  112. this._haveGold = false;
  113. }
  114. base.CollectProfit(profit, maxed);
  115. this._lastCollectTime = Timing.UtcNow;
  116. this.serializing.Serialize();
  117. }
  118. protected override bool HasTimeLeft
  119. {
  120. get
  121. {
  122. return this._vehicle == null || !this._vehicle.IsIdle();
  123. }
  124. }
  125. public override ILocalizedString TimeLeftString()
  126. {
  127. if (this._vehicle == null || !this._vehicle.IsIdle())
  128. {
  129. return Localization.Key("port_wait_for_boat");
  130. }
  131. return Localization.Format(Localization.Key("second_symbol"), new ILocalizedString[]
  132. {
  133. Localization.Integer(0)
  134. });
  135. }
  136. protected void CreateVehicle()
  137. {
  138. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.fishingBoatPrefab);
  139. this._vehicle = gameObject.GetComponent<ScriptedVehicle>();
  140. if (this._vehicle == null)
  141. {
  142. UnityEngine.Debug.LogError("Failed to instantiate a the boat.");
  143. UnityEngine.Object.Destroy(gameObject);
  144. return;
  145. }
  146. if (this.pathOverride != null && this.pathOverride.Length > 0)
  147. {
  148. this._vehicle.path = this.pathOverride;
  149. }
  150. this._vehicle.idleDirection = this.idleDirectionOverride;
  151. this._vehicle.transform.parent = this.tile.grid.transform;
  152. GridIndex index = this.tile.element.Index;
  153. index.u += this.vehicleStartOffsetU;
  154. index.v += this.vehicleStartOffsetV;
  155. this._vehicle.SpawnAt(index, 0);
  156. }
  157. protected void Launch()
  158. {
  159. if (this._vehicle != null)
  160. {
  161. this._vehicle.Launch();
  162. }
  163. }
  164. public GameObject fishingBoatPrefab;
  165. public Vehicle.Direction[] pathOverride;
  166. public Vehicle.Direction idleDirectionOverride;
  167. public int vehicleStartOffsetU;
  168. public int vehicleStartOffsetV;
  169. protected const string LastCollectTimeKey = "LastCollectTime";
  170. protected const string GoldSpawnedKey = "Spawned";
  171. protected const string GoldSpawnDictionaryKey = "RabbitHole";
  172. protected const string GoldSpawnTimestampKey = "BunnyTime";
  173. protected const double GoldSpawnInterval_Seconds = 28800.0;
  174. protected ScriptedVehicle _vehicle;
  175. protected double _lastCollectTime;
  176. protected bool _haveGold;
  177. }