using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x020000FC RID: 252
public class BakedMeshAnimator : MonoBehaviour
{
	// Token: 0x060015A7 RID: 5543 RVA: 0x0019B24C File Offset: 0x0019944C
	private void Awake()
	{
		this.tr = base.transform;
		this.currentAnimation = Mathf.Clamp(this.startAnimation, 0, this.animations.Length - 1);
		if (this.animationMeshRenderer == null)
		{
			this.animationMeshRenderer = base.GetComponent<MeshRenderer>();
		}
		if (this.animationMeshRenderer == null)
		{
			Debug.LogError("BakedMeshAnimator: " + ((this != null) ? this.ToString() : null) + " has no assigned MeshRenderer!");
		}
		this.meshFilter = this.animationMeshRenderer.GetComponent<MeshFilter>();
		this.meshCacheCount = this.animations[this.currentAnimation].meshes.Length;
		this.currentSpeed = this.animations[this.currentAnimation].playSpeed;
		this.CreateCrossFadeMesh();
		this.StartCrossfade();
		if (this.meshFilter.sharedMesh == null)
		{
			this.meshFilter.sharedMesh = this.animations[0].meshes[0];
		}
	}

	// Token: 0x060015A8 RID: 5544 RVA: 0x0019B346 File Offset: 0x00199546
	public void AnimateUpdate()
	{
		if (this.animations.Length == 0)
		{
			return;
		}
		this.Animate();
	}

	// Token: 0x060015A9 RID: 5545 RVA: 0x0019B358 File Offset: 0x00199558
	public void SetAnimation(int _animation, int _transitionFrame)
	{
		if (this.currentAnimation == _animation)
		{
			return;
		}
		this.transitionFrame = _transitionFrame;
		this.anim = _animation;
		this.SetAnimCommon();
	}

	// Token: 0x060015AA RID: 5546 RVA: 0x0019B378 File Offset: 0x00199578
	public void SetAnimation(int _animation)
	{
		if (this.currentAnimation == _animation)
		{
			return;
		}
		this.transitionFrame = this.animations[this.currentAnimation].transitionFrame;
		this.anim = _animation;
		this.SetAnimCommon();
	}

	// Token: 0x060015AB RID: 5547 RVA: 0x0019B3A9 File Offset: 0x001995A9
	private void SetAnimCommon()
	{
		base.enabled = true;
		this.transitioning = true;
		this.StartCrossfade();
	}

	// Token: 0x060015AC RID: 5548 RVA: 0x0019B3C0 File Offset: 0x001995C0
	public void Animate()
	{
		if (!this.animationMeshRenderer.isVisible)
		{
			return;
		}
		Vector3 cameraTargetPosition = Links.x.rtsCamera.cameraTargetPosition;
		cameraTargetPosition.y = this.tr.position.y;
		if ((this.tr.position - cameraTargetPosition).sqrMagnitude > 1600f)
		{
			return;
		}
		if (this.transitioning)
		{
			if (this.crossfade || (int)this.currentFrame == this.transitionFrame || this.failsafe > this.transitionFailsafe)
			{
				this.failsafe = 0f;
				this.transitioning = false;
				this.currentAnimation = this.anim;
				this.meshCacheCount = this.animations[this.currentAnimation].meshes.Length;
				this.currentSpeed = this.animations[this.currentAnimation].playSpeed;
				if (Time.time < 1f && this.animations[this.currentAnimation].randomStartFrame)
				{
					this.currentFrame = (float)Random.Range(this.meshCacheCount, 0);
				}
				else if (this.crossfade)
				{
					this.currentFrame = (float)this.animations[this.currentAnimation].crossfadeFrame;
				}
				else
				{
					this.currentFrame = (float)this.animations[this.currentAnimation].transitionFrame;
				}
			}
			else
			{
				this.failsafe += Time.deltaTime;
			}
		}
		if (!this.doCrossfade)
		{
			if (this.animations[this.currentAnimation].pingPong)
			{
				this.PingPongFrame();
			}
			else
			{
				this.NextFrame();
			}
			if (this.currentFrameInt != (int)this.currentFrame)
			{
				this.currentFrameInt = (int)this.currentFrame;
				if (this.crossfade && this.crossfadeNormalFix)
				{
					this.animations[this.currentAnimation].meshes[(int)this.currentFrame].GetVertices(this.norms);
					this.crossfadeMeshEnd.SetVertices(this.norms);
				}
				else
				{
					this.meshFilter.sharedMesh = this.animations[this.currentAnimation].meshes[(int)this.currentFrame];
				}
			}
		}
		this.UpdateCrossfade();
	}

