25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

310 satır
7.8 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using SUISSEngine;
  4. using UnityEngine;
  5. public class UnlockBuildingVehicle : MonoBehaviour
  6. {
  7. public UnlockBuildingVehicle()
  8. {
  9. Vector3 vector = new Vector3(0f, 1f);
  10. this._flyingHeightUnit = vector.normalized;
  11. }
  12. public bool IsLeavingMap
  13. {
  14. get
  15. {
  16. return this._isLeavingMap;
  17. }
  18. set
  19. {
  20. this._isLeavingMap = value;
  21. }
  22. }
  23. public int TravelDirection
  24. {
  25. get
  26. {
  27. return this._travelDirection;
  28. }
  29. set
  30. {
  31. this._travelDirection = value;
  32. }
  33. }
  34. public float FlyingHeight
  35. {
  36. get
  37. {
  38. if (this._currentSegment >= 0 && this._currentSegment < this._flyingHeight.Length - 1)
  39. {
  40. float a = this._flyingHeight[this._currentSegment];
  41. float b = this._flyingHeight[this._currentSegment + 1];
  42. return Mathf.Lerp(a, b, this._currentT);
  43. }
  44. return 0f;
  45. }
  46. }
  47. public GridPoint Location
  48. {
  49. get
  50. {
  51. return this._location;
  52. }
  53. }
  54. public CIGUnlockBuilding UnlockBuilding
  55. {
  56. get
  57. {
  58. return this._unlockBuilding;
  59. }
  60. }
  61. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  62. public event UnlockBuildingVehicle.OnTargetReachedEventHandler OnTargetReachedEvent;
  63. public virtual void Awake()
  64. {
  65. this._spriteRenderer = base.gameObject.GetComponent<SpriteRenderer>();
  66. }
  67. public virtual void Update()
  68. {
  69. if (this._isTravelling && this.SegmentWithinPath(this._currentSegment, 1))
  70. {
  71. float num = this.speed * Time.deltaTime * this._pathSpeedMods[this._currentSegment];
  72. if (this.TravelDirection > 0)
  73. {
  74. while (this._currentT + num > 1f)
  75. {
  76. this._currentSegment++;
  77. if (!this.SegmentWithinPath(this._currentSegment, 1))
  78. {
  79. break;
  80. }
  81. num = this._currentT + num - 1f;
  82. num *= this._pathSpeedMods[this._currentSegment] / this._pathSpeedMods[this._currentSegment - 1];
  83. this._currentT = 0f;
  84. }
  85. }
  86. else if (this.TravelDirection < 0)
  87. {
  88. while (this._currentT - num < 0f)
  89. {
  90. this._currentSegment--;
  91. if (!this.SegmentWithinPath(this._currentSegment, 1))
  92. {
  93. break;
  94. }
  95. num = (this._currentT - num) * -1f;
  96. num *= this._pathSpeedMods[this._currentSegment] / this._pathSpeedMods[this._currentSegment + 1];
  97. this._currentT = 1f;
  98. }
  99. }
  100. if (this.SegmentWithinPath(this._currentSegment, 1))
  101. {
  102. GridPoint gridPoint = this._path[this._currentSegment];
  103. GridPoint left = this._path[this._currentSegment + 1];
  104. GridPoint gridPoint2 = left - gridPoint;
  105. if (this.TravelDirection > 0)
  106. {
  107. this._currentT += num;
  108. }
  109. else if (this.TravelDirection < 0)
  110. {
  111. this._currentT -= num;
  112. }
  113. this._location = gridPoint + new GridPoint(gridPoint2.u * this._currentT, gridPoint2.v * this._currentT);
  114. if (this._currentSegment >= 0 && this._currentSegment < this._flyingHeight.Length)
  115. {
  116. float a = this._flyingHeight[this._currentSegment];
  117. float b = this._flyingHeight[this._currentSegment + 1];
  118. this._flyingHeightOffset = this._flyingHeightUnit * Mathf.Lerp(a, b, this._currentT);
  119. }
  120. else
  121. {
  122. this._flyingHeightOffset = Vector3.zero;
  123. }
  124. Vector2 a2 = new Vector2(gridPoint2.u, gridPoint2.v);
  125. float num2 = 0f;
  126. int num3 = 0;
  127. int num4 = 0;
  128. while (num4 < this.spriteDirections.Length && num4 < this.sprites.Length)
  129. {
  130. float num5 = Vector2.Dot(a2 * (float)this.TravelDirection, this.spriteDirections[num4]);
  131. if (num5 > num2)
  132. {
  133. num2 = num5;
  134. num3 = num4;
  135. }
  136. num4++;
  137. }
  138. this._spriteRenderer.sprite = this.sprites[num3 * this.spriteSheetSize + (int)this.spriteAnimationIndex];
  139. }
  140. else
  141. {
  142. this._currentSegment = ((this.TravelDirection <= 0) ? 0 : (this._path.Length - 1));
  143. this._location = this._path[this._currentSegment];
  144. this._currentT = 0f;
  145. if (this._currentSegment >= 0 && this._currentSegment < this._flyingHeight.Length)
  146. {
  147. this._flyingHeightOffset = this._flyingHeightUnit * this._flyingHeight[this._currentSegment];
  148. }
  149. else
  150. {
  151. this._flyingHeightOffset = Vector3.zero;
  152. }
  153. this.FireOnTargetReachedEvent();
  154. }
  155. }
  156. this.UpdateTransform();
  157. }
  158. public virtual void SpawnAt(IsometricGrid grid, CIGUnlockBuilding.SVehicleSpawnInfo spawnInfo)
  159. {
  160. base.transform.parent = grid.transform;
  161. this._grid = grid;
  162. this._unlockBuilding = spawnInfo.unlockBuilding;
  163. this._path = spawnInfo.path;
  164. this._isTravelling = true;
  165. this._pathSpeedMods = new float[this._path.Length];
  166. this._flyingHeight = spawnInfo.flyingHeight;
  167. if (spawnInfo.path.Length > 0)
  168. {
  169. this._location = spawnInfo.path[0];
  170. if (spawnInfo.path.Length > 1)
  171. {
  172. for (int i = 0; i < this._path.Length - 1; i++)
  173. {
  174. Vector3 vector = grid.GetPositionForGridPoint(this._path[i]);
  175. Vector3 a = grid.GetPositionForGridPoint(this._path[i + 1]);
  176. if (i < this._flyingHeight.Length)
  177. {
  178. vector += this._flyingHeightUnit * this._flyingHeight[i];
  179. }
  180. if (i + 1 < this._flyingHeight.Length)
  181. {
  182. a += this._flyingHeightUnit * this._flyingHeight[i + 1];
  183. }
  184. this._pathSpeedMods[i] = 1f / (a - vector).magnitude;
  185. }
  186. this._pathSpeedMods[this._pathSpeedMods.Length - 1] = this._pathSpeedMods[this._pathSpeedMods.Length - 2];
  187. }
  188. }
  189. this.UpdateTransform();
  190. this.SetVisibility(true);
  191. }
  192. public virtual void StartTravelling()
  193. {
  194. this._isTravelling = true;
  195. }
  196. public virtual void StopTravelling()
  197. {
  198. this._isTravelling = false;
  199. }
  200. public void SetVisibility(bool visible)
  201. {
  202. this._spriteRenderer.enabled = visible;
  203. }
  204. public void ReverseTravelDirection()
  205. {
  206. this._currentSegment = ((this._currentSegment != this._path.Length - 1) ? ((this._currentSegment != 0) ? this._currentSegment : 0) : (this._path.Length - 2));
  207. this.TravelDirection *= -1;
  208. if (this.TravelDirection < 0)
  209. {
  210. this._currentT = 1f - this._currentT;
  211. }
  212. }
  213. public Vector3 GetPositionForGridPoint(GridPoint location)
  214. {
  215. return this._grid.GetPositionForGridPoint(location);
  216. }
  217. protected virtual void UpdateTransform()
  218. {
  219. GridPoint location = this._location;
  220. Vector3 positionForGridPoint = this.GetPositionForGridPoint(location);
  221. positionForGridPoint.z = 0f;
  222. base.transform.localPosition = positionForGridPoint + this._flyingHeightOffset;
  223. this._spriteRenderer.sortingOrder = GridTile.GetSortingOrder(new GridIndex(location), new GridSize(this.sizeInTilesU, this.sizeInTilesV));
  224. }
  225. private void FireOnTargetReachedEvent()
  226. {
  227. if (this.OnTargetReachedEvent != null)
  228. {
  229. this.OnTargetReachedEvent(this);
  230. }
  231. }
  232. private bool SegmentWithinPath(int startWp, int travelDirection = 1)
  233. {
  234. return startWp >= 0 && startWp < this._path.Length && startWp + travelDirection >= 0 && startWp + travelDirection < this._path.Length;
  235. }
  236. public float spriteAnimationIndex;
  237. public int spriteSheetSize = 1;
  238. public int sizeInTilesU = 1;
  239. public int sizeInTilesV = 1;
  240. public float speed;
  241. public float dockedTime;
  242. public float awayTime;
  243. public Sprite[] sprites;
  244. public Vector2[] spriteDirections = new Vector2[]
  245. {
  246. new Vector2(0f, 1f),
  247. new Vector2(1f, 0f),
  248. new Vector2(0f, -1f),
  249. new Vector2(-1f, 0f)
  250. };
  251. protected bool _isTravelling;
  252. protected bool _isLeavingMap;
  253. protected int _travelDirection;
  254. protected int _currentSegment;
  255. protected float _currentT;
  256. protected CIGUnlockBuilding _unlockBuilding;
  257. protected GridPoint _location;
  258. protected Vector3 _flyingHeightOffset;
  259. protected IsometricGrid _grid;
  260. protected SpriteRenderer _spriteRenderer;
  261. protected float[] _pathSpeedMods;
  262. protected float[] _flyingHeight;
  263. protected GridPoint[] _path;
  264. private Vector3 _flyingHeightUnit;
  265. public delegate void OnTargetReachedEventHandler(UnlockBuildingVehicle vehicle);
  266. }