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.
 
 
 

342 lines
7.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using SUISS.Core;
  5. using SUISS.Storage;
  6. using UnityEngine;
  7. namespace SUISSEngine
  8. {
  9. public class IsometricIsland : MonoBehaviour
  10. {
  11. public IsometricIsland()
  12. {
  13. this._buildingsOnIsland = new List<Building>();
  14. this.hasCenteredViewOnce = false;
  15. }
  16. public static IsometricIsland GetParent(Component node)
  17. {
  18. return node.GetComponentInParent<IsometricIsland>();
  19. }
  20. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  21. public static event IsometricIsland.IslandLoadedEventHandler IslandLoadedEvent;
  22. protected static void FireIslandLoadedEvent(IsometricIsland island)
  23. {
  24. if (IsometricIsland.IslandLoadedEvent != null)
  25. {
  26. IsometricIsland.IslandLoadedEvent(island);
  27. }
  28. }
  29. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  30. public static event IsometricIsland.IslandUnloadedEventHandler IslandUnloadedEvent;
  31. protected static void FireIslandUnloadedEvent()
  32. {
  33. if (IsometricIsland.IslandUnloadedEvent != null)
  34. {
  35. IsometricIsland.IslandUnloadedEvent();
  36. }
  37. }
  38. public List<Building> BuildingsOnIsland
  39. {
  40. get
  41. {
  42. return this._buildingsOnIsland;
  43. }
  44. }
  45. public Bounds islandWorldBounds
  46. {
  47. get
  48. {
  49. Vector3 center = base.transform.TransformPoint(this.islandBounds.center);
  50. Vector3 size = base.transform.TransformDirection(this.islandBounds.size);
  51. return new Bounds(center, size);
  52. }
  53. }
  54. public IslandInput input
  55. {
  56. get
  57. {
  58. if (this._input == null)
  59. {
  60. this._input = UnityEngine.Object.FindObjectOfType<IslandInput>();
  61. }
  62. return this._input;
  63. }
  64. }
  65. public GameObjectManager gameobjectManager
  66. {
  67. get
  68. {
  69. if (this._goManager == null)
  70. {
  71. this._goManager = SingletonMonobehaviour<GameObjectManager>.Instance;
  72. }
  73. return this._goManager;
  74. }
  75. }
  76. public CIGGameState gameState
  77. {
  78. get
  79. {
  80. if (this._gameState == null)
  81. {
  82. this._gameState = SingletonMonobehaviour<CIGGameState>.Instance;
  83. }
  84. return this._gameState;
  85. }
  86. }
  87. public CIGDropChestManager DropChestManager
  88. {
  89. get
  90. {
  91. return this._dropChestManager;
  92. }
  93. }
  94. public CIGFloatingChestManager FloatingChestManager
  95. {
  96. get
  97. {
  98. return this._floatingChestManager;
  99. }
  100. }
  101. public float dpi
  102. {
  103. get
  104. {
  105. return this._dpi;
  106. }
  107. }
  108. public bool isPausing
  109. {
  110. get
  111. {
  112. return this._isPausing;
  113. }
  114. }
  115. public bool HasCenteredViewOnce
  116. {
  117. get
  118. {
  119. return this.hasCenteredViewOnce;
  120. }
  121. }
  122. private void HandleGridDeserializedEvent(UnityEngine.Object sender)
  123. {
  124. this.builder.OnTileBuild += this.HandleOnTileBuild;
  125. this.builder.OnTileDestroyed += this.HandleOnTileDestroyed;
  126. this._buildingsOnIsland = new List<Building>(this.grid.GetComponentsInChildren<Building>());
  127. }
  128. private void HandleOnTileDestroyed(GridTile tile, GridTile.Status priorStatus)
  129. {
  130. Building component = tile.GetComponent<Building>();
  131. if (component == null || !this._buildingsOnIsland.Contains(component))
  132. {
  133. if (component != null)
  134. {
  135. UnityEngine.Debug.LogWarning("The buildings list doesnt contain the destroyed Building");
  136. }
  137. return;
  138. }
  139. this._buildingsOnIsland.Remove(component);
  140. }
  141. private void HandleOnTileBuild(GridTile tile)
  142. {
  143. Building component = tile.GetComponent<Building>();
  144. if (component == null || this._buildingsOnIsland.Contains(component))
  145. {
  146. return;
  147. }
  148. this._buildingsOnIsland.Add(component);
  149. }
  150. private void Awake()
  151. {
  152. this._isPausing = false;
  153. if (this.islandCamera == null)
  154. {
  155. Camera[] componentsInChildren = base.gameObject.GetComponentsInChildren<Camera>(false);
  156. foreach (Camera camera in componentsInChildren)
  157. {
  158. if (camera.gameObject.CompareTag("MainCamera"))
  159. {
  160. this.islandCamera = camera;
  161. break;
  162. }
  163. }
  164. if (this.islandCamera == null)
  165. {
  166. throw new InvalidOperationException("No camera was set and no main camera was found.");
  167. }
  168. }
  169. if (!this.islandCamera.orthographic)
  170. {
  171. throw new InvalidOperationException("The camera is not set to orthographic mode.");
  172. }
  173. if (this.islandBounds.extents.x <= 0f || this.islandBounds.extents.y <= 0f)
  174. {
  175. throw new ArgumentException("The X and Y extents of the island bounds must be larger than 0.", (this.islandBounds.extents.x > 0f) ? "bounds.extents.y" : "bounds.extents.x");
  176. }
  177. this._dpi = ((Screen.dpi > 0f) ? Screen.dpi : 72f);
  178. IsometricIsland.Current = this;
  179. IsometricIsland.FireIslandLoadedEvent(this);
  180. }
  181. private void Start()
  182. {
  183. this._autoSaveTimer = Time.realtimeSinceStartup;
  184. SingletonMonobehaviour<WorldMap>.Instance.VisibilityChangedEvent += this.OnWorldMapVisibilityChanged;
  185. this.OnWorldMapVisibilityChanged(SingletonMonobehaviour<WorldMap>.Instance.IsVisible);
  186. }
  187. private void OnDestroy()
  188. {
  189. if (SingletonMonobehaviour<WorldMap>.IsAvailable)
  190. {
  191. SingletonMonobehaviour<WorldMap>.Instance.VisibilityChangedEvent -= this.OnWorldMapVisibilityChanged;
  192. }
  193. IsometricIsland.FireIslandUnloadedEvent();
  194. }
  195. private void OnApplicationQuit()
  196. {
  197. StorageController.SaveAll();
  198. }
  199. private void OnApplicationPause(bool paused)
  200. {
  201. this._isPausing = paused;
  202. if (paused)
  203. {
  204. StorageController.SaveAll();
  205. }
  206. else
  207. {
  208. Storage.NewSession();
  209. }
  210. }
  211. private void OnGridDeserialized()
  212. {
  213. this.HandleGridDeserializedEvent(this.grid);
  214. }
  215. public void EnsureSave()
  216. {
  217. if (this.isPausing)
  218. {
  219. StorageController.SaveAll();
  220. }
  221. }
  222. private void LateUpdate()
  223. {
  224. if (this.autoSaveInterval > 0f)
  225. {
  226. float realtimeSinceStartup = Time.realtimeSinceStartup;
  227. if (realtimeSinceStartup - this._autoSaveTimer >= this.autoSaveInterval)
  228. {
  229. this._autoSaveTimer = realtimeSinceStartup;
  230. StorageController.SaveAll();
  231. }
  232. }
  233. }
  234. public void CenterCameraOnAnyActiveBuilding()
  235. {
  236. foreach (Building building in this._buildingsOnIsland)
  237. {
  238. if (building != null && !(building is Scenery) && (!(building is ActivatableBuilding) || (building as ActivatableBuilding).Activated))
  239. {
  240. this.input.CenterViewOn(building.gameObject, false);
  241. this.hasCenteredViewOnce = true;
  242. break;
  243. }
  244. }
  245. }
  246. public void CenterCameraOnBuilding(Building b)
  247. {
  248. this.input.CenterViewOn(b.gameObject, false);
  249. this.hasCenteredViewOnce = true;
  250. }
  251. private void OnWorldMapVisibilityChanged(bool visible)
  252. {
  253. this.input.enabled = !visible;
  254. }
  255. public static IsometricIsland Current;
  256. public float pixelsPerUnit = 1f;
  257. public Bounds islandBounds = new Bounds(new Vector3(0f, 0f, 0f), new Vector3(1000f, 1000f, 1000f));
  258. public Vector2 preferedInitialPosition = new Vector2(500f, 500f);
  259. public float preferedInitialZoomLevel = 500f;
  260. public float autoSaveInterval = 60f;
  261. public Camera islandCamera;
  262. [SelfReference]
  263. public Builder builder;
  264. [SelfReference]
  265. public Destroyer destroyer;
  266. [SelfReference]
  267. public VehicleManager vehicleManager;
  268. [ChildReference]
  269. public IsometricGrid grid;
  270. [SelfReference]
  271. public CIGIslandState islandState;
  272. [SerializeField]
  273. private CIGDropChestManager _dropChestManager;
  274. [SerializeField]
  275. private CIGFloatingChestManager _floatingChestManager;
  276. private IslandInput _input;
  277. private GameObjectManager _goManager;
  278. private CIGGameState _gameState;
  279. private float _dpi;
  280. private float _autoSaveTimer;
  281. private bool _isPausing;
  282. private List<Building> _buildingsOnIsland;
  283. private bool hasCenteredViewOnce;
  284. public delegate void IslandLoadedEventHandler(IsometricIsland island);
  285. public delegate void IslandUnloadedEventHandler();
  286. }
  287. }