Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

391 rader
9.0 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using Engine.Models;
  6. using SUISS.Scheduling;
  7. using UnityEngine;
  8. namespace SUISSEngine
  9. {
  10. [RequireComponent(typeof(Serializing), typeof(Builder))]
  11. public class Destroyer : MonoBehaviour
  12. {
  13. protected virtual void OnSerialize(Dictionary<string, object> values)
  14. {
  15. }
  16. protected virtual void OnDeserialize(Dictionary<string, object> values)
  17. {
  18. }
  19. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  20. public event Destroyer.QueueChangedEventHandler QueueChangedEvent;
  21. protected virtual void FireQueueChangedEvent()
  22. {
  23. if (this.QueueChangedEvent != null)
  24. {
  25. this.QueueChangedEvent();
  26. }
  27. }
  28. public bool isSelecting { get; private set; }
  29. public bool isDestroying
  30. {
  31. get
  32. {
  33. return this.currentlyDestroying != null;
  34. }
  35. }
  36. public void ClickedBuilding(Building building)
  37. {
  38. if (building.state != BuildingState.Normal)
  39. {
  40. return;
  41. }
  42. if (!building.CanDemolish)
  43. {
  44. return;
  45. }
  46. if (this.selectedBuildings.Contains(building))
  47. {
  48. Destroyer.Deselect(building.gameObject);
  49. this.selectedBuildings.Remove(building);
  50. }
  51. else
  52. {
  53. Destroyer.Select(building.gameObject);
  54. this.selectedBuildings.Add(building);
  55. }
  56. }
  57. public void ClickedRoad(Road road)
  58. {
  59. if (this.selectedRoads.Contains(road))
  60. {
  61. Destroyer.Deselect(road.gameObject);
  62. this.selectedRoads.Remove(road);
  63. }
  64. else
  65. {
  66. Destroyer.Select(road.gameObject);
  67. this.selectedRoads.Add(road);
  68. }
  69. }
  70. protected static void SetColor(GameObject obj, Color color)
  71. {
  72. SpriteRenderer component = obj.GetComponent<SpriteRenderer>();
  73. if (component != null)
  74. {
  75. component.color = color;
  76. }
  77. }
  78. protected static void Select(GameObject obj)
  79. {
  80. Destroyer.SetColor(obj, Color.red);
  81. }
  82. protected static void Deselect(GameObject obj)
  83. {
  84. Destroyer.SetColor(obj, Color.white);
  85. }
  86. protected void ResetSelection(bool destroy)
  87. {
  88. foreach (Building building in this.selectedBuildings)
  89. {
  90. Destroyer.Deselect(building.gameObject);
  91. }
  92. foreach (Road road in this.selectedRoads)
  93. {
  94. Destroyer.Deselect(road.gameObject);
  95. }
  96. if (destroy)
  97. {
  98. this.StartDestroyingSelection();
  99. }
  100. this.selectedBuildings.Clear();
  101. this.selectedRoads.Clear();
  102. }
  103. public void StartSelecting()
  104. {
  105. if (this.isSelecting)
  106. {
  107. UnityEngine.Debug.LogWarning("Cant start selecting when already selecting");
  108. return;
  109. }
  110. this.isSelecting = true;
  111. }
  112. public void CancelSelecting()
  113. {
  114. if (!this.isSelecting)
  115. {
  116. UnityEngine.Debug.LogWarning("Cant cancel selecting when not selecting");
  117. return;
  118. }
  119. this.isSelecting = false;
  120. this.ResetSelection(false);
  121. }
  122. public Building[] SelectedBuildings
  123. {
  124. get
  125. {
  126. return this.selectedBuildings.ToArray();
  127. }
  128. }
  129. public void DestroySelection()
  130. {
  131. if (!this.isSelecting)
  132. {
  133. UnityEngine.Debug.LogWarning("Cant start destroying when not selecting");
  134. return;
  135. }
  136. this.isSelecting = false;
  137. this.ResetSelection(true);
  138. }
  139. private Building GetLastInDestroyQueue()
  140. {
  141. if (this.destroyQueue.Count != 0)
  142. {
  143. return this.destroyQueue.List[this.destroyQueue.Count - 1];
  144. }
  145. return this.currentlyDestroying;
  146. }
  147. public double GetTimeTillDestroyed(Building b)
  148. {
  149. if (!(this.currentlyDestroying == b) && !this.destroyQueue.List.Contains(b))
  150. {
  151. UnityEngine.Debug.LogError("The Building is not in the destroyQueue");
  152. return 0.0;
  153. }
  154. if (this.currentlyDestroying == b)
  155. {
  156. return this.currentlyDestroying.DemolishTimeLeft;
  157. }
  158. double num = this.currentlyDestroying.DemolishTimeLeft;
  159. for (int i = 0; i < this.destroyQueue.Count; i++)
  160. {
  161. num += (double)this.destroyQueue.List[i].demolishTime;
  162. if (this.destroyQueue.List[i] == b)
  163. {
  164. break;
  165. }
  166. }
  167. return num;
  168. }
  169. public double GetTimeTillFinished()
  170. {
  171. return this.GetTimeTillDestroyed(this.GetLastInDestroyQueue());
  172. }
  173. private void StartDestroyingSelection()
  174. {
  175. this.DestroyRoads();
  176. this.QueueDestroyBuildings();
  177. }
  178. private void EndCurrentDestroyAction(double endedAtTime)
  179. {
  180. if (this.currentlyDestroying != null)
  181. {
  182. this.currentlyDestroying.DestroyedEvent -= this.EndCurrentDestroyAction;
  183. this.currentlyDestroying = null;
  184. }
  185. if (this.destroyQueue != null && this.destroyQueue.Count > 0)
  186. {
  187. if ((int)(Timing.time - endedAtTime) > 0)
  188. {
  189. this.FastForward(Timing.time - endedAtTime);
  190. }
  191. else
  192. {
  193. this.DestroyNext(Timing.time);
  194. }
  195. }
  196. else
  197. {
  198. this.UpdateNotification();
  199. }
  200. }
  201. public void DestroyNext(double startTime)
  202. {
  203. if (this.isDestroying || this.destroyQueue.Count == 0)
  204. {
  205. UnityEngine.Debug.LogWarning("Cant destroy next building. " + ((!this.isDestroying) ? "Queue is empty!" : "The destroyer hasnt finished destroying yet"));
  206. return;
  207. }
  208. this.currentlyDestroying = this.destroyQueue.Dequeue();
  209. this.FireQueueChangedEvent();
  210. this.currentlyDestroying.StartDemolishing(startTime);
  211. this.currentlyDestroying.DestroyedEvent += this.EndCurrentDestroyAction;
  212. this.FireQueueChangedEvent();
  213. this.UpdateNotification();
  214. }
  215. private void DestroyRoads()
  216. {
  217. foreach (Road road in this.selectedRoads)
  218. {
  219. this.builder.DestroyTile(road.GetComponent<GridTile>());
  220. }
  221. }
  222. private void QueueDestroyBuildings()
  223. {
  224. foreach (Building building in this.selectedBuildings)
  225. {
  226. this.DestroyBuilding(building);
  227. }
  228. }
  229. public void DestroyBuilding(Building building)
  230. {
  231. if (building.CanDemolish && building.state == BuildingState.Normal)
  232. {
  233. building.SetInDemolishingState((!this.isDestroying) ? 1 : (this.GetLastInDestroyQueue().SpotInDestroyQueue + 1));
  234. this.EnqueueBuilding(building, true);
  235. }
  236. }
  237. public void EnqueueBuilding(Building building, bool startDestroyingIfPossible)
  238. {
  239. this.destroyQueue.Enqueue(building);
  240. this.FireQueueChangedEvent();
  241. if (!this.isDestroying && startDestroyingIfPossible)
  242. {
  243. this.DestroyNext(Timing.time);
  244. }
  245. else
  246. {
  247. this.UpdateNotification();
  248. }
  249. }
  250. public void CancelDestroyBuilding(Building building)
  251. {
  252. if (this.currentlyDestroying == building)
  253. {
  254. this.EndCurrentDestroyAction(Timing.time);
  255. }
  256. else
  257. {
  258. this.destroyQueue.Remove(building);
  259. }
  260. this.UpdateNotification();
  261. }
  262. public void SetCurrentlyDestroying(Building building)
  263. {
  264. if (this.isDestroying)
  265. {
  266. UnityEngine.Debug.LogWarning("Cant set a new building to be destroyed. The destroyer is already destroying another building");
  267. return;
  268. }
  269. this.currentlyDestroying = building;
  270. this.currentlyDestroying.DestroyedEvent += this.EndCurrentDestroyAction;
  271. }
  272. private void FastForward(double time)
  273. {
  274. if (this.isDestroying || this.destroyQueue.Count == 0)
  275. {
  276. UnityEngine.Debug.LogWarning("Cant fastforward since " + ((!this.isDestroying) ? "the destroyqueue is empty" : "there is a destroyment in progress!!"));
  277. return;
  278. }
  279. this.DestroyNext(Timing.time - time);
  280. }
  281. private void UpdateNotification()
  282. {
  283. HUDView hudview = UnityEngine.Object.FindObjectOfType<HUDView>();
  284. if (hudview != null)
  285. {
  286. hudview.UpdateDemolishQueueButton();
  287. }
  288. }
  289. private void OnGridDeserialized()
  290. {
  291. this.UpdateNotification();
  292. }
  293. public void FinishAll()
  294. {
  295. if (this.isDestroying)
  296. {
  297. this.currentlyDestroying.FinishDemolishing();
  298. }
  299. IEnumerator enumerator = this.destroyQueue.GetEnumerator();
  300. try
  301. {
  302. while (enumerator.MoveNext())
  303. {
  304. object obj = enumerator.Current;
  305. Building building = (Building)obj;
  306. building.StartDemolishing(Timing.time);
  307. building.FinishDemolishing();
  308. }
  309. }
  310. finally
  311. {
  312. IDisposable disposable;
  313. if ((disposable = (enumerator as IDisposable)) != null)
  314. {
  315. disposable.Dispose();
  316. }
  317. }
  318. }
  319. public List<Building> DemolishList
  320. {
  321. get
  322. {
  323. List<Building> list = new List<Building>();
  324. if (this.currentlyDestroying != null)
  325. {
  326. list.Add(this.currentlyDestroying);
  327. }
  328. list.AddRange(this.destroyQueue.List);
  329. return list;
  330. }
  331. }
  332. [SelfReference]
  333. public Builder builder;
  334. [SelfReference]
  335. public Serializing serializing;
  336. private List<Building> selectedBuildings = new List<Building>();
  337. private List<Road> selectedRoads = new List<Road>();
  338. private Building currentlyDestroying;
  339. private PriorityQueue<Building> destroyQueue = new PriorityQueue<Building>(new Destroyer.DestroyBuildingComparer());
  340. public delegate void QueueChangedEventHandler();
  341. private class DestroyBuildingComparer : IComparer<Building>
  342. {
  343. public int Compare(Building x, Building y)
  344. {
  345. return x.SpotInDestroyQueue.CompareTo(y.SpotInDestroyQueue);
  346. }
  347. }
  348. }
  349. }