Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

923 рядки
25 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using CIGEnums;
  5. using SUISS.Core;
  6. using UnityEngine;
  7. namespace SUISSEngine
  8. {
  9. public class Builder : MonoBehaviour
  10. {
  11. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  12. public event Builder.TileBuilt OnTileBuild;
  13. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  14. public event Builder.TileDestroyed OnTileDestroyed;
  15. protected virtual void Awake()
  16. {
  17. this._isBuilding = false;
  18. this._currentTile = null;
  19. this._isBuildingRoad = false;
  20. this._currentRoadPrefab = null;
  21. this._currentRoadType = -1;
  22. this._currentRoadTiles = new List<GridTile>();
  23. this._removedRoadTiles = new List<KeyValuePair<GridTile, GridIndex>>();
  24. this._originalIndex = GridIndex.invalid;
  25. this._tilesHidden = false;
  26. this._roadsHidden = false;
  27. }
  28. protected virtual void OnGridDeserialized()
  29. {
  30. }
  31. private void OnDestroy()
  32. {
  33. this.OnTileBuild = null;
  34. this.OnTileDestroyed = null;
  35. if (SingletonMonobehaviour<OverlayManager>.IsAvailable)
  36. {
  37. SingletonMonobehaviour<OverlayManager>.Instance.EnableInteractionRequest(this._hideOverlayRequest);
  38. SingletonMonobehaviour<OverlayManager>.Instance.EnableInteractionRequest(this._usingOverlayRequest);
  39. }
  40. }
  41. public bool isBuilding
  42. {
  43. get
  44. {
  45. return this._isBuilding;
  46. }
  47. }
  48. public GridTile currentTile
  49. {
  50. get
  51. {
  52. return this._currentTile;
  53. }
  54. }
  55. public bool isBuildingRoad
  56. {
  57. get
  58. {
  59. return this._isBuildingRoad;
  60. }
  61. }
  62. public int currentRoadType
  63. {
  64. get
  65. {
  66. return this._currentRoadType;
  67. }
  68. }
  69. public virtual bool TilesHidden
  70. {
  71. get
  72. {
  73. return this._tilesHidden;
  74. }
  75. set
  76. {
  77. if (value != this._tilesHidden)
  78. {
  79. this._tilesHidden = value;
  80. if (this.grid != null)
  81. {
  82. GridSize size = this.grid.Size;
  83. for (int i = 0; i < size.v; i++)
  84. {
  85. for (int j = 0; j < size.u; j++)
  86. {
  87. GridTile tile = this.grid[j, i].Tile;
  88. if (tile != null && tile.GetComponent<Road>() == null && tile.GetComponent<Scenery>() == null)
  89. {
  90. tile.hidden = this._tilesHidden;
  91. }
  92. }
  93. }
  94. if (this._tilesHidden)
  95. {
  96. SingletonMonobehaviour<OverlayManager>.Instance.DisableInteractionRequest(this._hideOverlayRequest);
  97. }
  98. else
  99. {
  100. SingletonMonobehaviour<OverlayManager>.Instance.EnableInteractionRequest(this._hideOverlayRequest);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. public virtual bool roadsHidden
  107. {
  108. get
  109. {
  110. return this._roadsHidden;
  111. }
  112. set
  113. {
  114. if (value != this._roadsHidden)
  115. {
  116. this._roadsHidden = value;
  117. if (this.grid != null)
  118. {
  119. GridSize size = this.grid.Size;
  120. for (int i = 0; i < size.v; i++)
  121. {
  122. for (int j = 0; j < size.u; j++)
  123. {
  124. GridTile tile = this.grid[j, i].Tile;
  125. if (tile != null && tile.GetComponent<Road>() != null)
  126. {
  127. tile.hidden = this._roadsHidden;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. public void BeginBuild(GameObject prefab)
  136. {
  137. this.BeginBuild(prefab, GridIndex.invalid, false);
  138. }
  139. public void BeginBuild(GameObject prefab, bool mirrored)
  140. {
  141. this.BeginBuild(prefab, GridIndex.invalid, mirrored);
  142. }
  143. public void BeginBuild(GameObject prefab, GridIndex indexHint)
  144. {
  145. this.BeginBuild(prefab, indexHint, false);
  146. }
  147. public void BeginBuild(GameObject prefab, GridIndex indexHint, bool mirrored)
  148. {
  149. if (this._isBuilding)
  150. {
  151. UnityEngine.Debug.LogWarning("Can't begin building because this Builder is already building.");
  152. return;
  153. }
  154. if (this.grid == null)
  155. {
  156. UnityEngine.Debug.LogError("Can't begin building because no grid could be found.");
  157. return;
  158. }
  159. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(prefab);
  160. GridTile component = gameObject.GetComponent<GridTile>();
  161. if (component == null)
  162. {
  163. UnityEngine.Debug.LogError("Can't build " + prefab + " because it has no GridTile component.");
  164. UnityEngine.Object.Destroy(gameObject);
  165. return;
  166. }
  167. gameObject.name = prefab.name;
  168. GridSize size = component.size;
  169. if (mirrored)
  170. {
  171. size = new GridSize(size.v, size.u);
  172. }
  173. if (indexHint.isInvalid)
  174. {
  175. indexHint = this.grid.ConvertIslandCoordinateToGridIndex(this.island.islandCamera.transform.position);
  176. if (indexHint.isInvalid)
  177. {
  178. indexHint.u = this.grid.Size.u / 2;
  179. indexHint.v = this.grid.Size.v / 2;
  180. }
  181. }
  182. indexHint.u += size.u / 2;
  183. if (indexHint.u >= this.grid.Size.u)
  184. {
  185. indexHint.u = this.grid.Size.u - 1;
  186. }
  187. indexHint.v += size.v / 2;
  188. if (indexHint.v >= this.grid.Size.v)
  189. {
  190. indexHint.v = this.grid.Size.v - 1;
  191. }
  192. GridIndex index = indexHint;
  193. if (!this.CanBuildTile(component, this.grid, index, mirrored, false))
  194. {
  195. float num = (float)(this.grid.Size.u + this.grid.Size.v);
  196. bool flag = false;
  197. for (int i = 1; i < this.grid.Size.u + this.grid.Size.v; i++)
  198. {
  199. GridIndex gridIndex;
  200. for (int j = -i; j <= i; j++)
  201. {
  202. gridIndex.u = indexHint.u + j;
  203. gridIndex.v = indexHint.v - i;
  204. float num2 = Mathf.Sqrt((float)((indexHint.u - gridIndex.u) * (indexHint.u - gridIndex.u) + (indexHint.v - gridIndex.v) * (indexHint.v - gridIndex.v)));
  205. if (num2 < num && this.CanBuildTile(component, this.grid, gridIndex, mirrored, false))
  206. {
  207. num = num2;
  208. index = gridIndex;
  209. flag = true;
  210. }
  211. gridIndex.v = indexHint.v + i;
  212. num2 = Mathf.Sqrt((float)((indexHint.u - gridIndex.u) * (indexHint.u - gridIndex.u) + (indexHint.v - gridIndex.v) * (indexHint.v - gridIndex.v)));
  213. if (num2 < num && this.CanBuildTile(component, this.grid, gridIndex, mirrored, false))
  214. {
  215. num = num2;
  216. index = gridIndex;
  217. flag = true;
  218. }
  219. }
  220. for (int k = -i + 1; k <= i - 1; k++)
  221. {
  222. gridIndex.v = indexHint.v + k;
  223. gridIndex.u = indexHint.u - i;
  224. float num2 = Mathf.Sqrt((float)((indexHint.u - gridIndex.u) * (indexHint.u - gridIndex.u) + (indexHint.v - gridIndex.v) * (indexHint.v - gridIndex.v)));
  225. if (num2 < num && this.CanBuildTile(component, this.grid, gridIndex, mirrored, false))
  226. {
  227. num = num2;
  228. index = gridIndex;
  229. flag = true;
  230. }
  231. gridIndex.u = indexHint.u + i;
  232. num2 = Mathf.Sqrt((float)((indexHint.u - gridIndex.u) * (indexHint.u - gridIndex.u) + (indexHint.v - gridIndex.v) * (indexHint.v - gridIndex.v)));
  233. if (num2 < num && this.CanBuildTile(component, this.grid, gridIndex, mirrored, false))
  234. {
  235. num = num2;
  236. index = gridIndex;
  237. flag = true;
  238. }
  239. }
  240. if (flag)
  241. {
  242. break;
  243. }
  244. }
  245. }
  246. this._isBuilding = true;
  247. this._currentTile = component;
  248. component.status = GridTile.Status.Preview;
  249. this.UpdateCurrentTile(this._currentTile, index, mirrored);
  250. if (this.island.input != null)
  251. {
  252. this.island.input.CenterViewOn(this._currentTile.gameObject, true);
  253. }
  254. SingletonMonobehaviour<OverlayManager>.Instance.DisableInteractionRequest(this._usingOverlayRequest);
  255. }
  256. public void BeginMove(GameObject clone)
  257. {
  258. if (this._isBuilding)
  259. {
  260. UnityEngine.Debug.LogWarning("Can't moving building because this Builder is already building.");
  261. return;
  262. }
  263. if (this.grid == null)
  264. {
  265. UnityEngine.Debug.LogError("Can't begin moving because no grid could be found.");
  266. return;
  267. }
  268. GridTile component = clone.GetComponent<GridTile>();
  269. if (component == null)
  270. {
  271. UnityEngine.Debug.LogError("Can't move " + clone + " because it has no GridTile component.");
  272. UnityEngine.Object.Destroy(clone);
  273. return;
  274. }
  275. GridIndex index = component.index;
  276. this.WillDestroyTile(component, index, component.size);
  277. this.grid.RemoveAtIndex(index, component.size, false);
  278. if (this.TilesHidden)
  279. {
  280. component.hidden = false;
  281. }
  282. this._originalIndex = index;
  283. this._isBuilding = true;
  284. this._currentTile = component;
  285. component.status = GridTile.Status.Moving;
  286. this.UpdateCurrentTile(this._currentTile, index, component.mirrored);
  287. this.DidDestroyTile(component, index, component.size);
  288. if (this.island.input != null)
  289. {
  290. this.island.input.CenterViewOn(this._currentTile.gameObject, true);
  291. }
  292. SingletonMonobehaviour<OverlayManager>.Instance.DisableInteractionRequest(this._usingOverlayRequest);
  293. }
  294. public void UpdateBuild(GridIndex newIndex)
  295. {
  296. if (!this._isBuilding)
  297. {
  298. return;
  299. }
  300. this.UpdateBuild(newIndex, this._currentTile.mirrored);
  301. }
  302. public void UpdateBuild(bool mirrored)
  303. {
  304. if (!this._isBuilding)
  305. {
  306. return;
  307. }
  308. this.UpdateBuild(this._currentTile.index, mirrored);
  309. }
  310. public void UpdateBuild(GridIndex newIndex, bool mirrored)
  311. {
  312. if (!this._isBuilding)
  313. {
  314. return;
  315. }
  316. if (newIndex.isInvalid)
  317. {
  318. newIndex = this._currentTile.index;
  319. }
  320. if (mirrored && !this._currentTile.canMirror)
  321. {
  322. mirrored = false;
  323. }
  324. this.UpdateCurrentTile(this._currentTile, newIndex, mirrored);
  325. }
  326. public void CancelBuild()
  327. {
  328. if (!this._isBuilding)
  329. {
  330. return;
  331. }
  332. SingletonMonobehaviour<OverlayManager>.Instance.EnableInteractionRequest(this._usingOverlayRequest);
  333. bool flag = true;
  334. if (this._currentTile.status == GridTile.Status.Moving && this.BuildAt(this._currentTile, this._originalIndex, this._currentTile.mirrored, false))
  335. {
  336. flag = false;
  337. }
  338. if (flag)
  339. {
  340. this.DestroyTile(this._currentTile);
  341. }
  342. this._currentTile = null;
  343. this._isBuilding = false;
  344. this._originalIndex = GridIndex.invalid;
  345. }
  346. public GridTile FinishBuild()
  347. {
  348. if (!this._isBuilding)
  349. {
  350. return null;
  351. }
  352. SingletonMonobehaviour<OverlayManager>.Instance.EnableInteractionRequest(this._usingOverlayRequest);
  353. if (this.BuildAt(this._currentTile, this._currentTile.index, this._currentTile.mirrored, false))
  354. {
  355. GridTile currentTile = this._currentTile;
  356. this._currentTile = null;
  357. this._isBuilding = false;
  358. this._originalIndex = GridIndex.invalid;
  359. return currentTile;
  360. }
  361. this.CancelBuild();
  362. return null;
  363. }
  364. public void BeginRoad(GameObject prefab)
  365. {
  366. if (this._isBuildingRoad)
  367. {
  368. UnityEngine.Debug.LogWarning("Can't begin building roads because this Builder is already building roads.");
  369. return;
  370. }
  371. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(prefab);
  372. Road component = gameObject.GetComponent<Road>();
  373. if (component == null)
  374. {
  375. UnityEngine.Object.Destroy(gameObject);
  376. UnityEngine.Debug.LogWarning("Can't begin building road " + prefab + " because it has no Road component.");
  377. return;
  378. }
  379. this._currentRoadType = component.type;
  380. UnityEngine.Object.Destroy(gameObject);
  381. this._currentRoadPrefab = prefab;
  382. this._currentRoadTiles.Clear();
  383. this._isBuildingRoad = true;
  384. VehicleManager vehicleManager = this.island.vehicleManager;
  385. if (vehicleManager != null)
  386. {
  387. vehicleManager.WillBeginBuildingRoads(this._currentRoadType);
  388. }
  389. SingletonMonobehaviour<OverlayManager>.Instance.DisableInteractionRequest(this._usingOverlayRequest);
  390. }
  391. public void UpdateRoad(GridIndex index)
  392. {
  393. if (!this._isBuildingRoad)
  394. {
  395. return;
  396. }
  397. if (this.grid == null)
  398. {
  399. return;
  400. }
  401. if (index.isInvalid || index.u >= this.grid.Size.u || index.v >= this.grid.Size.v)
  402. {
  403. return;
  404. }
  405. GameObject gameObject = this.createRoadObject(this._currentRoadPrefab);
  406. if (gameObject == null)
  407. {
  408. return;
  409. }
  410. GridTile component = gameObject.GetComponent<GridTile>();
  411. Road component2 = component.GetComponent<Road>();
  412. GridElement gridElement = this.grid[index.u, index.v];
  413. if (gridElement.Tile == null)
  414. {
  415. if (!this.BuildAt(component, index, false, false))
  416. {
  417. UnityEngine.Object.Destroy(component.gameObject);
  418. }
  419. else
  420. {
  421. this._currentRoadTiles.Add(component);
  422. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.ButtonClick);
  423. }
  424. }
  425. else if (gridElement.Tile.GetComponent<Road>() != null)
  426. {
  427. GridTile tile = gridElement.Tile;
  428. if (component2.canBuildBridgeOver(tile.GetComponent<Road>().type))
  429. {
  430. GameObject bridgePrefab = component2.getBridgePrefab(tile.GetComponent<Road>().type);
  431. GameObject gameObject2 = this.createRoadObject(bridgePrefab);
  432. if (gameObject2 != null)
  433. {
  434. this.grid.RemoveAtIndex(index, tile.currentSize, false);
  435. tile.gameObject.SetActive(false);
  436. this._removedRoadTiles.Add(new KeyValuePair<GridTile, GridIndex>(tile, index));
  437. GridTile component3 = gameObject2.GetComponent<GridTile>();
  438. this.BuildAt(component3, index, false, true);
  439. this._currentRoadTiles.Add(component3);
  440. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.ButtonClick);
  441. }
  442. }
  443. component.status = GridTile.Status.Destroyed;
  444. UnityEngine.Object.Destroy(component.gameObject);
  445. }
  446. else
  447. {
  448. component.status = GridTile.Status.Destroyed;
  449. UnityEngine.Object.Destroy(component.gameObject);
  450. }
  451. }
  452. private GameObject createRoadObject(GameObject prefab)
  453. {
  454. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(prefab);
  455. GridTile component = gameObject.GetComponent<GridTile>();
  456. if (component == null)
  457. {
  458. UnityEngine.Debug.LogError("Can't build " + prefab + " because it has no GridTile component.");
  459. UnityEngine.Object.Destroy(gameObject);
  460. return null;
  461. }
  462. Road component2 = component.GetComponent<Road>();
  463. if (component2 == null)
  464. {
  465. UnityEngine.Debug.LogError("Can't build " + prefab + " because it has no Road component.");
  466. component.status = GridTile.Status.Destroyed;
  467. UnityEngine.Object.Destroy(gameObject);
  468. return null;
  469. }
  470. gameObject.name = prefab.name;
  471. return gameObject;
  472. }
  473. public void CancelRoad()
  474. {
  475. if (!this._isBuildingRoad)
  476. {
  477. return;
  478. }
  479. SingletonMonobehaviour<OverlayManager>.Instance.EnableInteractionRequest(this._usingOverlayRequest);
  480. foreach (GridTile tile in this._currentRoadTiles)
  481. {
  482. this.DestroyTile(tile);
  483. }
  484. foreach (KeyValuePair<GridTile, GridIndex> keyValuePair in this._removedRoadTiles)
  485. {
  486. if (this.grid == null)
  487. {
  488. return;
  489. }
  490. if (keyValuePair.Key != null)
  491. {
  492. keyValuePair.Key.gameObject.SetActive(true);
  493. this.grid.AddTileAt(keyValuePair.Key, keyValuePair.Value, true);
  494. this.NotifyRoadsOfRoadChange(keyValuePair.Key);
  495. }
  496. else
  497. {
  498. UnityEngine.Debug.LogWarning("tile.Key == null");
  499. }
  500. }
  501. VehicleManager vehicleManager = this.island.vehicleManager;
  502. if (vehicleManager != null)
  503. {
  504. vehicleManager.DidFinishBuildingRoads(this._currentRoadType);
  505. }
  506. this._currentRoadPrefab = null;
  507. this._currentRoadType = -1;
  508. this._currentRoadTiles.Clear();
  509. this._removedRoadTiles.Clear();
  510. this._isBuildingRoad = false;
  511. }
  512. public void FinishRoad()
  513. {
  514. if (!this._isBuildingRoad)
  515. {
  516. return;
  517. }
  518. SingletonMonobehaviour<OverlayManager>.Instance.EnableInteractionRequest(this._usingOverlayRequest);
  519. VehicleManager vehicleManager = this.island.vehicleManager;
  520. if (vehicleManager != null)
  521. {
  522. vehicleManager.DidFinishBuildingRoads(this._currentRoadType);
  523. }
  524. foreach (KeyValuePair<GridTile, GridIndex> keyValuePair in this._removedRoadTiles)
  525. {
  526. if (keyValuePair.Key != null)
  527. {
  528. keyValuePair.Key.status = GridTile.Status.Destroyed;
  529. UnityEngine.Object.Destroy(keyValuePair.Key.gameObject);
  530. }
  531. else
  532. {
  533. UnityEngine.Debug.LogWarning("tile.Key == null");
  534. }
  535. }
  536. this._currentRoadPrefab = null;
  537. this._currentRoadType = -1;
  538. this._currentRoadTiles.Clear();
  539. this._removedRoadTiles.Clear();
  540. this._isBuildingRoad = false;
  541. }
  542. public bool BuildAt(GameObject prefab, GridIndex index, bool mirrored, bool forced)
  543. {
  544. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(prefab);
  545. GridTile component = gameObject.GetComponent<GridTile>();
  546. if (component == null)
  547. {
  548. UnityEngine.Debug.LogError("Can't build " + prefab + " because it has no GridTile component.");
  549. UnityEngine.Object.Destroy(gameObject);
  550. return false;
  551. }
  552. gameObject.name = prefab.name;
  553. bool flag = this.BuildAt(component, index, mirrored, forced);
  554. if (!flag && component.status == GridTile.Status.Moving)
  555. {
  556. flag = this.BuildAt(component, this._originalIndex, mirrored, forced);
  557. }
  558. if (!flag)
  559. {
  560. this.DestroyTile(component);
  561. }
  562. return flag;
  563. }
  564. public bool BuildAt(GridTile tile, GridIndex index, bool mirrored, bool forced)
  565. {
  566. if (this.grid == null)
  567. {
  568. UnityEngine.Debug.LogError("Can't build anything because no grid could be found.");
  569. return false;
  570. }
  571. if (!this.CanBuildTile(tile, this.grid, index, mirrored, forced))
  572. {
  573. return false;
  574. }
  575. GridSize size = tile.size;
  576. if (mirrored)
  577. {
  578. size = new GridSize(size.v, size.u);
  579. }
  580. Road component = tile.GetComponent<Road>();
  581. VehicleManager vehicleManager = this.island.vehicleManager;
  582. this.WillBuildTile(tile, index, size, mirrored);
  583. tile.transform.parent = this.grid.transform;
  584. tile.index = index;
  585. if (component == null)
  586. {
  587. tile.hidden = this._tilesHidden;
  588. }
  589. else
  590. {
  591. tile.hidden = this._roadsHidden;
  592. }
  593. tile.mirrored = mirrored;
  594. if (tile.status == GridTile.Status.Moving)
  595. {
  596. this.grid.MoveTileFrom(tile, this._originalIndex, true);
  597. }
  598. else
  599. {
  600. this.grid.AddTileAt(tile, index, true);
  601. }
  602. if (component != null && vehicleManager != null)
  603. {
  604. vehicleManager.DidBuildRoadAt(index, component);
  605. }
  606. tile.UpdateTransform();
  607. if (component == null && vehicleManager != null)
  608. {
  609. vehicleManager.DidBuildTileAt(index, tile);
  610. }
  611. SpriteRenderer component2 = tile.GetComponent<SpriteRenderer>();
  612. if (component2 != null)
  613. {
  614. component2.color = Color.white;
  615. }
  616. if (this.OnTileBuild != null)
  617. {
  618. this.OnTileBuild(tile);
  619. }
  620. tile.status = GridTile.Status.Created;
  621. this.NotifyRoadsOfRoadChange(tile);
  622. this.DidBuildTile(tile, index, size, mirrored);
  623. return true;
  624. }
  625. public void DestroyTile(GridTile tile)
  626. {
  627. if (tile == null)
  628. {
  629. return;
  630. }
  631. GridIndex index = tile.index;
  632. GridSize currentSize = tile.currentSize;
  633. this.WillDestroyTile(tile, index, currentSize);
  634. if (tile.status != GridTile.Status.Preview)
  635. {
  636. GridElement element = tile.element;
  637. if (element != null)
  638. {
  639. IsometricGrid isometricGrid = element.Grid;
  640. if (isometricGrid != null)
  641. {
  642. Road component = tile.GetComponent<Road>();
  643. VehicleManager vehicleManager = this.island.vehicleManager;
  644. if (component == null && vehicleManager != null)
  645. {
  646. vehicleManager.WillDestroyTileAt(element.Index, tile);
  647. }
  648. if (component != null && vehicleManager != null)
  649. {
  650. vehicleManager.WillDestroyRoadAt(element.Index, component);
  651. }
  652. isometricGrid.RemoveAtIndex(element.Index, currentSize, true);
  653. if (component != null)
  654. {
  655. this.NotifyRoadsOfRoadChange(tile);
  656. }
  657. }
  658. else
  659. {
  660. element.Tile = null;
  661. }
  662. }
  663. }
  664. GridTile.Status status = tile.status;
  665. tile.status = GridTile.Status.Destroyed;
  666. if (this.OnTileDestroyed != null)
  667. {
  668. this.OnTileDestroyed(tile, status);
  669. }
  670. UnityEngine.Object.Destroy(tile.gameObject);
  671. this.DidDestroyTile(tile, index, currentSize);
  672. }
  673. public bool CanBuild()
  674. {
  675. return this.CanBuildTile(this._currentTile, this._currentTile.grid, this._currentTile.index, false, false);
  676. }
  677. protected virtual bool CanBuildTile(GridTile tile, IsometricGrid grid, GridIndex index, bool mirrored, bool forced)
  678. {
  679. if (index.isInvalid || index.u >= grid.Size.u || index.v >= grid.Size.v)
  680. {
  681. return false;
  682. }
  683. if (mirrored && !tile.canMirror)
  684. {
  685. return false;
  686. }
  687. GridSize size = tile.size;
  688. if (mirrored)
  689. {
  690. size = new GridSize(size.v, size.u);
  691. }
  692. if (size.u > index.u + 1 || size.v > index.v + 1)
  693. {
  694. return false;
  695. }
  696. for (int i = index.v; i > index.v - size.v; i--)
  697. {
  698. for (int j = index.u; j > index.u - size.u; j--)
  699. {
  700. GridElement gridElement = grid[j, i];
  701. if (gridElement.Tile != null)
  702. {
  703. if (!forced || !(gridElement.Tile.GetComponent<Scenery>() == null))
  704. {
  705. return false;
  706. }
  707. UnityEngine.Debug.LogWarning(string.Format("In order to build {0} at {1}, we need to remove {2} at ({3},{4})", new object[]
  708. {
  709. tile.name,
  710. index,
  711. gridElement.Tile.name,
  712. j,
  713. i
  714. }));
  715. this.DestroyTile(gridElement.Tile);
  716. }
  717. if (!forced && !this.CanBuildTileOnElement(tile, grid, gridElement))
  718. {
  719. return false;
  720. }
  721. }
  722. }
  723. return true;
  724. }
  725. protected virtual bool IsElementTypeCompatible(int type, int required)
  726. {
  727. return type == required;
  728. }
  729. protected virtual bool CanBuildTileOnElement(GridTile tile, IsometricGrid grid, GridElement element)
  730. {
  731. return this.IsElementTypeCompatible(element.Type, tile.requiredGridType);
  732. }
  733. protected void UpdateCurrentTile(GridTile tile, GridIndex index, bool mirrored)
  734. {
  735. if (this.grid == null)
  736. {
  737. return;
  738. }
  739. if (index.u < tile.size.u - 1)
  740. {
  741. index.u = tile.size.u - 1;
  742. }
  743. if (index.u >= this.grid.Size.u)
  744. {
  745. index.u = this.grid.Size.u - 1;
  746. }
  747. if (index.v < tile.size.v - 1)
  748. {
  749. index.v = tile.size.v - 1;
  750. }
  751. if (index.v >= this.grid.Size.v)
  752. {
  753. index.v = tile.size.v - 1;
  754. }
  755. tile.transform.parent = this.grid.transform;
  756. tile.index = index;
  757. tile.mirrored = mirrored;
  758. tile.UpdateTransform();
  759. Vector3 localPosition = tile.transform.localPosition;
  760. localPosition.z = this.grid.zNear;
  761. tile.transform.localPosition = localPosition;
  762. SpriteRenderer component = tile.GetComponent<SpriteRenderer>();
  763. if (component != null)
  764. {
  765. Color color;
  766. if (this.CanBuildTile(tile, this.grid, index, tile.mirrored, false))
  767. {
  768. color = Color.white;
  769. }
  770. else
  771. {
  772. color = Color.red;
  773. }
  774. color.a *= 0.5f;
  775. component.color = color;
  776. }
  777. }
  778. protected virtual void WillBuildTile(GridTile tile, GridIndex index, GridSize size, bool mirrored)
  779. {
  780. }
  781. protected virtual void DidBuildTile(GridTile tile, GridIndex index, GridSize size, bool mirrored)
  782. {
  783. }
  784. protected virtual void WillDestroyTile(GridTile tile, GridIndex index, GridSize size)
  785. {
  786. }
  787. protected virtual void DidDestroyTile(GridTile tile, GridIndex index, GridSize size)
  788. {
  789. }
  790. private static void TryUpdateRoadAt(Road original, IsometricGrid grid, GridIndex index)
  791. {
  792. if (index.isInvalid || index.u >= grid.Size.u || index.v >= grid.Size.v)
  793. {
  794. return;
  795. }
  796. GridTile tile = grid[index].Tile;
  797. if (tile != null)
  798. {
  799. Road component = tile.GetComponent<Road>();
  800. if (component != null && Road.RoadsMatch(component.type, original.type, true))
  801. {
  802. component.UpdateSprite();
  803. }
  804. }
  805. }
  806. private void NotifyBuildingsOfRoadChange(GridTile roadTile)
  807. {
  808. if (roadTile != null && roadTile.grid != null)
  809. {
  810. foreach (GridTile gridTile in roadTile.grid.GetNeighbourTiles(roadTile))
  811. {
  812. Building component = gridTile.GetComponent<Building>();
  813. if (component != null && component.checkForRoad)
  814. {
  815. component.OnAdjacentRoadsChanged(roadTile);
  816. }
  817. }
  818. }
  819. }
  820. private void NotifyRoadsOfRoadChange(GridTile tile)
  821. {
  822. if (this.grid == null)
  823. {
  824. return;
  825. }
  826. Road component = tile.GetComponent<Road>();
  827. if (component != null)
  828. {
  829. component.UpdateSprite();
  830. GridIndex index = tile.index;
  831. index.v--;
  832. Builder.TryUpdateRoadAt(component, this.grid, index);
  833. index.v++;
  834. index.u++;
  835. Builder.TryUpdateRoadAt(component, this.grid, index);
  836. index.u--;
  837. index.v++;
  838. Builder.TryUpdateRoadAt(component, this.grid, index);
  839. index.v--;
  840. index.u--;
  841. Builder.TryUpdateRoadAt(component, this.grid, index);
  842. index.u++;
  843. this.NotifyBuildingsOfRoadChange(tile);
  844. }
  845. }
  846. [SelfReference]
  847. public IsometricIsland island;
  848. [ChildReference]
  849. public IsometricGrid grid;
  850. private bool _isBuilding;
  851. private GridTile _currentTile;
  852. private bool _isBuildingRoad;
  853. private GameObject _currentRoadPrefab;
  854. private int _currentRoadType;
  855. private List<GridTile> _currentRoadTiles;
  856. private List<KeyValuePair<GridTile, GridIndex>> _removedRoadTiles;
  857. private GridIndex _originalIndex;
  858. private bool _tilesHidden;
  859. private bool _roadsHidden;
  860. private object _hideOverlayRequest = new object();
  861. private object _usingOverlayRequest = new object();
  862. public delegate void TileBuilt(GridTile tile);
  863. public delegate void TileDestroyed(GridTile tile, GridTile.Status priorStatus);
  864. }
  865. }