您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

231 行
5.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using CIG;
  5. using CIG.Extensions;
  6. using SUISS.Core;
  7. using SUISSEngine;
  8. using UnityEngine;
  9. public class CIGDropChestManager : MonoBehaviour
  10. {
  11. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  12. public event Action StateChangedEvent;
  13. private void FireStateChangedEvent()
  14. {
  15. if (this.StateChangedEvent != null)
  16. {
  17. this.StateChangedEvent();
  18. }
  19. }
  20. private void Awake()
  21. {
  22. if (this._defaultFallbackDropChestLocations.Length == 0)
  23. {
  24. UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGDropChestManager.DefaultFallbackDropChestLocations must have atleast 1 grid index!", new object[]
  25. {
  26. base.gameObject.name
  27. });
  28. }
  29. }
  30. private void OnDestroy()
  31. {
  32. this.ClearInvokes();
  33. this.StateChangedEvent = null;
  34. if (SingletonMonobehaviour<VideoAds1Manager>.IsAvailable)
  35. {
  36. SingletonMonobehaviour<VideoAds1Manager>.Instance.UnlockedEvent -= this.OnVideoWatchingUnlocked;
  37. }
  38. }
  39. public float RemainingTimeUntilDrop
  40. {
  41. get
  42. {
  43. return Mathf.Max((float)this._dropTime.Subtract(DateTime.UtcNow).TotalSeconds, 0f);
  44. }
  45. }
  46. public CIGDropChestManager.ChestState State
  47. {
  48. get
  49. {
  50. return this._state;
  51. }
  52. }
  53. public void DropChest()
  54. {
  55. this.ClearInvokes();
  56. GridIndex index = this.FindChestDropLocation(this._expansions, this._grid);
  57. this._builder.BuildAt(this._dropChestPrefab, index, false, true);
  58. this._state = CIGDropChestManager.ChestState.Dropped;
  59. this._serializing.Serialize();
  60. this.FireStateChangedEvent();
  61. }
  62. private void SetChestReady()
  63. {
  64. this.ClearInvokes();
  65. this._state = CIGDropChestManager.ChestState.Ready;
  66. this._serializing.Serialize();
  67. this.FireStateChangedEvent();
  68. }
  69. private void StartChestDrop()
  70. {
  71. this.ClearInvokes();
  72. this._dropTime = DateTime.UtcNow + TimeSpan.FromHours((double)this._droppingTimerHours);
  73. this._state = CIGDropChestManager.ChestState.Timer;
  74. this._serializing.Serialize();
  75. this.FireStateChangedEvent();
  76. this.Invoke(new Action(this.SetChestReady), this.RemainingTimeUntilDrop, true);
  77. }
  78. private void ClearInvokes()
  79. {
  80. this.CancelInvoke(new Action(this.SetChestReady));
  81. }
  82. private GridIndex FindChestDropLocation(CIGExpansions expansions, IsometricGrid grid)
  83. {
  84. List<GridIndex> list = new List<GridIndex>();
  85. List<GridIndex> list2 = new List<GridIndex>();
  86. for (int i = 0; i < grid.sizeU; i++)
  87. {
  88. for (int j = 0; j < grid.sizeV; j++)
  89. {
  90. GridElement gridElement = grid[i, j];
  91. if (gridElement.Type > 0 && gridElement.Type != 3 && gridElement.Tile == null)
  92. {
  93. GridIndex gridIndex = new GridIndex(i, j);
  94. if (gridElement.Unlocked)
  95. {
  96. list2.Add(gridIndex);
  97. }
  98. else if (expansions.HasBuySign(gridIndex))
  99. {
  100. list.Add(gridIndex);
  101. }
  102. }
  103. }
  104. }
  105. int count = list.Count;
  106. int count2 = list2.Count;
  107. if (count > 0)
  108. {
  109. return list[UnityEngine.Random.Range(0, count)];
  110. }
  111. if (count2 > 0)
  112. {
  113. return list2[UnityEngine.Random.Range(0, count2)];
  114. }
  115. return this._defaultFallbackDropChestLocations[UnityEngine.Random.Range(0, this._defaultFallbackDropChestLocations.Length)];
  116. }
  117. private void OnSerialize(Dictionary<string, object> values)
  118. {
  119. values["state"] = (int)this._state;
  120. values["dropTime"] = this._dropTime.ToBinary();
  121. }
  122. private void OnDeserialize(Dictionary<string, object> values)
  123. {
  124. object obj;
  125. if (values.TryGetValue("state", out obj) && obj is int)
  126. {
  127. this._state = (CIGDropChestManager.ChestState)((int)obj);
  128. }
  129. else
  130. {
  131. this._state = CIGDropChestManager.ChestState.Disabled;
  132. }
  133. object obj2;
  134. if (values.TryGetValue("dropTime", out obj2) && obj2 is long)
  135. {
  136. this._dropTime = DateTime.FromBinary((long)obj2);
  137. }
  138. else
  139. {
  140. this._dropTime = DateTime.UtcNow;
  141. }
  142. }
  143. private void OnDeserialized()
  144. {
  145. if (this._state == CIGDropChestManager.ChestState.Disabled)
  146. {
  147. if (SingletonMonobehaviour<VideoAds1Manager>.IsAvailable)
  148. {
  149. VideoAds1Manager instance = SingletonMonobehaviour<VideoAds1Manager>.Instance;
  150. if (instance.IsUnlocked)
  151. {
  152. this.StartChestDrop();
  153. }
  154. else
  155. {
  156. instance.UnlockedEvent += this.OnVideoWatchingUnlocked;
  157. }
  158. }
  159. }
  160. else if (this._state == CIGDropChestManager.ChestState.Timer)
  161. {
  162. this.CancelInvoke(new Action(this.SetChestReady));
  163. if (this.RemainingTimeUntilDrop > 0f)
  164. {
  165. this.Invoke(new Action(this.SetChestReady), this.RemainingTimeUntilDrop, true);
  166. }
  167. else
  168. {
  169. this.SetChestReady();
  170. }
  171. }
  172. this.FireStateChangedEvent();
  173. }
  174. private void OnVideoWatchingUnlocked()
  175. {
  176. this.StartChestDrop();
  177. }
  178. [SerializeField]
  179. private IsometricGrid _grid;
  180. [SerializeField]
  181. private CIGExpansions _expansions;
  182. [SerializeField]
  183. private CIGBuilder _builder;
  184. [SerializeField]
  185. private GameObject _dropChestPrefab;
  186. [SerializeField]
  187. private Serializing _serializing;
  188. [SerializeField]
  189. private float _droppingTimerHours;
  190. [SerializeField]
  191. private GridIndex[] _defaultFallbackDropChestLocations;
  192. private CIGDropChestManager.ChestState _state;
  193. private DateTime _dropTime = DateTime.UtcNow;
  194. private const string StateKey = "state";
  195. private const string DropTimeKey = "dropTime";
  196. public enum ChestState
  197. {
  198. Disabled,
  199. Timer,
  200. Ready,
  201. Dropped
  202. }
  203. }