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.
 
 
 

256 lines
7.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using CIG.Translation;
  4. using CIG3.ExtensionMethods;
  5. using CIGEnums;
  6. using SUISSEngine;
  7. using UnityEngine;
  8. public class CIGUnlockBuilding : CIGBuilding
  9. {
  10. protected GridIndex UnlockTileIndex
  11. {
  12. get
  13. {
  14. GridIndex index = this.tile.index;
  15. index.u += this.unlockTileOffsetU;
  16. index.v += this.unlockTileOffsetV;
  17. return index;
  18. }
  19. }
  20. public virtual void OnDrawGizmosSelected()
  21. {
  22. if (IsometricIsland.Current != null && this.VehiclePath.Length > 0)
  23. {
  24. Vector3 position = IsometricIsland.Current.grid.transform.position;
  25. Vector3 b = IsometricIsland.Current.grid.GetPositionForGridPoint(new GridPoint((float)this.gridPositionU, (float)this.gridPositionV));
  26. Vector3 vector = IsometricIsland.Current.grid.GetPositionForGridPoint(new GridPoint(this.VehiclePath[0].x, this.VehiclePath[0].y));
  27. if (this.FlyingHeight.Length > 0)
  28. {
  29. Vector3 a = vector;
  30. Vector3 vector2 = new Vector3(0f, 1f);
  31. vector = a + vector2.normalized * this.FlyingHeight[0];
  32. }
  33. vector += position + b;
  34. for (int i = 1; i < this.VehiclePath.Length; i++)
  35. {
  36. Vector3 vector3 = IsometricIsland.Current.grid.GetPositionForGridPoint(new GridPoint(this.VehiclePath[i].x, this.VehiclePath[i].y));
  37. vector3 += position + b;
  38. if (i < this.FlyingHeight.Length)
  39. {
  40. Vector3 a2 = vector3;
  41. Vector3 vector4 = new Vector3(0f, 1f);
  42. vector3 = a2 + vector4.normalized * this.FlyingHeight[i];
  43. }
  44. Gizmos.DrawLine(vector, vector3);
  45. vector = vector3;
  46. }
  47. }
  48. }
  49. protected override void OnDeserialized()
  50. {
  51. base.OnDeserialized();
  52. bool flag = false;
  53. if (base.GameState.IsIslandUnlocked(this.unlocksIsland))
  54. {
  55. GridIndex unlockTileIndex = this.UnlockTileIndex;
  56. for (int i = unlockTileIndex.u; i > unlockTileIndex.u - this.tile.size.u; i--)
  57. {
  58. for (int j = unlockTileIndex.v; j > unlockTileIndex.v - this.tile.size.v; j--)
  59. {
  60. GridIndex index = new GridIndex(i, j);
  61. CIGExpansions.ExpansionBlock blockForIndex = this.expansions.reference.GetBlockForIndex(index);
  62. flag |= (blockForIndex != null);
  63. if (blockForIndex != null && !blockForIndex.Unlocked)
  64. {
  65. blockForIndex.CanUnlock = true;
  66. blockForIndex.Unlock(Currencies.Zero);
  67. }
  68. }
  69. }
  70. }
  71. if (!this.Activated)
  72. {
  73. if (base.GameState.IsIslandUnlocked(this.unlocksIsland))
  74. {
  75. if (!flag)
  76. {
  77. UnityEngine.Debug.LogError(string.Format("{1}: no blocks close to {0}! (key is {2})", this.UnlockTileIndex, base.name, this.serializing.StorageKey));
  78. }
  79. base.PerformUpgrade();
  80. this.SpawnVehicle();
  81. IsometricIsland parent = IsometricIsland.GetParent(this);
  82. if (!parent.HasCenteredViewOnce)
  83. {
  84. parent.CenterCameraOnBuilding(this);
  85. }
  86. }
  87. }
  88. else
  89. {
  90. this.SpawnVehicle();
  91. }
  92. }
  93. public override bool CanUpgrade
  94. {
  95. get
  96. {
  97. CIGExpansions.ExpansionBlock blockForIndex = this.expansions.reference.GetBlockForIndex(this.UnlockTileIndex);
  98. return (blockForIndex == null || blockForIndex.Unlocked) && (!this.checkForRoad || this.HasRoad) && base.CanUpgrade;
  99. }
  100. }
  101. public override ILocalizedString ReasonWhyCantUpgrade(out ILocalizedString title)
  102. {
  103. CIGExpansions reference = this.expansions.reference;
  104. if (reference != null)
  105. {
  106. CIGExpansions.ExpansionBlock blockForIndex = reference.GetBlockForIndex(this.UnlockTileIndex);
  107. if (blockForIndex != null && !blockForIndex.Unlocked)
  108. {
  109. title = ((!this.Activated) ? Localization.Key("steps_to_complete_to_activate_this_building") : Localization.Key("steps_to_complete_to_be_able_to_upgrade_this_building"));
  110. return Localization.Key("buy_expansion_first");
  111. }
  112. }
  113. if (this.checkForRoad && !this.HasRoad)
  114. {
  115. title = ((!this.Activated) ? Localization.Key("steps_to_complete_to_activate_this_building") : Localization.Key("steps_to_complete_to_be_able_to_upgrade_this_building"));
  116. return Localization.Key("building_needs_road");
  117. }
  118. return base.ReasonWhyCantUpgrade(out title);
  119. }
  120. public override bool InfoRequiresFrequentRefresh
  121. {
  122. get
  123. {
  124. return base.IsUpgrading && base.InfoRequiresFrequentRefresh;
  125. }
  126. }
  127. public override bool CanSpeedup
  128. {
  129. get
  130. {
  131. return base.IsUpgrading && base.CanSpeedup;
  132. }
  133. }
  134. public override ILocalizedString InfoText()
  135. {
  136. if (base.IsUpgrading)
  137. {
  138. return base.InfoText();
  139. }
  140. if (this.Activated)
  141. {
  142. return Localization.Format(Localization.Key("you_can_go_to"), new ILocalizedString[]
  143. {
  144. this.unlocksIsland.GetDisplayName()
  145. });
  146. }
  147. return Localization.Format(Localization.Key("unlocks_island"), new ILocalizedString[]
  148. {
  149. this.unlocksIsland.GetDisplayName()
  150. });
  151. }
  152. public override List<BuildingProperty> ShownProperties
  153. {
  154. get
  155. {
  156. List<BuildingProperty> list = base.ShownProperties;
  157. if (list == null)
  158. {
  159. list = new List<BuildingProperty>();
  160. }
  161. if (!base.IsUpgrading)
  162. {
  163. list.Add(BuildingProperty.Text);
  164. }
  165. return list;
  166. }
  167. }
  168. protected override void OnUpgradeCompleted(double upgradedTime)
  169. {
  170. base.OnUpgradeCompleted(upgradedTime);
  171. if (base.CurrentLevel == 1)
  172. {
  173. base.GameState.SetIslandUnlocked(this.unlocksIsland);
  174. base.GameState.SetConnectionUnlocked(this.island, this.unlocksIsland);
  175. }
  176. base.IslandState.AddTransport(50);
  177. this.SpawnVehicle();
  178. }
  179. protected override bool UseGreyShader
  180. {
  181. get
  182. {
  183. return false;
  184. }
  185. }
  186. private void SpawnVehicle()
  187. {
  188. if (this.VehiclePrefab != null)
  189. {
  190. Transform transform = base.transform;
  191. CIGUnlockBuildingVehicleManager cigunlockBuildingVehicleManager = null;
  192. while (cigunlockBuildingVehicleManager == null && transform != null)
  193. {
  194. cigunlockBuildingVehicleManager = transform.gameObject.GetComponent<CIGUnlockBuildingVehicleManager>();
  195. transform = transform.parent;
  196. }
  197. if (cigunlockBuildingVehicleManager == null)
  198. {
  199. UnityEngine.Debug.LogError("Can't find CIGUnlockBuildingVehicleManager in parent. I need it to spawn my vehicle.");
  200. }
  201. else
  202. {
  203. GridPoint[] array = new GridPoint[this.VehiclePath.Length];
  204. for (int i = 0; i < this.VehiclePath.Length; i++)
  205. {
  206. array[i] = new GridPoint(this.VehiclePath[i].x, this.VehiclePath[i].y) + new GridPoint((float)this.gridPositionU, (float)this.gridPositionV);
  207. }
  208. cigunlockBuildingVehicleManager.SpawnVehicleAt(new CIGUnlockBuilding.SVehicleSpawnInfo
  209. {
  210. path = array,
  211. vehiclePrefab = this.VehiclePrefab,
  212. unlockBuilding = this,
  213. flyingHeight = this.FlyingHeight
  214. });
  215. }
  216. }
  217. }
  218. public Island unlocksIsland;
  219. public int unlockTileOffsetU;
  220. public int unlockTileOffsetV;
  221. public GameObject VehiclePrefab;
  222. public Vector2[] VehiclePath;
  223. public float[] FlyingHeight;
  224. private MagicalReference<CIGExpansions> expansions = new MagicalReference<CIGExpansions>();
  225. public struct SVehicleSpawnInfo
  226. {
  227. public GameObject vehiclePrefab;
  228. public CIGUnlockBuilding unlockBuilding;
  229. public GridPoint[] path;
  230. public float[] flyingHeight;
  231. }
  232. }