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.
 
 
 

457 line
16 KiB

  1. using System;
  2. using CIG;
  3. using UnityEngine;
  4. namespace SUISSEngine
  5. {
  6. [RequireComponent(typeof(SpriteRenderer))]
  7. public class Vehicle : MonoBehaviour
  8. {
  9. public int RoadType
  10. {
  11. get
  12. {
  13. return this.roadType;
  14. }
  15. }
  16. public GridPoint CurrentLocation
  17. {
  18. get
  19. {
  20. return this.location;
  21. }
  22. }
  23. public Vehicle.Direction CurrentDirection
  24. {
  25. get
  26. {
  27. return this.direction;
  28. }
  29. }
  30. public bool IsStuck
  31. {
  32. get
  33. {
  34. return this.stuck;
  35. }
  36. }
  37. public Balloon Balloon
  38. {
  39. get
  40. {
  41. return this._balloon;
  42. }
  43. }
  44. protected virtual void Awake()
  45. {
  46. this.cachedName = base.name;
  47. this.originalScale = base.transform.localScale;
  48. }
  49. protected virtual void Start()
  50. {
  51. }
  52. protected virtual void Update()
  53. {
  54. float num = Time.deltaTime;
  55. bool flag = false;
  56. while (!this.stuck && num > 0f)
  57. {
  58. float num2 = this.location.DistanceTo(this.targetLocation);
  59. if ((double)num2 < 1E-06 && flag)
  60. {
  61. break;
  62. }
  63. float num3 = this.speed * num;
  64. if (num3 < num2)
  65. {
  66. GridPoint gridPoint = new GridPoint(0f, 0f);
  67. switch (this.direction)
  68. {
  69. case Vehicle.Direction.North:
  70. gridPoint = new GridPoint(0f, -this.speed);
  71. break;
  72. case Vehicle.Direction.East:
  73. gridPoint = new GridPoint(this.speed, 0f);
  74. break;
  75. case Vehicle.Direction.South:
  76. gridPoint = new GridPoint(0f, this.speed);
  77. break;
  78. case Vehicle.Direction.West:
  79. gridPoint = new GridPoint(-this.speed, 0f);
  80. break;
  81. }
  82. this.location.u = this.location.u + gridPoint.u * num;
  83. this.location.v = this.location.v + gridPoint.v * num;
  84. num2 -= num3;
  85. num = 0f;
  86. }
  87. else
  88. {
  89. this.location = this.targetLocation;
  90. this.FindDirection(true);
  91. flag = true;
  92. num -= num2 / this.speed;
  93. }
  94. }
  95. if (!this.stuck && this.requiredRoadType > 0)
  96. {
  97. GridIndex index;
  98. index.u = Mathf.FloorToInt(this.location.u);
  99. index.v = Mathf.FloorToInt(this.location.v);
  100. if (!Road.RoadExists(this.grid, index, this.roadType, this.canMoveOnBriges))
  101. {
  102. this.stuck = true;
  103. }
  104. }
  105. if (this.stuck)
  106. {
  107. IsometricIsland.GetParent(this).vehicleManager.VehicleStuck(this);
  108. }
  109. if (this.cachedName.StartsWith("Boat"))
  110. {
  111. Vector2 vector = base.transform.position;
  112. Vector2 vector2 = base.transform.position;
  113. vector += this.frontPosition[(int)this.direction];
  114. vector2 += this.backPosition[(int)this.direction];
  115. GridIndex index2 = this.grid.ConvertIslandCoordinateToGridIndex(vector);
  116. GridIndex index3 = this.grid.ConvertIslandCoordinateToGridIndex(vector2);
  117. bool flag2 = Road.RoadExists(this.grid, index2, 1, true) || Road.RoadExists(this.grid, index3, 1, true) || Road.RoadExists(this.grid, index2, 2, true) || Road.RoadExists(this.grid, index3, 2, true);
  118. this.spriteRenderer.enabled = !flag2;
  119. }
  120. this.UpdateTransform();
  121. }
  122. private void OnDestroy()
  123. {
  124. if (this._balloon != null)
  125. {
  126. this._balloon.CollectedEvent -= this.OnBalloonCollected;
  127. this._balloon.ExpiredEvent -= this.OnBalloonExpired;
  128. this._balloon = null;
  129. }
  130. }
  131. public static bool DirectionsAreOpposite(Vehicle.Direction dir1, Vehicle.Direction dir2)
  132. {
  133. return Math.Abs(dir1 - dir2) == 2;
  134. }
  135. public static GridIndex ApplyDirection(GridIndex index, Vehicle.Direction dir)
  136. {
  137. return new GridIndex(index.u + Vehicle.__du[(int)dir], index.v + Vehicle.__dv[(int)dir]);
  138. }
  139. public static Vehicle.Direction InvertDirection(Vehicle.Direction dir)
  140. {
  141. return dir ^ Vehicle.Direction.South;
  142. }
  143. public virtual void SpawnAt(GridIndex index, int roadType)
  144. {
  145. this.grid = IsometricIsland.GetParent(this).grid;
  146. if (this.grid == null)
  147. {
  148. UnityEngine.Debug.LogError("Vehicle " + this + " couldn't find a grid in SpawnAt().");
  149. return;
  150. }
  151. this.roadType = roadType;
  152. base.transform.parent = this.grid.transform;
  153. this.speed = UnityEngine.Random.Range(this.minSpeed, this.maxSpeed);
  154. this.location = new GridPoint(index);
  155. this.location.u = this.location.u + 0.5f;
  156. this.location.v = this.location.v + 0.5f;
  157. this.UpdateTransform();
  158. this.FindDirection(false);
  159. }
  160. public void ShowBalloon(Balloon balloon)
  161. {
  162. if (this._balloon != null)
  163. {
  164. UnityEngine.Debug.LogError("Can't show two balloons on a vehicle.");
  165. return;
  166. }
  167. if (balloon == null)
  168. {
  169. UnityEngine.Debug.LogError("Balloon to show on vehicle is null.");
  170. return;
  171. }
  172. this._balloon = balloon;
  173. this._balloon.CollectedEvent += this.OnBalloonCollected;
  174. this._balloon.ExpiredEvent += this.OnBalloonExpired;
  175. BalloonView balloonView = this._gridTileIconManager.SetIcon<BalloonView>(this._balloon.GridTileIcon);
  176. balloonView.Init(this._balloon);
  177. }
  178. protected virtual void FindDirection(bool dontTurnBack)
  179. {
  180. GridIndex[] array = new GridIndex[4];
  181. Vehicle.Direction[] array2 = new Vehicle.Direction[4];
  182. int num = 0;
  183. GridIndex gridIndex;
  184. gridIndex.u = Mathf.FloorToInt(this.location.u);
  185. gridIndex.v = Mathf.FloorToInt(this.location.v);
  186. if (!Road.RoadExists(this.grid, gridIndex, this.roadType, this.canMoveOnBriges))
  187. {
  188. this.stuck = true;
  189. return;
  190. }
  191. Road roadOfTypeAt = Road.GetRoadOfTypeAt(this.grid, gridIndex, this.roadType, true);
  192. if (roadOfTypeAt == null)
  193. {
  194. return;
  195. }
  196. GridIndex gridIndex2 = gridIndex;
  197. gridIndex2.v--;
  198. if (this.HasAccessableNeighbour(gridIndex2, Vehicle.Direction.North) && roadOfTypeAt.IsAccessableFrom(Vehicle.Direction.North, this.roadType))
  199. {
  200. array[num] = gridIndex2;
  201. array2[num] = Vehicle.Direction.North;
  202. num++;
  203. }
  204. gridIndex2.v++;
  205. gridIndex2.u++;
  206. if (this.HasAccessableNeighbour(gridIndex2, Vehicle.Direction.East) && roadOfTypeAt.IsAccessableFrom(Vehicle.Direction.East, this.roadType))
  207. {
  208. array[num] = gridIndex2;
  209. array2[num] = Vehicle.Direction.East;
  210. num++;
  211. }
  212. gridIndex2.u--;
  213. gridIndex2.v++;
  214. if (this.HasAccessableNeighbour(gridIndex2, Vehicle.Direction.South) && roadOfTypeAt.IsAccessableFrom(Vehicle.Direction.South, this.roadType))
  215. {
  216. array[num] = gridIndex2;
  217. array2[num] = Vehicle.Direction.South;
  218. num++;
  219. }
  220. gridIndex2.v--;
  221. gridIndex2.u--;
  222. if (this.HasAccessableNeighbour(gridIndex2, Vehicle.Direction.West) && roadOfTypeAt.IsAccessableFrom(Vehicle.Direction.West, this.roadType))
  223. {
  224. array[num] = gridIndex2;
  225. array2[num] = Vehicle.Direction.West;
  226. num++;
  227. }
  228. gridIndex2.u++;
  229. if (dontTurnBack && num > 1)
  230. {
  231. for (int i = 0; i < num; i++)
  232. {
  233. if (Vehicle.DirectionsAreOpposite(this.direction, array2[i]))
  234. {
  235. for (int j = i; j < num - 1; j++)
  236. {
  237. array[j] = array[j + 1];
  238. array2[j] = array2[j + 1];
  239. }
  240. num--;
  241. break;
  242. }
  243. }
  244. }
  245. if (num == 0)
  246. {
  247. this.stuck = true;
  248. }
  249. else
  250. {
  251. this.stuck = false;
  252. int num2 = UnityEngine.Random.Range(0, num);
  253. this.targetLocation = new GridPoint(array[num2]);
  254. this.targetLocation.u = this.targetLocation.u + 0.5f;
  255. this.targetLocation.v = this.targetLocation.v + 0.5f;
  256. this.SetDirection(array2[num2]);
  257. }
  258. }
  259. protected virtual void SetDirection(Vehicle.Direction direction)
  260. {
  261. this.direction = direction;
  262. if (this.animator != null)
  263. {
  264. this.animator.SetInteger("Direction", (int)this.direction);
  265. }
  266. else if (this.spriteRenderer != null)
  267. {
  268. Sprite sprite = null;
  269. bool flag = false;
  270. if (this.sprites != null && (Vehicle.Direction)this.sprites.Length > this.direction)
  271. {
  272. sprite = this.sprites[(int)this.direction];
  273. }
  274. if (sprite == null)
  275. {
  276. Vehicle.Direction direction2 = Vehicle.Direction.North;
  277. switch (this.direction)
  278. {
  279. case Vehicle.Direction.North:
  280. direction2 = Vehicle.Direction.West;
  281. break;
  282. case Vehicle.Direction.East:
  283. direction2 = Vehicle.Direction.South;
  284. break;
  285. case Vehicle.Direction.South:
  286. direction2 = Vehicle.Direction.East;
  287. break;
  288. case Vehicle.Direction.West:
  289. direction2 = Vehicle.Direction.North;
  290. break;
  291. }
  292. if (this.sprites != null && (Vehicle.Direction)this.sprites.Length > direction2)
  293. {
  294. sprite = this.sprites[(int)direction2];
  295. if (sprite != null)
  296. {
  297. flag = true;
  298. }
  299. }
  300. }
  301. this.spriteRenderer.sprite = sprite;
  302. Vector3 localScale = this.originalScale;
  303. if (flag)
  304. {
  305. localScale.x = -localScale.x;
  306. }
  307. base.transform.localScale = localScale;
  308. }
  309. this.UpdateTransform();
  310. }
  311. protected virtual void UpdateTransform()
  312. {
  313. GridPoint point = location;
  314. if ((offsets != null) & (offsets.Length > (int)direction))
  315. {
  316. point.u += offsets[(int)direction].x;
  317. point.v += offsets[(int)direction].y;
  318. }
  319. Vector3 localPosition = grid.GetPositionForGridPoint(point);
  320. localPosition.z = 0f;
  321. base.transform.localPosition = localPosition;
  322. int num = (int)CurrentDirection / 2;
  323. spriteRenderer.sortingOrder = GridTile.GetSortingOrder(new GridIndex(location), new GridSize(1, 1)) + num;
  324. }
  325. private bool HasAccessableNeighbour(GridIndex index, Vehicle.Direction dir)
  326. {
  327. Road roadOfTypeAt = Road.GetRoadOfTypeAt(this.grid, index, this.roadType, this.canMoveOnBriges);
  328. return !(roadOfTypeAt == null) && roadOfTypeAt.IsAccessableFrom(dir, this.roadType);
  329. }
  330. private void RemoveBalloon(Balloon balloon)
  331. {
  332. if (this._gridTileIconManager != null)
  333. {
  334. this._gridTileIconManager.RemoveIcon(this._balloon.GridTileIcon);
  335. }
  336. this._balloon.CollectedEvent -= this.OnBalloonCollected;
  337. this._balloon.ExpiredEvent -= this.OnBalloonExpired;
  338. this._balloon = null;
  339. }
  340. private void OnBalloonCollected(Balloon balloon)
  341. {
  342. this.RemoveBalloon(balloon);
  343. }
  344. private void OnBalloonExpired(Balloon balloon)
  345. {
  346. this.RemoveBalloon(balloon);
  347. }
  348. // Note: this type is marked as 'beforefieldinit'.
  349. static Vehicle()
  350. {
  351. int[] array = new int[4];
  352. array[0] = -1;
  353. array[2] = 1;
  354. Vehicle.__dv = array;
  355. }
  356. private static int[] __du = new int[]
  357. {
  358. 0,
  359. 1,
  360. 0,
  361. -1
  362. };
  363. private static int[] __dv;
  364. public float minSpeed = 0.5f;
  365. public float maxSpeed = 0.5f;
  366. public Sprite[] sprites = new Sprite[4];
  367. public Vector2[] offsets = new Vector2[4];
  368. public Vector2[] frontPosition = new Vector2[4];
  369. public Vector2[] backPosition = new Vector2[4];
  370. public bool canMoveOnBriges;
  371. public Vehicle.VehicleType type = Vehicle.VehicleType.Civilian;
  372. [SerializeField]
  373. private Vector3 _iconOffset;
  374. public int requiredRoadType;
  375. [SelfReference]
  376. public SpriteRenderer spriteRenderer;
  377. [SelfReference(true)]
  378. public Animator animator;
  379. [SerializeField]
  380. private GridTileIconManager _gridTileIconManager;
  381. protected IsometricGrid grid;
  382. protected int roadType = -1;
  383. protected Vector3 originalScale;
  384. protected float speed;
  385. protected GridPoint location = new GridPoint(-1f, -1f);
  386. protected GridPoint targetLocation = new GridPoint(-1f, -1f);
  387. protected Vehicle.Direction direction;
  388. protected bool stuck = true;
  389. protected string cachedName = string.Empty;
  390. private Balloon _balloon;
  391. public enum VehicleType
  392. {
  393. None = -99,
  394. Civilian = -2,
  395. Taxi,
  396. Ambulance = 1,
  397. FireTruck,
  398. Police
  399. }
  400. public enum Direction
  401. {
  402. North,
  403. East,
  404. South,
  405. West
  406. }
  407. }
  408. }