using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using CIG; using CIG.Translation; using CIGEnums; using SUISS.Core; using SUISS.Scheduling; using UnityEngine; namespace SUISSEngine { public class UpgradableBuilding : Building { public static bool LevelIconsHidden { get { return UpgradableBuilding.levelIconsHidden; } } public static void SetLevelIconsHidden(bool hidden, IsometricGrid grid) { if (hidden != UpgradableBuilding.levelIconsHidden) { UpgradableBuilding.levelIconsHidden = hidden; if (grid != null) { foreach (GridTile gridTile in grid.Tiles) { UpgradableBuilding component = gridTile.GetComponent(); if (component != null) { component.UpdateLevelIcon(component.CurrentLevel); } } } } } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event Action UpgradeStartedEvent; protected virtual void FireUpgradeStartedEvent() { if (this.UpgradeStartedEvent != null) { this.UpgradeStartedEvent(); } } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event Building.TimedBuildingEventHandler UpgradeCompletedEvent; protected virtual void FireUpgradeCompletedEvent(double upgradeCompletedTime) { if (this.UpgradeCompletedEvent != null) { this.UpgradeCompletedEvent(upgradeCompletedTime); } } public virtual Currencies UpgradeCost { get { return this.initialPurchasePrice; } } protected override void Awake() { base.Awake(); this.currentLevel = this.initialLevel; this.IsUpgrading = false; } protected override void OnDestroy() { this.StopUpgradeRoutine(); base.OnDestroy(); } public int CurrentLevel { get { return this.currentLevel; } } public virtual int DisplayLevel { get { return this.CurrentLevel; } } public virtual bool CanUpgrade { get { return this.currentLevel < this.GetMaxLevel(); } } public bool IsUpgrading { get; private set; } public virtual int UpgradeTime { get { int num = Math.Max(5, this.constructionTime / 60); return 60 * (int)((float)num * (0.75f + 0.15f * (float)this.CurrentLevel)); } } public double UpgradeTimeLeft { get { if (this._upgradeEnumerator != null) { return Scheduler.TimeLeft(this._upgradeEnumerator); } return 0.0; } } public int GetMaxLevel() { if (SingletonMonobehaviour.Instance.MaxBuildingLevel10 && this.maximumLevel > 10) { return 10; } return this.maximumLevel; } public virtual bool StartUpgrade() { if (!this.CanUpgrade) { UnityEngine.Debug.LogWarning(string.Format("{0}: Cannot upgrade, since current level is {1} and maximum level is {2}", base.name, this.currentLevel, this.GetMaxLevel())); return false; } SingletonMonobehaviour.Instance.PlayClip(Clip.Upgrade); this.StartUpgradeRoutine(); base.SetProgressBar((double)this.UpgradeTime, (double)this.UpgradeTime); this._gridTileIconManager.SetIcon(GridTileIconType.UpgradeArrow); return true; } public virtual void FinishUpgrade() { if (this._upgradeEnumerator != null) { SingletonMonobehaviour.Instance.PlayClip(Clip.Fairy); this.upgradedTime = Timing.time; SingletonMonobehaviour.Instance.StopRoutine(this._upgradeEnumerator); this._upgradeEnumerator = null; base.BroadcastMessage("OnSpeedupPurchased", SendMessageOptions.DontRequireReceiver); } } public virtual ILocalizedString ReasonWhyCantUpgrade(out ILocalizedString title) { title = Localization.EmptyLocalizedString; if (this.CanUpgrade) { return null; } return Localization.Key("object_upgraded_to_maximum_level"); } protected virtual void UpdateLevelIcon(int level) { if (UpgradableBuilding.levelIconsHidden) { this._gridTileIconManager.RemoveIcon(GridTileIconType.Level); } else if (this.maximumLevel != 0 && base.state != BuildingState.Constructing) { LevelGridTileIcon levelGridTileIcon = this._gridTileIconManager.SetIcon(GridTileIconType.Level); levelGridTileIcon.SetLevel(level); } } private IEnumerator WaitForUpgrade() { if (this.upgradedTime < 0.0) { UnityEngine.Debug.LogWarning("upgradedTime shouldn't be less then 0"); this.upgradedTime = Timing.time + (double)this.UpgradeTime; } yield return this.upgradedTime; yield break; } private IEnumerator UpgradeBehaviour() { if (!this.IsUpgrading) { this.IsUpgrading = true; this.upgradedTime = Timing.Instance.GameTime + (double)this.UpgradeTime; base.BroadcastMessage("OnUpgradeStarted", SendMessageOptions.DontRequireReceiver); this.serializing.Serialize(); SingletonMonobehaviour.Instance.StartTracking(this, this.upgradedTime); } else if (SingletonMonobehaviour.Instance.IsSpeedupped(this)) { this.upgradedTime = Timing.Instance.GameTime; } yield return this._upgradeEnumerator = this.WaitForUpgrade(); this._upgradeEnumerator = null; if (this._upgradeCancelled) { this._upgradeCancelled = false; this.IsUpgrading = false; SingletonMonobehaviour.Instance.FinishTracking(this); yield break; } this.PerformUpgrade(); this._upgradeBehaviourRoutine = null; yield break; } protected void PerformUpgrade() { ActivatableBuilding component = base.GetComponent(); SingletonMonobehaviour.Instance.AddBuildingAction(); this.currentLevel++; this.IsUpgrading = false; this.UpdateLevelIcon(this.currentLevel); base.BroadcastMessage("OnUpgradeCompleted", this.upgradedTime, SendMessageOptions.DontRequireReceiver); this.serializing.Serialize(); SingletonMonobehaviour.Instance.FinishTracking(this); } protected override void OnSerialize(Dictionary values) { base.OnSerialize(values); values["level"] = this.currentLevel; if (this.IsUpgrading) { if (this.upgradedTime >= 0.0) { values["upgradedTime"] = this.upgradedTime; } else if (this._upgradeEnumerator != null) { UnityEngine.Debug.LogWarning("upgradedTime shouldn't be less then 0"); values["upgradedTime"] = (double)this._upgradeEnumerator.Current; } else if (this.UpgradeTime > 0) { UnityEngine.Debug.LogWarning("upgradedTime shouldn't be less then 0"); values["upgradedTime"] = Timing.time + (double)this.UpgradeTime; } else { UnityEngine.Debug.LogWarning("Cannot find what to store for 'upgradedTime'"); } } else if (values.ContainsKey("upgradedTime")) { values.Remove("upgradedTime"); } } protected override void OnDeserialize(Dictionary values) { base.OnDeserialize(values); if (values.ContainsKey("level")) { this.currentLevel = (int)values["level"]; } if (values.ContainsKey("upgradedTime")) { this.upgradedTime = (double)values["upgradedTime"]; this.IsUpgrading = true; } else { this.upgradedTime = -1.0; this.IsUpgrading = false; } } protected override void OnDeserialized() { base.OnDeserialized(); this.UpdateLevelIcon(this.currentLevel); if (this.IsUpgrading) { this.StartUpgradeRoutine(); base.SetProgressBar(this.UpgradeTimeLeft, (double)this.UpgradeTime); this._gridTileIconManager.SetIcon(GridTileIconType.UpgradeArrow); } } protected override void OnConstructionCompleted() { base.OnConstructionCompleted(); this.UpdateLevelIcon(this.currentLevel); } protected override void OnDemolishStarted() { base.HideProgressBar(); base.OnDemolishStarted(); if (this.IsUpgrading) { this._upgradeCancelled = true; if (this._upgradeEnumerator != null) { SingletonMonobehaviour.Instance.StopRoutine(this._upgradeEnumerator); this._upgradeEnumerator = null; } } this._gridTileIconManager.RemoveIcon(GridTileIconType.Level); } protected virtual void OnUpgradeStarted() { this.FireUpgradeStartedEvent(); } protected virtual void OnUpgradeCompleted(double upgradedTime) { this.FireUpgradeCompletedEvent(upgradedTime); base.HideProgressBar(); this._gridTileIconManager.RemoveIcon(GridTileIconType.UpgradeArrow); } private void StartUpgradeRoutine() { this.StopUpgradeRoutine(); SingletonMonobehaviour.Instance.StartRoutine(this._upgradeBehaviourRoutine = this.UpgradeBehaviour(), base.gameObject); } private void StopUpgradeRoutine() { if (SingletonMonobehaviour.IsAvailable && this._upgradeBehaviourRoutine != null) { SingletonMonobehaviour.Instance.StopRoutine(this._upgradeBehaviourRoutine); this._upgradeBehaviourRoutine = null; this._upgradeEnumerator = null; } } protected static bool levelIconsHidden = true; public int initialLevel; [SerializeField] private int maximumLevel = 10; private int currentLevel; private IEnumerator _upgradeBehaviourRoutine; private IEnumerator _upgradeEnumerator; private bool _upgradeCancelled; protected double upgradedTime = -1.0; } }