|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using CIG;
- using CIGEnums;
- using SUISS.Core;
- using SUISS.Scheduling;
- using SUISS.Storage;
- using SUISSEngine;
- using UnityEngine;
-
- public sealed class CIGBuilderManager : SingletonMonobehaviour<CIGBuilderManager>
- {
- //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event CIGBuilderManager.HasDeserializedEventHandler HasDeserializedEvent;
-
- private void FireHasDeserializedEvent()
- {
- if (this.HasDeserializedEvent != null)
- {
- this.HasDeserializedEvent();
- }
- }
-
- //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event CIGBuilderManager.BuildCountChangedEventHandler BuildCountChangedEvent;
-
- private void FireBuildCountChangedEvent()
- {
- if (this.BuildCountChangedEvent != null)
- {
- this.BuildCountChangedEvent(this.CurrentBuildCount, this.MaxBuildCount);
- }
- }
-
- private void OnSerialize(Dictionary<string, object> parentValues)
- {
- Dictionary<string, object> dictionary = new Dictionary<string, object>();
- foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
- {
- string key = dictionary.Count.ToString();
- dictionary[key] = buildInfo.Serialize();
- }
- parentValues["builderManagerList"] = dictionary;
- parentValues["craneCount"] = this._craneCount;
- parentValues["goldCraneCount"] = this._goldCraneCount;
- parentValues["iapCraneCount"] = this._iapCraneCount;
- }
-
- private void OnDeserialize(Dictionary<string, object> parentValues)
- {
- this._buildings.Clear();
- if (parentValues.ContainsKey("builderManagerList"))
- {
- Dictionary<string, object> dictionary = (Dictionary<string, object>)parentValues["builderManagerList"];
- foreach (KeyValuePair<string, object> keyValuePair in dictionary)
- {
- this._buildings.Add(new CIGBuilderManager.BuildInfo((Dictionary<string, object>)keyValuePair.Value));
- }
- }
- this._craneCount = parentValues.GetInt("craneCount", this.initialCraneCount);
- this._goldCraneCount = parentValues.GetInt("goldCraneCount", 0);
- this._iapCraneCount = parentValues.GetInt("iapCraneCount", 0);
- }
-
- private void OnDeserialized()
- {
- SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._updateRoutine = this.UpdateBadgeRoutine());
- this.HasDeserialized = true;
- this.FireHasDeserializedEvent();
- }
-
- protected override void Awake()
- {
- base.Awake();
- this._craneCount = this.initialCraneCount;
- }
-
- protected override void OnDestroy()
- {
- base.OnDestroy();
- if (this._updateRoutine != null && SingletonMonobehaviour<Scheduler>.IsAvailable)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._updateRoutine);
- }
- }
-
- private IEnumerator WaitForUpdateRoutine(double t)
- {
- yield return Timing.time + t;
- this._waitRoutine = null;
- yield break;
- }
-
- private IEnumerator UpdateBadgeRoutine()
- {
- for (;;)
- {
- this.UpdateHUDBadge();
- double t = 60.0;
- foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
- {
- if (!buildInfo.IsFinished && buildInfo.Timeleft < t)
- {
- t = buildInfo.Timeleft;
- }
- }
- yield return this._waitRoutine = this.WaitForUpdateRoutine(t + 0.1);
- }
- yield break;
- }
-
- private void ForceUpdate()
- {
- if (this._waitRoutine != null)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._waitRoutine);
- this._waitRoutine = null;
- }
- }
-
- private void UpdateHUDBadge()
- {
- HUDState hudstate = UnityEngine.Object.FindObjectOfType<HUDState>();
- if (hudstate != null)
- {
- hudstate.UpdateBuildQueueButton();
- }
- CIGTutorialManager instanceIfAvailable = SingletonMonobehaviour<CIGTutorialManager>.InstanceIfAvailable;
- if (instanceIfAvailable != null)
- {
- instanceIfAvailable.NotifyBuildCount(this, this.CurrentBuildCount);
- }
- }
-
- public void StartTracking(Building b, double finishTime)
- {
- this._buildings.Add(new CIGBuilderManager.BuildInfo(b, finishTime));
- this.serializing.Serialize();
- this.ForceUpdate();
- this.FireBuildCountChangedEvent();
- }
-
- public void FinishTracking(Building b)
- {
- for (int i = 0; i < this._buildings.Count; i++)
- {
- if (b.serializing.StorageKey == this._buildings[i].serializationKey)
- {
- this._buildings.RemoveAt(i);
- this.serializing.Serialize();
- this.ForceUpdate();
- this.FireBuildCountChangedEvent();
- return;
- }
- }
- }
-
- public void SpeedupAll()
- {
- Dictionary<string, Building> dictionary = new Dictionary<string, Building>();
- foreach (Building building in UnityEngine.Object.FindObjectsOfType<Building>())
- {
- dictionary[building.serializing.StorageKey] = building;
- }
- bool flag = false;
- for (int j = this._buildings.Count - 1; j >= 0; j--)
- {
- CIGBuilderManager.BuildInfo buildInfo = this._buildings[j];
- if (!buildInfo.IsFinished)
- {
- if (dictionary.ContainsKey(buildInfo.serializationKey))
- {
- Building building2 = dictionary[buildInfo.serializationKey];
- if (building2.state == BuildingState.Constructing)
- {
- building2.FinishConstruction();
- }
- else
- {
- UpgradableBuilding upgradableBuilding = building2 as UpgradableBuilding;
- if (upgradableBuilding != null)
- {
- if (upgradableBuilding.IsUpgrading)
- {
- upgradableBuilding.FinishUpgrade();
- }
- else
- {
- UnityEngine.Debug.LogWarning(string.Format("Building {0} is not constructing, but it's also not upgrading!", building2.name));
- }
- }
- else
- {
- UnityEngine.Debug.LogWarning(string.Format("Building {0} is not constructing, but it's not an UpgradableBuilding!", building2.name));
- }
- }
- }
- else
- {
- buildInfo.speedupped = true;
- }
- }
- }
- if (flag)
- {
- this.serializing.Serialize();
- }
- this.ForceUpdate();
- this.FireBuildCountChangedEvent();
- }
-
- public void AddCrane(bool iap)
- {
- int craneCount = this._craneCount;
- this._craneCount++;
- if (iap)
- {
- this._iapCraneCount++;
- }
- else
- {
- this._goldCraneCount++;
- }
- this.serializing.Serialize();
- Storage.SaveLifecycle(this.serializing.StorageLifecycle, true);
- SingletonMonobehaviour<CIGGameState>.Instance.FireValueChangedHandler("craneCount", craneCount, this._craneCount);
- }
-
- public bool IsSpeedupped(Building b)
- {
- foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
- {
- if (b.serializing.StorageKey == buildInfo.serializationKey)
- {
- return buildInfo.speedupped;
- }
- }
- return false;
- }
-
- public int MaxBuildCount
- {
- get
- {
- return this._craneCount;
- }
- }
-
- public int GoldCraneCount
- {
- get
- {
- return this._goldCraneCount;
- }
- }
-
- public int IapCraneCount
- {
- get
- {
- return this._iapCraneCount;
- }
- }
-
- public int AvailableBuildCount
- {
- get
- {
- return this.MaxBuildCount - this.CurrentBuildCount;
- }
- }
-
- public int CurrentBuildCount
- {
- get
- {
- int num = 0;
- foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
- {
- if (!buildInfo.IsFinished)
- {
- num++;
- }
- }
- return num;
- }
- }
-
- public List<CIGBuilderManager.BuildInfo> BuildList
- {
- get
- {
- List<CIGBuilderManager.BuildInfo> list = new List<CIGBuilderManager.BuildInfo>();
- foreach (CIGBuilderManager.BuildInfo buildInfo in this._buildings)
- {
- if (!buildInfo.IsFinished)
- {
- list.Add(buildInfo);
- }
- }
- return list;
- }
- }
-
- public bool HasDeserialized { get; private set; }
-
- public decimal CurrentCraneGoldCost
- {
- get
- {
- if (this._craneCount >= 15)
- {
- return -1m;
- }
- switch (this._goldCraneCount)
- {
- case 0:
- return 30m;
- case 1:
- return 50m;
- case 2:
- return 70m;
- case 3:
- return 100m;
- default:
- return 150m;
- }
- }
- }
-
- public CIG3StoreProduct CurrentCraneIAP
- {
- get
- {
- if (this._craneCount >= 15)
- {
- return null;
- }
- int iapCount = this._iapCraneCount + 1;
- return SingletonMonobehaviour<CIGStoreManager>.Instance.Store.FindProduct((CIG3StoreProduct p) => p.Category == StoreProductCategory.Cranes && p.From <= iapCount && p.To >= iapCount);
- }
- }
-
- public bool CanPurchaseCrane
- {
- get
- {
- return this.CurrentCraneIAP != null || this.CurrentCraneGoldCost > 0m;
- }
- }
-
- private const string NameKey = "name";
-
- private const string SerializationKeyKey = "serializationKey";
-
- private const string FinishTimeKey = "finishTime";
-
- private const string SpeeduppedKey = "speedupped";
-
- private const int MaxCranes = 15;
-
- [SelfReference]
- public Serializing serializing;
-
- public int initialCraneCount = 5;
-
- private const string ListKey = "builderManagerList";
-
- public const string CraneCountKey = "craneCount";
-
- private const string GoldCraneCountKey = "goldCraneCount";
-
- private const string IapCraneCountKey = "iapCraneCount";
-
- private List<CIGBuilderManager.BuildInfo> _buildings = new List<CIGBuilderManager.BuildInfo>();
-
- private IEnumerator _updateRoutine;
-
- private IEnumerator _waitRoutine;
-
- private int _craneCount;
-
- private int _goldCraneCount;
-
- private int _iapCraneCount;
-
- public class BuildInfo
- {
- public BuildInfo(Building building, double finishTime)
- {
- this.prefab = SingletonMonobehaviour<GameObjectManager>.Instance.GetPrefabWithName(building.name);
- this.serializationKey = building.serializing.StorageKey;
- this.finishTime = finishTime;
- this.speedupped = false;
- }
-
- public BuildInfo(Dictionary<string, object> values)
- {
- this.prefab = SingletonMonobehaviour<GameObjectManager>.Instance.GetPrefabWithName((string)values["name"]);
- this.serializationKey = (string)values["serializationKey"];
- this.finishTime = (double)values["finishTime"];
- this.speedupped = (bool)values["speedupped"];
- }
-
- public Dictionary<string, object> Serialize()
- {
- Dictionary<string, object> dictionary = new Dictionary<string, object>();
- dictionary["name"] = this.prefab.name;
- dictionary["serializationKey"] = this.serializationKey;
- dictionary["finishTime"] = this.finishTime;
- dictionary["speedupped"] = this.speedupped;
- return dictionary;
- }
-
- public double Timeleft
- {
- get
- {
- return this.finishTime - Timing.time;
- }
- }
-
- public bool IsFinished
- {
- get
- {
- return this.speedupped || this.finishTime <= Timing.time;
- }
- }
-
- public GameObject prefab;
-
- public string serializationKey;
-
- public double finishTime;
-
- public bool speedupped;
- }
-
- public delegate void HasDeserializedEventHandler();
-
- public delegate void BuildCountChangedEventHandler(int used, int total);
- }
|