using System; using System.Collections.Generic; using System.Diagnostics; using SUISS.Core; using SUISS.Storage; using UnityEngine; namespace SUISSEngine { public class IsometricIsland : MonoBehaviour { public IsometricIsland() { this._buildingsOnIsland = new List(); this.hasCenteredViewOnce = false; } public static IsometricIsland GetParent(Component node) { return node.GetComponentInParent(); } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public static event IsometricIsland.IslandLoadedEventHandler IslandLoadedEvent; protected static void FireIslandLoadedEvent(IsometricIsland island) { if (IsometricIsland.IslandLoadedEvent != null) { IsometricIsland.IslandLoadedEvent(island); } } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public static event IsometricIsland.IslandUnloadedEventHandler IslandUnloadedEvent; protected static void FireIslandUnloadedEvent() { if (IsometricIsland.IslandUnloadedEvent != null) { IsometricIsland.IslandUnloadedEvent(); } } public List BuildingsOnIsland { get { return this._buildingsOnIsland; } } public Bounds islandWorldBounds { get { Vector3 center = base.transform.TransformPoint(this.islandBounds.center); Vector3 size = base.transform.TransformDirection(this.islandBounds.size); return new Bounds(center, size); } } public IslandInput input { get { if (this._input == null) { this._input = UnityEngine.Object.FindObjectOfType(); } return this._input; } } public GameObjectManager gameobjectManager { get { if (this._goManager == null) { this._goManager = SingletonMonobehaviour.Instance; } return this._goManager; } } public CIGGameState gameState { get { if (this._gameState == null) { this._gameState = SingletonMonobehaviour.Instance; } return this._gameState; } } public CIGDropChestManager DropChestManager { get { return this._dropChestManager; } } public CIGFloatingChestManager FloatingChestManager { get { return this._floatingChestManager; } } public float dpi { get { return this._dpi; } } public bool isPausing { get { return this._isPausing; } } public bool HasCenteredViewOnce { get { return this.hasCenteredViewOnce; } } private void HandleGridDeserializedEvent(UnityEngine.Object sender) { this.builder.OnTileBuild += this.HandleOnTileBuild; this.builder.OnTileDestroyed += this.HandleOnTileDestroyed; this._buildingsOnIsland = new List(this.grid.GetComponentsInChildren()); } private void HandleOnTileDestroyed(GridTile tile, GridTile.Status priorStatus) { Building component = tile.GetComponent(); if (component == null || !this._buildingsOnIsland.Contains(component)) { if (component != null) { UnityEngine.Debug.LogWarning("The buildings list doesnt contain the destroyed Building"); } return; } this._buildingsOnIsland.Remove(component); } private void HandleOnTileBuild(GridTile tile) { Building component = tile.GetComponent(); if (component == null || this._buildingsOnIsland.Contains(component)) { return; } this._buildingsOnIsland.Add(component); } private void Awake() { this._isPausing = false; if (this.islandCamera == null) { Camera[] componentsInChildren = base.gameObject.GetComponentsInChildren(false); foreach (Camera camera in componentsInChildren) { if (camera.gameObject.CompareTag("MainCamera")) { this.islandCamera = camera; break; } } if (this.islandCamera == null) { throw new InvalidOperationException("No camera was set and no main camera was found."); } } if (!this.islandCamera.orthographic) { throw new InvalidOperationException("The camera is not set to orthographic mode."); } if (this.islandBounds.extents.x <= 0f || this.islandBounds.extents.y <= 0f) { 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"); } this._dpi = ((Screen.dpi > 0f) ? Screen.dpi : 72f); IsometricIsland.Current = this; IsometricIsland.FireIslandLoadedEvent(this); } private void Start() { this._autoSaveTimer = Time.realtimeSinceStartup; SingletonMonobehaviour.Instance.VisibilityChangedEvent += this.OnWorldMapVisibilityChanged; this.OnWorldMapVisibilityChanged(SingletonMonobehaviour.Instance.IsVisible); } private void OnDestroy() { if (SingletonMonobehaviour.IsAvailable) { SingletonMonobehaviour.Instance.VisibilityChangedEvent -= this.OnWorldMapVisibilityChanged; } IsometricIsland.FireIslandUnloadedEvent(); } private void OnApplicationQuit() { StorageController.SaveAll(); } private void OnApplicationPause(bool paused) { this._isPausing = paused; if (paused) { StorageController.SaveAll(); } else { Storage.NewSession(); } } private void OnGridDeserialized() { this.HandleGridDeserializedEvent(this.grid); } public void EnsureSave() { if (this.isPausing) { StorageController.SaveAll(); } } private void LateUpdate() { if (this.autoSaveInterval > 0f) { float realtimeSinceStartup = Time.realtimeSinceStartup; if (realtimeSinceStartup - this._autoSaveTimer >= this.autoSaveInterval) { this._autoSaveTimer = realtimeSinceStartup; StorageController.SaveAll(); } } } public void CenterCameraOnAnyActiveBuilding() { foreach (Building building in this._buildingsOnIsland) { if (building != null && !(building is Scenery) && (!(building is ActivatableBuilding) || (building as ActivatableBuilding).Activated)) { this.input.CenterViewOn(building.gameObject, false); this.hasCenteredViewOnce = true; break; } } } public void CenterCameraOnBuilding(Building b) { this.input.CenterViewOn(b.gameObject, false); this.hasCenteredViewOnce = true; } private void OnWorldMapVisibilityChanged(bool visible) { this.input.enabled = !visible; } public static IsometricIsland Current; public float pixelsPerUnit = 1f; public Bounds islandBounds = new Bounds(new Vector3(0f, 0f, 0f), new Vector3(1000f, 1000f, 1000f)); public Vector2 preferedInitialPosition = new Vector2(500f, 500f); public float preferedInitialZoomLevel = 500f; public float autoSaveInterval = 60f; public Camera islandCamera; [SelfReference] public Builder builder; [SelfReference] public Destroyer destroyer; [SelfReference] public VehicleManager vehicleManager; [ChildReference] public IsometricGrid grid; [SelfReference] public CIGIslandState islandState; [SerializeField] private CIGDropChestManager _dropChestManager; [SerializeField] private CIGFloatingChestManager _floatingChestManager; private IslandInput _input; private GameObjectManager _goManager; private CIGGameState _gameState; private float _dpi; private float _autoSaveTimer; private bool _isPausing; private List _buildingsOnIsland; private bool hasCenteredViewOnce; public delegate void IslandLoadedEventHandler(IsometricIsland island); public delegate void IslandUnloadedEventHandler(); } }