|
- using System;
- using UnityEngine;
-
- namespace SUISSEngine
- {
- public class Road : MonoBehaviour
- {
- public static Road GetRoadOfTypeAt(IsometricGrid grid, GridIndex index, int type, bool includeBridges)
- {
- if (index.u >= 0 && index.u < grid.Size.u && index.v >= 0 && index.v < grid.Size.v)
- {
- GridTile tile = grid[index].Tile;
- if (tile != null)
- {
- try
- {
- Road component = tile.GetComponent<Road>();
- if (component != null && Road.RoadsMatch(component.type, type, includeBridges))
- {
- return component;
- }
- }
- catch (UnityException)
- {
- return null;
- }
- }
- }
- return null;
- }
-
- public static bool RoadExists(IsometricGrid grid, GridIndex index, int type, bool includeBridges)
- {
- return Road.GetRoadOfTypeAt(grid, index, type, includeBridges) != null;
- }
-
- public static bool RoadExists(IsometricGrid grid, GridIndex index, int type)
- {
- return Road.RoadExists(grid, index, type, false);
- }
-
- public static bool RoadsMatch(int type1, int type2, bool includeBridges)
- {
- if (includeBridges)
- {
- return (type1 & type2) > 0;
- }
- return type1 == type2;
- }
-
- public GridTile gridTile
- {
- get
- {
- if (this._gridTile == null)
- {
- this._gridTile = base.GetComponent<GridTile>();
- }
- return this._gridTile;
- }
- }
-
- public GameObject getBridgePrefab(int roadType)
- {
- if (roadType == 1)
- {
- return this.bridgePrefabs[0];
- }
- if (roadType == 2)
- {
- return this.bridgePrefabs[1];
- }
- if (roadType == 4)
- {
- return this.bridgePrefabs[2];
- }
- return null;
- }
-
- public bool canBuildBridgeOver(int roadType)
- {
- return this.getBridgePrefab(roadType) != null;
- }
-
- public void UpdateSprite()
- {
- SpriteRenderer component = base.GetComponent<SpriteRenderer>();
- if (component == null)
- {
- return;
- }
- this._spriteIndex = 0;
- GridTile gridTile = this.gridTile;
- if (gridTile != null)
- {
- GridElement element = gridTile.element;
- if (element != null)
- {
- IsometricGrid grid = element.Grid;
- if (grid != null)
- {
- GridIndex index = element.Index;
- index.v--;
- if (this.InteractsWithNeighbour(grid, index, this.type, this.interactionMask))
- {
- this._spriteIndex |= 1;
- }
- index.v++;
- index.u++;
- if (this.InteractsWithNeighbour(grid, index, this.type, this.interactionMask))
- {
- this._spriteIndex |= 2;
- }
- index.u--;
- index.v++;
- if (this.InteractsWithNeighbour(grid, index, this.type, this.interactionMask))
- {
- this._spriteIndex |= 4;
- }
- index.v--;
- index.u--;
- if (this.InteractsWithNeighbour(grid, index, this.type, this.interactionMask))
- {
- this._spriteIndex |= 8;
- }
- index.u++;
- }
- }
- }
- Sprite sprite = null;
- if (this.sprites != null && this._spriteIndex < this.sprites.Length)
- {
- sprite = this.sprites[this._spriteIndex];
- if (this.foreground != null && this._spriteIndex < this.foreground.Length && this.foreground[this._spriteIndex] != null)
- {
- if (this._foregroundRenderer == null)
- {
- this._foregroundRenderer = this.createSpriteRendererAt(this.gridTile.index.u, this.gridTile.index.v);
- }
- this._foregroundRenderer.sprite = this.foreground[this._spriteIndex];
- }
- }
- component.sprite = sprite;
- }
-
- public bool IsAccessableFrom(Vehicle.Direction dir, int vehicleType)
- {
- if (this.IsNormalRoad)
- {
- return true;
- }
- bool flag = this._spriteIndex == 2 || this._spriteIndex == 8 || this._spriteIndex == 10;
- bool flag2 = dir == Vehicle.Direction.East || dir == Vehicle.Direction.West;
- if (this.type == 3)
- {
- if (vehicleType == 1)
- {
- return flag ^ flag2;
- }
- return !(flag ^ flag2);
- }
- else if (this.type == 5)
- {
- if (vehicleType == 4)
- {
- return flag ^ flag2;
- }
- return !(flag ^ flag2);
- }
- else
- {
- if (this.type != 6)
- {
- return false;
- }
- if (vehicleType == 4)
- {
- return flag ^ flag2;
- }
- return !(flag ^ flag2);
- }
- }
-
- public bool IsNormalRoad
- {
- get
- {
- return this.type == 1 || this.type == 2 || this.type == 4;
- }
- }
-
- private SpriteRenderer createSpriteRendererAt(int u, int v)
- {
- GameObject gameObject = new GameObject();
- gameObject.transform.parent = base.transform;
- gameObject.transform.localScale = Vector3.one;
- gameObject.name = "RoadForeground" + u + "." + v;
- gameObject.transform.localPosition = Vector2.zero;
- SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
- spriteRenderer.sortingOrder = GridTile.GetSortingOrder(gridTile.index, gridTile.size) + 3;
- return spriteRenderer;
- }
- private bool InteractsWithNeighbour(IsometricGrid grid, GridIndex index, int type, int mask)
- {
- Road roadOfTypeAt = Road.GetRoadOfTypeAt(grid, index, type, true);
- return !(roadOfTypeAt == null) && (roadOfTypeAt.type & type & mask) > 0;
- }
-
- public const int TypeRoad = 1;
-
- public const int TypePath = 2;
-
- public const int TypeWater = 4;
-
- public int type;
-
- [Tooltip("Used to mask interaction with other road tiles. Needed for bridges.")]
- public int interactionMask = 255;
-
- public Sprite[] sprites = new Sprite[16];
-
- public Sprite[] foreground = new Sprite[16];
-
- public GameObject[] bridgePrefabs = new GameObject[3];
-
- private int _spriteIndex;
-
- private SpriteRenderer _foregroundRenderer;
-
- private GridTile _gridTile;
- }
- }
|