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.
 
 
 

245 lines
6.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using CIG3.ExtensionMethods;
  5. using CIGEnums;
  6. using SUISS.Core;
  7. using SUISS.Scheduling;
  8. using SUISSEngine;
  9. using UnityEngine;
  10. public class CIGSceneryManager : MonoBehaviour
  11. {
  12. private void OnGridDeserialized()
  13. {
  14. if (++this.initializationCounter == 2)
  15. {
  16. SingletonMonobehaviour<Scheduler>.Instance.ExecuteNextFrame(base.gameObject, delegate()
  17. {
  18. this.PlaceInitialScenery();
  19. });
  20. }
  21. }
  22. private void OnDeserialized()
  23. {
  24. if (++this.initializationCounter == 2)
  25. {
  26. SingletonMonobehaviour<Scheduler>.Instance.ExecuteNextFrame(base.gameObject, delegate()
  27. {
  28. this.PlaceInitialScenery();
  29. });
  30. }
  31. }
  32. private DateTime SceneryTimestamp
  33. {
  34. get
  35. {
  36. return this._sceneryTimestamp;
  37. }
  38. set
  39. {
  40. this._sceneryTimestamp = value;
  41. this.serializing.Serialize();
  42. }
  43. }
  44. private bool InitialSceneryPlaced
  45. {
  46. get
  47. {
  48. return this._initialSceneryPlaced;
  49. }
  50. set
  51. {
  52. this._initialSceneryPlaced = value;
  53. this.serializing.Serialize();
  54. }
  55. }
  56. private void OnSerialize(Dictionary<string, object> dict)
  57. {
  58. dict["SceneryTimestamp"] = this._sceneryTimestamp.ToString("yyyyMMddHHmmss");
  59. dict["InitialSceneryIsPlaced"] = this._initialSceneryPlaced;
  60. }
  61. private void OnDeserialize(Dictionary<string, object> dict)
  62. {
  63. if (dict.ContainsKey("SceneryTimestamp"))
  64. {
  65. CultureInfo invariantCulture = CultureInfo.InvariantCulture;
  66. this._sceneryTimestamp = DateTime.ParseExact((string)dict["SceneryTimestamp"], "yyyyMMddHHmmss", invariantCulture);
  67. }
  68. if (dict.ContainsKey("InitialSceneryIsPlaced"))
  69. {
  70. this._initialSceneryPlaced = (bool)dict["InitialSceneryIsPlaced"];
  71. }
  72. }
  73. protected void LoadSceneryPrefabs()
  74. {
  75. bool[] availableSurfaceTypes = this.grid.AvailableSurfaceTypes;
  76. bool flag = false;
  77. for (int i = 1; i < availableSurfaceTypes.Length; i++)
  78. {
  79. if (availableSurfaceTypes[i] && ((SurfaceType)i).IsLand())
  80. {
  81. flag = true;
  82. break;
  83. }
  84. }
  85. List<GameObject> additionalPrefabs = SingletonMonobehaviour<GameObjectManager>.Instance.AdditionalPrefabs;
  86. int count = SingletonMonobehaviour<GameObjectManager>.Instance.AdditionalPrefabs.Count;
  87. for (int j = 0; j < count; j++)
  88. {
  89. GameObject gameObject = additionalPrefabs[j];
  90. CIGScenery component = gameObject.GetComponent<CIGScenery>();
  91. if (component != null)
  92. {
  93. if (!component.island.IsValid() || component.island == this.cityIsland.island)
  94. {
  95. GridTile tile = component.tile;
  96. int requiredGridType = tile.requiredGridType;
  97. if ((requiredGridType == 10 && flag) || (requiredGridType < availableSurfaceTypes.Length && availableSurfaceTypes[requiredGridType]))
  98. {
  99. this.sceneryPrefabs.Add(gameObject);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. private void PlaceInitialScenery()
  106. {
  107. if (this.InitialSceneryPlaced)
  108. {
  109. return;
  110. }
  111. this.LoadSceneryPrefabs();
  112. List<GridIndex> list = new List<GridIndex>(this.expansions.blockSizeU * this.expansions.blockSizeV);
  113. foreach (CIGExpansionDefinition cigexpansionDefinition in this.expansions.blockDefinitions)
  114. {
  115. list.Clear();
  116. int num = cigexpansionDefinition.sceneryItems;
  117. for (int j = 0; j < this.expansions.blockSizeV; j++)
  118. {
  119. for (int k = 0; k < this.expansions.blockSizeU; k++)
  120. {
  121. GridIndex gridIndex = new GridIndex(k + this.expansions.blockSizeU * cigexpansionDefinition.u, j + this.expansions.blockSizeV * cigexpansionDefinition.v);
  122. GridElement gridElement = this.grid[gridIndex];
  123. if (this.HavePrefabForType(gridElement.Type))
  124. {
  125. if (gridElement.Tile != null)
  126. {
  127. if (gridElement.Tile.GetComponent<CIGScenery>() != null)
  128. {
  129. num--;
  130. }
  131. }
  132. else
  133. {
  134. list.Add(gridIndex);
  135. }
  136. }
  137. }
  138. }
  139. list.Shuffle<GridIndex>();
  140. int num2 = 0;
  141. while (num2 < list.Count && num2 < cigexpansionDefinition.sceneryItems)
  142. {
  143. GridIndex index = list[num2];
  144. GridElement gridElement2 = this.grid[index];
  145. int type = gridElement2.Type;
  146. GameObject prefabForType = this.GetPrefabForType(type);
  147. GridTile component = prefabForType.GetComponent<GridTile>();
  148. bool mirrored = component.canMirror && UnityEngine.Random.value >= 0.5f;
  149. this.island.builder.BuildAt(prefabForType, index, mirrored, true);
  150. num2++;
  151. }
  152. }
  153. this._initialSceneryPlaced = true;
  154. this.SceneryTimestamp = DateTime.UtcNow;
  155. }
  156. private bool HavePrefabForType(int type)
  157. {
  158. return this.GetPrefabForType(type) != null;
  159. }
  160. private GameObject GetPrefabForType(int type)
  161. {
  162. if (!this.availablePrefabs.ContainsKey(type))
  163. {
  164. List<GameObject> list = new List<GameObject>();
  165. foreach (GameObject gameObject in this.sceneryPrefabs)
  166. {
  167. CIGScenery component = gameObject.GetComponent<CIGScenery>();
  168. GridTile tile = component.tile;
  169. bool flag = false;
  170. if (this.exclusiveElementType != (SurfaceType)type && tile.requiredGridType == 10)
  171. {
  172. if (((SurfaceType)type).IsLand())
  173. {
  174. flag = true;
  175. }
  176. }
  177. else if (tile.requiredGridType == type)
  178. {
  179. flag = true;
  180. }
  181. if (flag)
  182. {
  183. if (component.multiple)
  184. {
  185. list = new List<GameObject>
  186. {
  187. gameObject
  188. };
  189. break;
  190. }
  191. list.Add(gameObject);
  192. }
  193. }
  194. this.availablePrefabs[type] = list;
  195. }
  196. int count = this.availablePrefabs[type].Count;
  197. if (count == 0)
  198. {
  199. return null;
  200. }
  201. return this.availablePrefabs[type][UnityEngine.Random.Range(0, count)];
  202. }
  203. [SelfReference]
  204. public IsometricIsland island;
  205. [SelfReference]
  206. public CityIsland cityIsland;
  207. [ChildReference]
  208. public IsometricGrid grid;
  209. [SelfReference]
  210. public Serializing serializing;
  211. [SelfReference]
  212. public CIGExpansions expansions;
  213. public SurfaceType exclusiveElementType = (SurfaceType)(-1);
  214. public const string SceneryTimestampKey = "SceneryTimestamp";
  215. public const string InitialSceneryPlacedKey = "InitialSceneryIsPlaced";
  216. protected List<GameObject> sceneryPrefabs = new List<GameObject>();
  217. protected int initializationCounter;
  218. private Dictionary<int, List<GameObject>> availablePrefabs = new Dictionary<int, List<GameObject>>();
  219. private DateTime _sceneryTimestamp = DateTime.MinValue;
  220. private bool _initialSceneryPlaced;
  221. }