	// Token: 0x060015AD RID: 5549 RVA: 0x0019B5EC File Offset: 0x001997EC
	public bool NextFrame()
	{
		this.currentFrame += this.currentSpeed * Time.deltaTime * this.playSpeedMultiplier;
		if (this.currentFrame > (float)(this.meshCacheCount + 1))
		{
			this.currentFrame = 0f;
			if (!this.animations[this.currentAnimation].loop)
			{
				base.enabled = false;
			}
			return true;
		}
		if (this.currentFrame >= (float)this.meshCacheCount)
		{
			this.currentFrame = (float)this.meshCacheCount - this.currentFrame;
			if (!this.animations[this.currentAnimation].loop)
			{
				base.enabled = false;
			}
			return true;
		}
		return false;
	}

	// Token: 0x060015AE RID: 5550 RVA: 0x0019B694 File Offset: 0x00199894
	public bool PingPongFrame()
	{
		if (this.pingPongToggle)
		{
			this.currentFrame += this.currentSpeed * Time.deltaTime * this.playSpeedMultiplier;
		}
		else
		{
			this.currentFrame -= this.currentSpeed * Time.deltaTime * this.playSpeedMultiplier;
		}
		if (this.currentFrame <= 0f)
		{
			this.currentFrame = 0f;
			this.pingPongToggle = true;
			return true;
		}
		if (this.currentFrame >= (float)this.meshCacheCount)
		{
			this.pingPongToggle = false;
			this.currentFrame = (float)(this.meshCacheCount - 1);
			return true;
		}
		return false;
	}

	// Token: 0x060015AF RID: 5551 RVA: 0x0019B734 File Offset: 0x00199934
	public void SetSpeedMultiplier(float speed)
	{
		this.playSpeedMultiplier = speed;
	}

	// Token: 0x060015B0 RID: 5552 RVA: 0x0019B740 File Offset: 0x00199940
	private void CrossfadeInit()
	{
		if (this.vertsDiff == null)
		{
			this.vertsDiff = new Vector3[this.vertsStart.Count];
		}
		this.crossfadeMestTo.GetVertices(this.meshVerts);
		for (int i = 0; i < this.vertsStart.Count; i++)
		{
			this.vertsDiff[i] = this.meshVerts[i] - this.vertsStart[i];
		}
	}

	// Token: 0x060015B1 RID: 5553 RVA: 0x0019B7BC File Offset: 0x001999BC
	private void CreateCrossFadeMesh()
	{
		if (!this.crossfade)
		{
			return;
		}
		this.crossfadeMeshStart = this.meshFilter.sharedMesh;
		this.crossfadeMeshStart.GetVertices(this.vertsStart);
		if (this.crossfadeMeshEnd == null)
		{
			this.crossfadeMeshEnd = new Mesh();
			this.crossfadeMeshEnd.MarkDynamic();
			this.crossfadeMeshEnd.SetVertices(this.vertsStart);
			this.crossfadeMeshEnd.triangles = this.crossfadeMeshStart.triangles;
			this.crossfadeMeshEnd.uv = this.crossfadeMeshStart.uv;
			this.crossfadeMeshStart.GetNormals(this.norms);
			this.crossfadeMeshEnd.SetNormals(this.norms);
		}
	}

	// Token: 0x060015B2 RID: 5554 RVA: 0x0019B878 File Offset: 0x00199A78
	private void StartCrossfade()
	{
		if (!this.crossfade)
		{
			return;
		}
		this.crossfadeMeshStart = this.meshFilter.sharedMesh;
		this.crossfadeMeshStart.GetVertices(this.vertsStart);
		this.doCrossfade = true;
		this.crossfadeWeight = 0f;
		this.crossfadeMeshEnd.SetVertices(this.vertsStart);
		this.meshFilter.mesh = this.crossfadeMeshEnd;
		this.crossfadeMeshStart.GetVertices(this.vertsStart);
		if (this.vertsCurrent == null)
		{
			this.vertsCurrent = new Vector3[this.vertsStart.Count];
		}
	}

