using System; using System.Collections.Generic; using CIG.Extensions; using CIG.Translation; using SUISS.Core; using SUISSEngine; using UnityEngine; public class CIGFloatingChestManager : MonoBehaviour, IHasNotification { private void OnDestroy() { this.ClearInvokes(); if (SingletonMonobehaviour.IsAvailable) { SingletonMonobehaviour.Instance.TutorialFinishedEvent -= this.OnTutorialFinished; } if (this._chest != null) { this._chest.RewardCollectedEvent -= this.StartChestDropTimer; } if (SingletonMonobehaviour.IsAvailable) { SingletonMonobehaviour.Instance.NoLongerHasNotification(this); } if (this._isometricGrid != null) { this._isometricGrid.GridDeserializedEvent -= new IsometricGrid.GridDeserializedEventHandler(this.OnIsometricGridDeserialized); } } public float RemainingTimeUntilDrop { get { return Mathf.Max((float)this._dropTime.Subtract(DateTime.UtcNow).TotalSeconds, 0f); } } private bool CanDrop { get { return this._defaultFallbackFloatingChestLocations.Length > 0; } } private void DropChest() { this.ClearInvokes(); if (!this.CanDrop) { UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGFloatingChestManager.DropChest: DefaultFallbackFloatingChestLocations must have atleast 1 grid index!", new object[] { base.gameObject.name }); return; } if (this._isometricGrid == null) { UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGFloatingChestManager.DropChest: IsometricGrid cannot be found! Cannot continue with the initialization of the Floating Chest", new object[] { base.gameObject.name }); return; } GridIndex index = this._defaultFallbackFloatingChestLocations[UnityEngine.Random.Range(0, this._defaultFallbackFloatingChestLocations.Length)]; this._builder.BuildAt(this._floatingChestPrefab, index, false, true); this._state = CIGFloatingChestManager.ChestState.Dropped; GridSize size = new GridSize(1, 1); GridTile gridTile = this._isometricGrid.FindTileAt(index, size); if (gridTile != null) { this._chest = gridTile.GetComponent(); if (this._chest != null) { this._chest.RewardCollectedEvent += this.StartChestDropTimer; } } this._serializing.Serialize(); } private void StartChestDropTimer() { this.ClearInvokes(); this._chest = null; this._dropTime = DateTime.UtcNow + TimeSpan.FromSeconds((double)UnityEngine.Random.Range(43200f, 129600f)); this._state = CIGFloatingChestManager.ChestState.Timer; this._serializing.Serialize(); this.Invoke(new Action(this.DropChest), this.RemainingTimeUntilDrop, true); } private void ClearInvokes() { this.CancelInvoke(new Action(this.DropChest)); } public PlannedNotification[] GetNotifications() { List list = new List(); if (this._state == CIGFloatingChestManager.ChestState.Timer) { list.Add(new PlannedNotification((int)this.RemainingTimeUntilDrop, Localization.Key("floating_chest_notification"), false)); } return list.ToArray(); } private void OnSerialize(Dictionary values) { values["floatingChestState"] = (int)this._state; values["floatingChestDropTime"] = this._dropTime.ToBinary(); values["floatingChestGridIndexU"] = this._gridIndexU; values["floatingChestGridIndexV"] = this._gridIndexV; } private void OnDeserialize(Dictionary values) { object obj; if (values.TryGetValue("floatingChestState", out obj) && obj is int) { this._state = (CIGFloatingChestManager.ChestState)((int)obj); } else { this._state = CIGFloatingChestManager.ChestState.Disabled; } object obj2; if (values.TryGetValue("floatingChestDropTime", out obj2) && obj2 is long) { this._dropTime = DateTime.FromBinary((long)obj2); } else { this._dropTime = DateTime.UtcNow; } object obj3; if (values.TryGetValue("floatingChestGridIndexU", out obj3) && obj3 is int) { this._gridIndexU = (int)obj3; } else { this._gridIndexU = 0; } object obj4; if (values.TryGetValue("floatingChestGridIndexV", out obj4) && obj4 is int) { this._gridIndexV = (int)obj4; } else { this._gridIndexV = 0; } } private void OnDeserialized() { if (!this.CanDrop) { UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGFloatingChestManager.OnDeserialized: DefaultFallbackFloatingChestLocations must have atleast 1 grid index!", new object[] { base.gameObject.name }); return; } if (CityIsland.Current.isometricIsland != null) { this._isometricGrid = CityIsland.Current.isometricIsland.grid; if (this._state == CIGFloatingChestManager.ChestState.Disabled) { if (SingletonMonobehaviour.IsAvailable) { CIGTutorialManager instance = SingletonMonobehaviour.Instance; if (instance.IsFinished) { this.StartChestDropTimer(); } else { instance.TutorialFinishedEvent += this.OnTutorialFinished; } } } else if (this._state == CIGFloatingChestManager.ChestState.Timer) { if (this.RemainingTimeUntilDrop > 0f) { this.Invoke(new Action(this.DropChest), this.RemainingTimeUntilDrop, true); } else { this.DropChest(); } } else if (this._state == CIGFloatingChestManager.ChestState.Dropped) { if (this._isometricGrid.HasGridDeserialized) { this.OnIsometricGridDeserialized(this._isometricGrid); } else { this._isometricGrid.GridDeserializedEvent += new IsometricGrid.GridDeserializedEventHandler(this.OnIsometricGridDeserialized); } } SingletonMonobehaviour.Instance.HasNotification(this); return; } UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGFloatingChestManager.OnDeserialized: IsometricGrid cannot be found! Cannot continue with the initialization of the Floating Chest", new object[] { base.gameObject.name }); } private void OnTutorialFinished() { this.StartChestDropTimer(); } private void OnIsometricGridDeserialized(object sender) { GridIndex index = new GridIndex(this._gridIndexU, this._gridIndexV); GridSize size = new GridSize(1, 1); GridTile gridTile = this._isometricGrid.FindTileAt(index, size); if (gridTile != null) { this._chest = gridTile.GetComponent(); if (this._chest != null) { this._chest.RewardCollectedEvent += this.StartChestDropTimer; } else { this.StartChestDropTimer(); } } else { this.StartChestDropTimer(); } } private const float MinDropTimeInSeconds = 43200f; private const float MaxDropTimeInSeconds = 129600f; [SerializeField] private CIGBuilder _builder; [SerializeField] private GameObject _floatingChestPrefab; [SerializeField] private Serializing _serializing; [SerializeField] private GridIndex[] _defaultFallbackFloatingChestLocations; private CIGFloatingChestManager.ChestState _state; private DateTime _dropTime = DateTime.UtcNow; private int _gridIndexU; private int _gridIndexV; private FloatingChest _chest; private IsometricGrid _isometricGrid; private const string StateKey = "floatingChestState"; private const string DropTimeKey = "floatingChestDropTime"; private const string GridIndexUKey = "floatingChestGridIndexU"; private const string GridIndexVKey = "floatingChestGridIndexV"; public enum ChestState { Disabled, Timer, Dropped } }