using System; using System.Diagnostics; using SUISSEngine; using UnityEngine; public class UnlockBuildingVehicle : MonoBehaviour { public UnlockBuildingVehicle() { Vector3 vector = new Vector3(0f, 1f); this._flyingHeightUnit = vector.normalized; } public bool IsLeavingMap { get { return this._isLeavingMap; } set { this._isLeavingMap = value; } } public int TravelDirection { get { return this._travelDirection; } set { this._travelDirection = value; } } public float FlyingHeight { get { if (this._currentSegment >= 0 && this._currentSegment < this._flyingHeight.Length - 1) { float a = this._flyingHeight[this._currentSegment]; float b = this._flyingHeight[this._currentSegment + 1]; return Mathf.Lerp(a, b, this._currentT); } return 0f; } } public GridPoint Location { get { return this._location; } } public CIGUnlockBuilding UnlockBuilding { get { return this._unlockBuilding; } } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event UnlockBuildingVehicle.OnTargetReachedEventHandler OnTargetReachedEvent; public virtual void Awake() { this._spriteRenderer = base.gameObject.GetComponent(); } public virtual void Update() { if (this._isTravelling && this.SegmentWithinPath(this._currentSegment, 1)) { float num = this.speed * Time.deltaTime * this._pathSpeedMods[this._currentSegment]; if (this.TravelDirection > 0) { while (this._currentT + num > 1f) { this._currentSegment++; if (!this.SegmentWithinPath(this._currentSegment, 1)) { break; } num = this._currentT + num - 1f; num *= this._pathSpeedMods[this._currentSegment] / this._pathSpeedMods[this._currentSegment - 1]; this._currentT = 0f; } } else if (this.TravelDirection < 0) { while (this._currentT - num < 0f) { this._currentSegment--; if (!this.SegmentWithinPath(this._currentSegment, 1)) { break; } num = (this._currentT - num) * -1f; num *= this._pathSpeedMods[this._currentSegment] / this._pathSpeedMods[this._currentSegment + 1]; this._currentT = 1f; } } if (this.SegmentWithinPath(this._currentSegment, 1)) { GridPoint gridPoint = this._path[this._currentSegment]; GridPoint left = this._path[this._currentSegment + 1]; GridPoint gridPoint2 = left - gridPoint; if (this.TravelDirection > 0) { this._currentT += num; } else if (this.TravelDirection < 0) { this._currentT -= num; } this._location = gridPoint + new GridPoint(gridPoint2.u * this._currentT, gridPoint2.v * this._currentT); if (this._currentSegment >= 0 && this._currentSegment < this._flyingHeight.Length) { float a = this._flyingHeight[this._currentSegment]; float b = this._flyingHeight[this._currentSegment + 1]; this._flyingHeightOffset = this._flyingHeightUnit * Mathf.Lerp(a, b, this._currentT); } else { this._flyingHeightOffset = Vector3.zero; } Vector2 a2 = new Vector2(gridPoint2.u, gridPoint2.v); float num2 = 0f; int num3 = 0; int num4 = 0; while (num4 < this.spriteDirections.Length && num4 < this.sprites.Length) { float num5 = Vector2.Dot(a2 * (float)this.TravelDirection, this.spriteDirections[num4]); if (num5 > num2) { num2 = num5; num3 = num4; } num4++; } this._spriteRenderer.sprite = this.sprites[num3 * this.spriteSheetSize + (int)this.spriteAnimationIndex]; } else { this._currentSegment = ((this.TravelDirection <= 0) ? 0 : (this._path.Length - 1)); this._location = this._path[this._currentSegment]; this._currentT = 0f; if (this._currentSegment >= 0 && this._currentSegment < this._flyingHeight.Length) { this._flyingHeightOffset = this._flyingHeightUnit * this._flyingHeight[this._currentSegment]; } else { this._flyingHeightOffset = Vector3.zero; } this.FireOnTargetReachedEvent(); } } this.UpdateTransform(); } public virtual void SpawnAt(IsometricGrid grid, CIGUnlockBuilding.SVehicleSpawnInfo spawnInfo) { base.transform.parent = grid.transform; this._grid = grid; this._unlockBuilding = spawnInfo.unlockBuilding; this._path = spawnInfo.path; this._isTravelling = true; this._pathSpeedMods = new float[this._path.Length]; this._flyingHeight = spawnInfo.flyingHeight; if (spawnInfo.path.Length > 0) { this._location = spawnInfo.path[0]; if (spawnInfo.path.Length > 1) { for (int i = 0; i < this._path.Length - 1; i++) { Vector3 vector = grid.GetPositionForGridPoint(this._path[i]); Vector3 a = grid.GetPositionForGridPoint(this._path[i + 1]); if (i < this._flyingHeight.Length) { vector += this._flyingHeightUnit * this._flyingHeight[i]; } if (i + 1 < this._flyingHeight.Length) { a += this._flyingHeightUnit * this._flyingHeight[i + 1]; } this._pathSpeedMods[i] = 1f / (a - vector).magnitude; } this._pathSpeedMods[this._pathSpeedMods.Length - 1] = this._pathSpeedMods[this._pathSpeedMods.Length - 2]; } } this.UpdateTransform(); this.SetVisibility(true); } public virtual void StartTravelling() { this._isTravelling = true; } public virtual void StopTravelling() { this._isTravelling = false; } public void SetVisibility(bool visible) { this._spriteRenderer.enabled = visible; } public void ReverseTravelDirection() { this._currentSegment = ((this._currentSegment != this._path.Length - 1) ? ((this._currentSegment != 0) ? this._currentSegment : 0) : (this._path.Length - 2)); this.TravelDirection *= -1; if (this.TravelDirection < 0) { this._currentT = 1f - this._currentT; } } public Vector3 GetPositionForGridPoint(GridPoint location) { return this._grid.GetPositionForGridPoint(location); } protected virtual void UpdateTransform() { GridPoint location = this._location; Vector3 positionForGridPoint = this.GetPositionForGridPoint(location); positionForGridPoint.z = 0f; base.transform.localPosition = positionForGridPoint + this._flyingHeightOffset; this._spriteRenderer.sortingOrder = GridTile.GetSortingOrder(new GridIndex(location), new GridSize(this.sizeInTilesU, this.sizeInTilesV)); } private void FireOnTargetReachedEvent() { if (this.OnTargetReachedEvent != null) { this.OnTargetReachedEvent(this); } } private bool SegmentWithinPath(int startWp, int travelDirection = 1) { return startWp >= 0 && startWp < this._path.Length && startWp + travelDirection >= 0 && startWp + travelDirection < this._path.Length; } public float spriteAnimationIndex; public int spriteSheetSize = 1; public int sizeInTilesU = 1; public int sizeInTilesV = 1; public float speed; public float dockedTime; public float awayTime; public Sprite[] sprites; public Vector2[] spriteDirections = new Vector2[] { new Vector2(0f, 1f), new Vector2(1f, 0f), new Vector2(0f, -1f), new Vector2(-1f, 0f) }; protected bool _isTravelling; protected bool _isLeavingMap; protected int _travelDirection; protected int _currentSegment; protected float _currentT; protected CIGUnlockBuilding _unlockBuilding; protected GridPoint _location; protected Vector3 _flyingHeightOffset; protected IsometricGrid _grid; protected SpriteRenderer _spriteRenderer; protected float[] _pathSpeedMods; protected float[] _flyingHeight; protected GridPoint[] _path; private Vector3 _flyingHeightUnit; public delegate void OnTargetReachedEventHandler(UnlockBuildingVehicle vehicle); }