using System; using SUISSEngine; using UnityEngine; public class CIGAnimatedCreature : MonoBehaviour { private void Start() { if (this.randomizeSpeed) { base.InvokeRepeating("RandomizeSpeed", 0.5f, 1f); } } private void FitDirection() { CIGAnimatedCreature.DirectionDefinition directionDefinition = null; float num = -2f; foreach (CIGAnimatedCreature.DirectionDefinition directionDefinition2 in this.directions) { float num2 = Vector2.Dot(directionDefinition2.vectorDir.normalized, this._currentVector); if (num2 > num) { num = num2; directionDefinition = directionDefinition2; } } if (directionDefinition == null) { UnityEngine.Debug.LogError(string.Format("Could not find best fit for direction {0}.", this._currentVector)); } this._currentAnimation = directionDefinition; } private void RandomizeSpeed() { this.animator.speed = ((!this._moving) ? UnityEngine.Random.Range(0.05f, 0.15f) : UnityEngine.Random.Range(0.5f, 0.65f)); } public void SetDirection(Vector2 direction, bool moving) { this._currentVector = direction.normalized; this._moving = moving; this.FitDirection(); if (this._currentAnimation == null) { UnityEngine.Debug.LogError("_currentAnimation is null after SelectAnimation()"); return; } this.animator.SetInteger("Direction", (int)this._currentAnimation.directionEnum); this.animator.SetBool("Moving", this._moving); if (this.disappearWhenStill) { this.spriteRenderer.enabled = this._moving; } } public void SetMoving(bool moving) { this._moving = moving; this.FitDirection(); this.animator.SetBool("Moving", this._moving); if (this.disappearWhenStill) { this.spriteRenderer.enabled = this._moving; } } public CIGAnimatedCreature.DirectionDefinition[] directions; public bool randomizeSpeed = true; public bool disappearWhenStill; [SerializeField] private SpriteRenderer spriteRenderer; [SerializeField] private Animator animator; private Vector2 _currentVector; private bool _moving = true; private CIGAnimatedCreature.DirectionDefinition _currentAnimation; [Serializable] public class DirectionDefinition { public Vector2 vectorDir; public Vehicle.Direction directionEnum; } }