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.
 
 
 

264 lines
6.7 KiB

  1. using System;
  2. using CIG3.ExtensionMethods;
  3. using CIGEnums;
  4. using SUISS.Core;
  5. using SUISSEngine;
  6. using UnityEngine;
  7. public class CIGBuilder : Builder
  8. {
  9. public static Color AvailableElementColor
  10. {
  11. get
  12. {
  13. return CIGBuilder.availableElementColor;
  14. }
  15. }
  16. public static Color OccupiedElementColor
  17. {
  18. get
  19. {
  20. return CIGBuilder.occupiedElementColor;
  21. }
  22. }
  23. protected override void OnGridDeserialized()
  24. {
  25. base.OnGridDeserialized();
  26. this.ResetAllColors();
  27. base.OnTileBuild += SingletonMonobehaviour<CIGGameStats>.Instance.OnTileBuilt;
  28. base.OnTileDestroyed += SingletonMonobehaviour<CIGGameStats>.Instance.OnTileDestroyed;
  29. }
  30. protected virtual void OnDestroy()
  31. {
  32. if (SingletonMonobehaviour<CIGGameStats>.IsAvailable)
  33. {
  34. base.OnTileBuild -= SingletonMonobehaviour<CIGGameStats>.Instance.OnTileBuilt;
  35. base.OnTileDestroyed -= SingletonMonobehaviour<CIGGameStats>.Instance.OnTileDestroyed;
  36. }
  37. }
  38. public override bool TilesHidden
  39. {
  40. get
  41. {
  42. return base.TilesHidden;
  43. }
  44. set
  45. {
  46. bool tilesHidden = base.TilesHidden;
  47. base.TilesHidden = value;
  48. if (base.TilesHidden != tilesHidden)
  49. {
  50. this.ResetAllColors();
  51. }
  52. }
  53. }
  54. public void ExpansionBlockDidUnlock(CIGExpansions expansions, CIGExpansions.ExpansionBlock block)
  55. {
  56. for (int i = block.Origin.v; i < block.Origin.v + block.Size.v; i++)
  57. {
  58. for (int j = block.Origin.u; j < block.Origin.u + block.Size.u; j++)
  59. {
  60. GridElement gridElement = this.grid[j, i];
  61. gridElement.Color = this.GetColorForElement(true, gridElement);
  62. gridElement.Unlocked = true;
  63. expansions.DidUnlockTileWithType(gridElement.Type);
  64. }
  65. }
  66. }
  67. protected override bool IsElementTypeCompatible(int type, int required)
  68. {
  69. return base.IsElementTypeCompatible(type, required) || (required == 10 && ((SurfaceType)type).IsLand());
  70. }
  71. protected override void DidBuildTile(GridTile tile, GridIndex index, GridSize size, bool mirrored)
  72. {
  73. base.DidBuildTile(tile, index, size, mirrored);
  74. for (int i = index.v; i > index.v - size.v; i--)
  75. {
  76. for (int j = index.u; j > index.u - size.u; j--)
  77. {
  78. GridElement gridElement = this.grid[j, i];
  79. CIGExpansions.ExpansionBlock blockForIndex = this.expansions.GetBlockForIndex(new GridIndex(j, i));
  80. if (blockForIndex != null)
  81. {
  82. gridElement.Color = this.GetColorForElement(blockForIndex.Unlocked, gridElement);
  83. }
  84. }
  85. }
  86. }
  87. protected override void DidDestroyTile(GridTile tile, GridIndex index, GridSize size)
  88. {
  89. base.DidDestroyTile(tile, index, size);
  90. if (index.isInvalid)
  91. {
  92. return;
  93. }
  94. for (int i = index.v; i > index.v - size.v; i--)
  95. {
  96. for (int j = index.u; j > index.u - size.u; j--)
  97. {
  98. GridElement gridElement = this.grid[j, i];
  99. CIGExpansions.ExpansionBlock blockForIndex = this.expansions.GetBlockForIndex(new GridIndex(j, i));
  100. if (blockForIndex != null)
  101. {
  102. gridElement.Color = this.GetColorForElement(blockForIndex.Unlocked, gridElement);
  103. }
  104. }
  105. }
  106. }
  107. protected override bool CanBuildTileOnElement(GridTile tile, IsometricGrid grid, GridElement element)
  108. {
  109. bool flag = base.CanBuildTileOnElement(tile, grid, element);
  110. if (flag)
  111. {
  112. CIGExpansions.ExpansionBlock blockForIndex = this.expansions.GetBlockForIndex(element.Index);
  113. if (blockForIndex == null || !blockForIndex.Unlocked)
  114. {
  115. flag = false;
  116. }
  117. }
  118. return flag;
  119. }
  120. protected override bool CanBuildTile(GridTile tile, IsometricGrid grid, GridIndex index, bool mirrored, bool forced)
  121. {
  122. if (index.isInvalid || index.u >= grid.Size.u || index.v >= grid.Size.v)
  123. {
  124. return false;
  125. }
  126. if (mirrored && !tile.canMirror)
  127. {
  128. return false;
  129. }
  130. GridSize size = tile.size;
  131. if (mirrored)
  132. {
  133. size = new GridSize(size.v, size.u);
  134. }
  135. if (size.u > index.u + 1 || size.v > index.v + 1)
  136. {
  137. return false;
  138. }
  139. int num = 0;
  140. for (int i = index.v; i > index.v - size.v; i--)
  141. {
  142. for (int j = index.u; j > index.u - size.u; j--)
  143. {
  144. GridElement gridElement = grid[j, i];
  145. if (gridElement.Tile != null)
  146. {
  147. if (!forced || !(tile.GetComponent<Scenery>() == null))
  148. {
  149. return false;
  150. }
  151. if (gridElement.Tile.GetComponent<Scenery>() == null)
  152. {
  153. UnityEngine.Debug.LogWarning(string.Format("In order to build {0} at {1}, we need to remove {2} at ({3},{4})", new object[]
  154. {
  155. tile.name,
  156. index,
  157. gridElement.Tile.name,
  158. j,
  159. i
  160. }));
  161. }
  162. base.DestroyTile(gridElement.Tile);
  163. }
  164. if (!forced)
  165. {
  166. if (this.IsElementTypeCompatible(gridElement.Type, tile.requiredGridType))
  167. {
  168. num++;
  169. }
  170. if (!this.CanBuildTileOnElement(tile, grid, gridElement))
  171. {
  172. return false;
  173. }
  174. }
  175. }
  176. }
  177. return forced || num == tile.size.u * tile.size.v;
  178. }
  179. private void ResetAllColors()
  180. {
  181. GridSize size = this.grid.Size;
  182. for (int i = 0; i < size.v; i++)
  183. {
  184. for (int j = 0; j < size.u; j++)
  185. {
  186. GridElement gridElement = this.grid[j, i];
  187. if (gridElement != null)
  188. {
  189. CIGExpansions.ExpansionBlock blockForIndex = this.expansions.GetBlockForIndex(new GridIndex(j, i));
  190. if (blockForIndex != null)
  191. {
  192. gridElement.Color = this.GetColorForElement(blockForIndex.Unlocked, gridElement);
  193. }
  194. }
  195. }
  196. }
  197. }
  198. private Color GetColorForElement(bool unlocked, GridElement element)
  199. {
  200. if (!unlocked || element.Type < 0)
  201. {
  202. return GridOverlay.TransparentColor;
  203. }
  204. GridTile tile = element.Tile;
  205. if (tile == null)
  206. {
  207. Color a = new Color(1f, 1f, 1f, 1f);
  208. if (this.showOverlayColors)
  209. {
  210. if (element.Type >= 0 && element.Type < CIGBuilder.elementTypeColors.Length)
  211. {
  212. a = CIGBuilder.elementTypeColors[element.Type];
  213. }
  214. }
  215. else if (element.Type <= 0)
  216. {
  217. return GridOverlay.TransparentColor;
  218. }
  219. return a * CIGBuilder.availableElementColor;
  220. }
  221. if ((this.TilesHidden && tile.GetComponent<Road>() == null) || (this.roadsHidden && tile.GetComponent<Road>() != null))
  222. {
  223. return CIGBuilder.occupiedElementColor;
  224. }
  225. return GridOverlay.TransparentColor;
  226. }
  227. private static Color availableElementColor = new Color(1f, 1f, 1f, 0.15f);
  228. private static Color occupiedElementColor = new Color(0.51f, 0.428f, 1f, 1f);
  229. private static Color[] elementTypeColors = new Color[]
  230. {
  231. new Color(1f, 1f, 1f, 0f),
  232. new Color(1f, 1f, 1f, 1f),
  233. new Color(1f, 1f, 1f, 1f),
  234. new Color(1f, 1f, 1f, 1f),
  235. new Color(1f, 1f, 1f, 1f),
  236. new Color(1f, 1f, 1f, 1f),
  237. new Color(1f, 1f, 1f, 1f),
  238. new Color(1f, 1f, 1f, 1f),
  239. new Color(1f, 1f, 1f, 1f),
  240. new Color(1f, 1f, 1f, 1f)
  241. };
  242. [SelfReference]
  243. public CIGExpansions expansions;
  244. public bool showOverlayColors = true;
  245. }