|
- 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<UpgradableBuilding>();
- 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<CIGGameState>.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<CIGAudioManager>.Instance.PlayClip(Clip.Upgrade);
- this.StartUpgradeRoutine();
- base.SetProgressBar((double)this.UpgradeTime, (double)this.UpgradeTime);
- this._gridTileIconManager.SetIcon<GridTileIcon>(GridTileIconType.UpgradeArrow);
- return true;
- }
-
- public virtual void FinishUpgrade()
- {
- if (this._upgradeEnumerator != null)
- {
- SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.Fairy);
- this.upgradedTime = Timing.time;
- SingletonMonobehaviour<Scheduler>.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<LevelGridTileIcon>(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<CIGBuilderManager>.Instance.StartTracking(this, this.upgradedTime);
- }
- else if (SingletonMonobehaviour<CIGBuilderManager>.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<CIGBuilderManager>.Instance.FinishTracking(this);
- yield break;
- }
- this.PerformUpgrade();
- this._upgradeBehaviourRoutine = null;
- yield break;
- }
-
- protected void PerformUpgrade()
- {
- ActivatableBuilding component = base.GetComponent<ActivatableBuilding>();
- SingletonMonobehaviour<CIGGameStats>.Instance.AddBuildingAction();
- this.currentLevel++;
- this.IsUpgrading = false;
- this.UpdateLevelIcon(this.currentLevel);
- base.BroadcastMessage("OnUpgradeCompleted", this.upgradedTime, SendMessageOptions.DontRequireReceiver);
- this.serializing.Serialize();
- SingletonMonobehaviour<CIGBuilderManager>.Instance.FinishTracking(this);
- }
-
- protected override void OnSerialize(Dictionary<string, object> 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<string, object> 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<GridTileIcon>(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<Scheduler>.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<Scheduler>.Instance.StartRoutine(this._upgradeBehaviourRoutine = this.UpgradeBehaviour(), base.gameObject);
- }
-
- private void StopUpgradeRoutine()
- {
- if (SingletonMonobehaviour<Scheduler>.IsAvailable && this._upgradeBehaviourRoutine != null)
- {
- SingletonMonobehaviour<Scheduler>.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;
- }
- }
|