You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

233 rivejä
7.8 KiB

  1. using System;
  2. using UnityEngine;
  3. namespace SUISSEngine
  4. {
  5. public class Road : MonoBehaviour
  6. {
  7. public static Road GetRoadOfTypeAt(IsometricGrid grid, GridIndex index, int type, bool includeBridges)
  8. {
  9. if (index.u >= 0 && index.u < grid.Size.u && index.v >= 0 && index.v < grid.Size.v)
  10. {
  11. GridTile tile = grid[index].Tile;
  12. if (tile != null)
  13. {
  14. try
  15. {
  16. Road component = tile.GetComponent<Road>();
  17. if (component != null && Road.RoadsMatch(component.type, type, includeBridges))
  18. {
  19. return component;
  20. }
  21. }
  22. catch (UnityException)
  23. {
  24. return null;
  25. }
  26. }
  27. }
  28. return null;
  29. }
  30. public static bool RoadExists(IsometricGrid grid, GridIndex index, int type, bool includeBridges)
  31. {
  32. return Road.GetRoadOfTypeAt(grid, index, type, includeBridges) != null;
  33. }
  34. public static bool RoadExists(IsometricGrid grid, GridIndex index, int type)
  35. {
  36. return Road.RoadExists(grid, index, type, false);
  37. }
  38. public static bool RoadsMatch(int type1, int type2, bool includeBridges)
  39. {
  40. if (includeBridges)
  41. {
  42. return (type1 & type2) > 0;
  43. }
  44. return type1 == type2;
  45. }
  46. public GridTile gridTile
  47. {
  48. get
  49. {
  50. if (this._gridTile == null)
  51. {
  52. this._gridTile = base.GetComponent<GridTile>();
  53. }
  54. return this._gridTile;
  55. }
  56. }
  57. public GameObject getBridgePrefab(int roadType)
  58. {
  59. if (roadType == 1)
  60. {
  61. return this.bridgePrefabs[0];
  62. }
  63. if (roadType == 2)
  64. {
  65. return this.bridgePrefabs[1];
  66. }
  67. if (roadType == 4)
  68. {
  69. return this.bridgePrefabs[2];
  70. }
  71. return null;
  72. }
  73. public bool canBuildBridgeOver(int roadType)
  74. {
  75. return this.getBridgePrefab(roadType) != null;
  76. }
  77. public void UpdateSprite()
  78. {
  79. SpriteRenderer component = base.GetComponent<SpriteRenderer>();
  80. if (component == null)
  81. {
  82. return;
  83. }
  84. this._spriteIndex = 0;
  85. GridTile gridTile = this.gridTile;
  86. if (gridTile != null)
  87. {
  88. GridElement element = gridTile.element;
  89. if (element != null)
  90. {
  91. IsometricGrid grid = element.Grid;
  92. if (grid != null)
  93. {
  94. GridIndex index = element.Index;
  95. index.v--;
  96. if (this.InteractsWithNeighbour(grid, index, this.type, this.interactionMask))
  97. {
  98. this._spriteIndex |= 1;
  99. }
  100. index.v++;
  101. index.u++;
  102. if (this.InteractsWithNeighbour(grid, index, this.type, this.interactionMask))
  103. {
  104. this._spriteIndex |= 2;
  105. }
  106. index.u--;
  107. index.v++;
  108. if (this.InteractsWithNeighbour(grid, index, this.type, this.interactionMask))
  109. {
  110. this._spriteIndex |= 4;
  111. }
  112. index.v--;
  113. index.u--;
  114. if (this.InteractsWithNeighbour(grid, index, this.type, this.interactionMask))
  115. {
  116. this._spriteIndex |= 8;
  117. }
  118. index.u++;
  119. }
  120. }
  121. }
  122. Sprite sprite = null;
  123. if (this.sprites != null && this._spriteIndex < this.sprites.Length)
  124. {
  125. sprite = this.sprites[this._spriteIndex];
  126. if (this.foreground != null && this._spriteIndex < this.foreground.Length && this.foreground[this._spriteIndex] != null)
  127. {
  128. if (this._foregroundRenderer == null)
  129. {
  130. this._foregroundRenderer = this.createSpriteRendererAt(this.gridTile.index.u, this.gridTile.index.v);
  131. }
  132. this._foregroundRenderer.sprite = this.foreground[this._spriteIndex];
  133. }
  134. }
  135. component.sprite = sprite;
  136. }
  137. public bool IsAccessableFrom(Vehicle.Direction dir, int vehicleType)
  138. {
  139. if (this.IsNormalRoad)
  140. {
  141. return true;
  142. }
  143. bool flag = this._spriteIndex == 2 || this._spriteIndex == 8 || this._spriteIndex == 10;
  144. bool flag2 = dir == Vehicle.Direction.East || dir == Vehicle.Direction.West;
  145. if (this.type == 3)
  146. {
  147. if (vehicleType == 1)
  148. {
  149. return flag ^ flag2;
  150. }
  151. return !(flag ^ flag2);
  152. }
  153. else if (this.type == 5)
  154. {
  155. if (vehicleType == 4)
  156. {
  157. return flag ^ flag2;
  158. }
  159. return !(flag ^ flag2);
  160. }
  161. else
  162. {
  163. if (this.type != 6)
  164. {
  165. return false;
  166. }
  167. if (vehicleType == 4)
  168. {
  169. return flag ^ flag2;
  170. }
  171. return !(flag ^ flag2);
  172. }
  173. }
  174. public bool IsNormalRoad
  175. {
  176. get
  177. {
  178. return this.type == 1 || this.type == 2 || this.type == 4;
  179. }
  180. }
  181. private SpriteRenderer createSpriteRendererAt(int u, int v)
  182. {
  183. GameObject gameObject = new GameObject();
  184. gameObject.transform.parent = base.transform;
  185. gameObject.transform.localScale = Vector3.one;
  186. gameObject.name = "RoadForeground" + u + "." + v;
  187. gameObject.transform.localPosition = Vector2.zero;
  188. SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
  189. spriteRenderer.sortingOrder = GridTile.GetSortingOrder(gridTile.index, gridTile.size) + 3;
  190. return spriteRenderer;
  191. }
  192. private bool InteractsWithNeighbour(IsometricGrid grid, GridIndex index, int type, int mask)
  193. {
  194. Road roadOfTypeAt = Road.GetRoadOfTypeAt(grid, index, type, true);
  195. return !(roadOfTypeAt == null) && (roadOfTypeAt.type & type & mask) > 0;
  196. }
  197. public const int TypeRoad = 1;
  198. public const int TypePath = 2;
  199. public const int TypeWater = 4;
  200. public int type;
  201. [Tooltip("Used to mask interaction with other road tiles. Needed for bridges.")]
  202. public int interactionMask = 255;
  203. public Sprite[] sprites = new Sprite[16];
  204. public Sprite[] foreground = new Sprite[16];
  205. public GameObject[] bridgePrefabs = new GameObject[3];
  206. private int _spriteIndex;
  207. private SpriteRenderer _foregroundRenderer;
  208. private GridTile _gridTile;
  209. }
  210. }