	// Token: 0x060015B3 RID: 5555 RVA: 0x0019B914 File Offset: 0x00199B14
	private void UpdateCrossfade()
	{
		if (!this.crossfade)
		{
			return;
		}
		this.nextUpdate += Time.deltaTime;
		if (this.nextUpdate < this.crossfadeFrequency)
		{
			return;
		}
		this.nextUpdate = 0f;
		if (this.crossfadeWeight >= 1f)
		{
			this.doCrossfade = false;
			return;
		}
		this.crossfadeMestTo = this.animations[this.currentAnimation].meshes[this.animations[this.currentAnimation].crossfadeFrame];
		if (this.crossfadeWeight == 0f)
		{
			this.CrossfadeInit();
		}
		for (int i = 0; i < this.vertsCurrent.Length; i++)
		{
			this.vertsCurrent[i] = this.vertsStart[i];
		}
		if (this.vertsDiff.Length != this.vertsStart.Count)
		{
			return;
		}
		for (int j = 0; j < this.vertsCurrent.Length; j++)
		{
			this.vertsCurrent[j] += this.vertsDiff[j] * this.crossfadeWeight;
		}
		this.crossfadeMeshEnd.SetVertices(this.vertsCurrent);
		this.crossfadeWeight += this.crossfadeWeightAdd;
	}

	// Token: 0x040025DA RID: 9690
	public MeshRenderer animationMeshRenderer;

	// Token: 0x040025DB RID: 9691
	public BakedMeshAnimation[] animations;

	// Token: 0x040025DC RID: 9692
	public int startAnimation;

	// Token: 0x040025DD RID: 9693
	private int currentAnimation;

	// Token: 0x040025DE RID: 9694
	private MeshFilter meshFilter;

	// Token: 0x040025DF RID: 9695
	public float currentFrame;

	// Token: 0x040025E0 RID: 9696
	private int currentFrameInt;

	// Token: 0x040025E1 RID: 9697
	private float currentSpeed;

	// Token: 0x040025E2 RID: 9698
	private bool pingPongToggle;

	// Token: 0x040025E3 RID: 9699
	public float playSpeedMultiplier = 1f;

	// Token: 0x040025E4 RID: 9700
	private int meshCacheCount;

	// Token: 0x040025E5 RID: 9701
	public float transitionFailsafe = 0.4f;

	// Token: 0x040025E6 RID: 9702
	private float failsafe;

	// Token: 0x040025E7 RID: 9703
	private int transitionFrame;

	// Token: 0x040025E8 RID: 9704
	private int anim;

	// Token: 0x040025E9 RID: 9705
	private bool transitioning = true;

	// Token: 0x040025EA RID: 9706
	private Transform tr;

	// Token: 0x040025EB RID: 9707
	public bool crossfade;

	// Token: 0x040025EC RID: 9708
	public bool crossfadeNormalFix;

	// Token: 0x040025ED RID: 9709
	public float crossfadeFrequency = 0.05f;

	// Token: 0x040025EE RID: 9710
	public float crossfadeWeightAdd = 0.221f;

	// Token: 0x040025EF RID: 9711
	private bool doCrossfade;

	// Token: 0x040025F0 RID: 9712
	private float crossfadeWeight = 1f;

	// Token: 0x040025F1 RID: 9713
	private Mesh crossfadeMestTo;

	// Token: 0x040025F2 RID: 9714
	private Mesh crossfadeMeshStart;

	// Token: 0x040025F3 RID: 9715
	private Mesh crossfadeMeshEnd;

	// Token: 0x040025F4 RID: 9716
	private List<Vector3> vertsStart = new List<Vector3>();

	// Token: 0x040025F5 RID: 9717
	private List<Vector3> norms = new List<Vector3>();

	// Token: 0x040025F6 RID: 9718
	private Vector3[] vertsCurrent;

	// Token: 0x040025F7 RID: 9719
	private Vector3[] vertsDiff;

	// Token: 0x040025F8 RID: 9720
	private List<Vector3> meshVerts = new List<Vector3>();

	// Token: 0x040025F9 RID: 9721
	private float nextUpdate;
}
