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

286 行
7.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using CIG.Extensions;
  4. using CIG.Translation;
  5. using SUISS.Core;
  6. using SUISSEngine;
  7. using UnityEngine;
  8. public class CIGFloatingChestManager : MonoBehaviour, IHasNotification
  9. {
  10. private void OnDestroy()
  11. {
  12. this.ClearInvokes();
  13. if (SingletonMonobehaviour<CIGTutorialManager>.IsAvailable)
  14. {
  15. SingletonMonobehaviour<CIGTutorialManager>.Instance.TutorialFinishedEvent -= this.OnTutorialFinished;
  16. }
  17. if (this._chest != null)
  18. {
  19. this._chest.RewardCollectedEvent -= this.StartChestDropTimer;
  20. }
  21. if (SingletonMonobehaviour<CIGLocalNotificationManager>.IsAvailable)
  22. {
  23. SingletonMonobehaviour<CIGLocalNotificationManager>.Instance.NoLongerHasNotification(this);
  24. }
  25. if (this._isometricGrid != null)
  26. {
  27. this._isometricGrid.GridDeserializedEvent -= new IsometricGrid.GridDeserializedEventHandler(this.OnIsometricGridDeserialized);
  28. }
  29. }
  30. public float RemainingTimeUntilDrop
  31. {
  32. get
  33. {
  34. return Mathf.Max((float)this._dropTime.Subtract(DateTime.UtcNow).TotalSeconds, 0f);
  35. }
  36. }
  37. private bool CanDrop
  38. {
  39. get
  40. {
  41. return this._defaultFallbackFloatingChestLocations.Length > 0;
  42. }
  43. }
  44. private void DropChest()
  45. {
  46. this.ClearInvokes();
  47. if (!this.CanDrop)
  48. {
  49. UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGFloatingChestManager.DropChest: DefaultFallbackFloatingChestLocations must have atleast 1 grid index!", new object[]
  50. {
  51. base.gameObject.name
  52. });
  53. return;
  54. }
  55. if (this._isometricGrid == null)
  56. {
  57. UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGFloatingChestManager.DropChest: IsometricGrid cannot be found! Cannot continue with the initialization of the Floating Chest", new object[]
  58. {
  59. base.gameObject.name
  60. });
  61. return;
  62. }
  63. GridIndex index = this._defaultFallbackFloatingChestLocations[UnityEngine.Random.Range(0, this._defaultFallbackFloatingChestLocations.Length)];
  64. this._builder.BuildAt(this._floatingChestPrefab, index, false, true);
  65. this._state = CIGFloatingChestManager.ChestState.Dropped;
  66. GridSize size = new GridSize(1, 1);
  67. GridTile gridTile = this._isometricGrid.FindTileAt(index, size);
  68. if (gridTile != null)
  69. {
  70. this._chest = gridTile.GetComponent<FloatingChest>();
  71. if (this._chest != null)
  72. {
  73. this._chest.RewardCollectedEvent += this.StartChestDropTimer;
  74. }
  75. }
  76. this._serializing.Serialize();
  77. }
  78. private void StartChestDropTimer()
  79. {
  80. this.ClearInvokes();
  81. this._chest = null;
  82. this._dropTime = DateTime.UtcNow + TimeSpan.FromSeconds((double)UnityEngine.Random.Range(43200f, 129600f));
  83. this._state = CIGFloatingChestManager.ChestState.Timer;
  84. this._serializing.Serialize();
  85. this.Invoke(new Action(this.DropChest), this.RemainingTimeUntilDrop, true);
  86. }
  87. private void ClearInvokes()
  88. {
  89. this.CancelInvoke(new Action(this.DropChest));
  90. }
  91. public PlannedNotification[] GetNotifications()
  92. {
  93. List<PlannedNotification> list = new List<PlannedNotification>();
  94. if (this._state == CIGFloatingChestManager.ChestState.Timer)
  95. {
  96. list.Add(new PlannedNotification((int)this.RemainingTimeUntilDrop, Localization.Key("floating_chest_notification"), false));
  97. }
  98. return list.ToArray();
  99. }
  100. private void OnSerialize(Dictionary<string, object> values)
  101. {
  102. values["floatingChestState"] = (int)this._state;
  103. values["floatingChestDropTime"] = this._dropTime.ToBinary();
  104. values["floatingChestGridIndexU"] = this._gridIndexU;
  105. values["floatingChestGridIndexV"] = this._gridIndexV;
  106. }
  107. private void OnDeserialize(Dictionary<string, object> values)
  108. {
  109. object obj;
  110. if (values.TryGetValue("floatingChestState", out obj) && obj is int)
  111. {
  112. this._state = (CIGFloatingChestManager.ChestState)((int)obj);
  113. }
  114. else
  115. {
  116. this._state = CIGFloatingChestManager.ChestState.Disabled;
  117. }
  118. object obj2;
  119. if (values.TryGetValue("floatingChestDropTime", out obj2) && obj2 is long)
  120. {
  121. this._dropTime = DateTime.FromBinary((long)obj2);
  122. }
  123. else
  124. {
  125. this._dropTime = DateTime.UtcNow;
  126. }
  127. object obj3;
  128. if (values.TryGetValue("floatingChestGridIndexU", out obj3) && obj3 is int)
  129. {
  130. this._gridIndexU = (int)obj3;
  131. }
  132. else
  133. {
  134. this._gridIndexU = 0;
  135. }
  136. object obj4;
  137. if (values.TryGetValue("floatingChestGridIndexV", out obj4) && obj4 is int)
  138. {
  139. this._gridIndexV = (int)obj4;
  140. }
  141. else
  142. {
  143. this._gridIndexV = 0;
  144. }
  145. }
  146. private void OnDeserialized()
  147. {
  148. if (!this.CanDrop)
  149. {
  150. UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGFloatingChestManager.OnDeserialized: DefaultFallbackFloatingChestLocations must have atleast 1 grid index!", new object[]
  151. {
  152. base.gameObject.name
  153. });
  154. return;
  155. }
  156. if (CityIsland.Current.isometricIsland != null)
  157. {
  158. this._isometricGrid = CityIsland.Current.isometricIsland.grid;
  159. if (this._state == CIGFloatingChestManager.ChestState.Disabled)
  160. {
  161. if (SingletonMonobehaviour<CIGTutorialManager>.IsAvailable)
  162. {
  163. CIGTutorialManager instance = SingletonMonobehaviour<CIGTutorialManager>.Instance;
  164. if (instance.IsFinished)
  165. {
  166. this.StartChestDropTimer();
  167. }
  168. else
  169. {
  170. instance.TutorialFinishedEvent += this.OnTutorialFinished;
  171. }
  172. }
  173. }
  174. else if (this._state == CIGFloatingChestManager.ChestState.Timer)
  175. {
  176. if (this.RemainingTimeUntilDrop > 0f)
  177. {
  178. this.Invoke(new Action(this.DropChest), this.RemainingTimeUntilDrop, true);
  179. }
  180. else
  181. {
  182. this.DropChest();
  183. }
  184. }
  185. else if (this._state == CIGFloatingChestManager.ChestState.Dropped)
  186. {
  187. if (this._isometricGrid.HasGridDeserialized)
  188. {
  189. this.OnIsometricGridDeserialized(this._isometricGrid);
  190. }
  191. else
  192. {
  193. this._isometricGrid.GridDeserializedEvent += new IsometricGrid.GridDeserializedEventHandler(this.OnIsometricGridDeserialized);
  194. }
  195. }
  196. SingletonMonobehaviour<CIGLocalNotificationManager>.Instance.HasNotification(this);
  197. return;
  198. }
  199. UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGFloatingChestManager.OnDeserialized: IsometricGrid cannot be found! Cannot continue with the initialization of the Floating Chest", new object[]
  200. {
  201. base.gameObject.name
  202. });
  203. }
  204. private void OnTutorialFinished()
  205. {
  206. this.StartChestDropTimer();
  207. }
  208. private void OnIsometricGridDeserialized(object sender)
  209. {
  210. GridIndex index = new GridIndex(this._gridIndexU, this._gridIndexV);
  211. GridSize size = new GridSize(1, 1);
  212. GridTile gridTile = this._isometricGrid.FindTileAt(index, size);
  213. if (gridTile != null)
  214. {
  215. this._chest = gridTile.GetComponent<FloatingChest>();
  216. if (this._chest != null)
  217. {
  218. this._chest.RewardCollectedEvent += this.StartChestDropTimer;
  219. }
  220. else
  221. {
  222. this.StartChestDropTimer();
  223. }
  224. }
  225. else
  226. {
  227. this.StartChestDropTimer();
  228. }
  229. }
  230. private const float MinDropTimeInSeconds = 43200f;
  231. private const float MaxDropTimeInSeconds = 129600f;
  232. [SerializeField]
  233. private CIGBuilder _builder;
  234. [SerializeField]
  235. private GameObject _floatingChestPrefab;
  236. [SerializeField]
  237. private Serializing _serializing;
  238. [SerializeField]
  239. private GridIndex[] _defaultFallbackFloatingChestLocations;
  240. private CIGFloatingChestManager.ChestState _state;
  241. private DateTime _dropTime = DateTime.UtcNow;
  242. private int _gridIndexU;
  243. private int _gridIndexV;
  244. private FloatingChest _chest;
  245. private IsometricGrid _isometricGrid;
  246. private const string StateKey = "floatingChestState";
  247. private const string DropTimeKey = "floatingChestDropTime";
  248. private const string GridIndexUKey = "floatingChestGridIndexU";
  249. private const string GridIndexVKey = "floatingChestGridIndexV";
  250. public enum ChestState
  251. {
  252. Disabled,
  253. Timer,
  254. Dropped
  255. }
  256. }