Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

95 řádky
2.3 KiB

  1. using System;
  2. using SUISSEngine;
  3. using UnityEngine;
  4. public class CIGAnimatedCreature : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. if (this.randomizeSpeed)
  9. {
  10. base.InvokeRepeating("RandomizeSpeed", 0.5f, 1f);
  11. }
  12. }
  13. private void FitDirection()
  14. {
  15. CIGAnimatedCreature.DirectionDefinition directionDefinition = null;
  16. float num = -2f;
  17. foreach (CIGAnimatedCreature.DirectionDefinition directionDefinition2 in this.directions)
  18. {
  19. float num2 = Vector2.Dot(directionDefinition2.vectorDir.normalized, this._currentVector);
  20. if (num2 > num)
  21. {
  22. num = num2;
  23. directionDefinition = directionDefinition2;
  24. }
  25. }
  26. if (directionDefinition == null)
  27. {
  28. UnityEngine.Debug.LogError(string.Format("Could not find best fit for direction {0}.", this._currentVector));
  29. }
  30. this._currentAnimation = directionDefinition;
  31. }
  32. private void RandomizeSpeed()
  33. {
  34. this.animator.speed = ((!this._moving) ? UnityEngine.Random.Range(0.05f, 0.15f) : UnityEngine.Random.Range(0.5f, 0.65f));
  35. }
  36. public void SetDirection(Vector2 direction, bool moving)
  37. {
  38. this._currentVector = direction.normalized;
  39. this._moving = moving;
  40. this.FitDirection();
  41. if (this._currentAnimation == null)
  42. {
  43. UnityEngine.Debug.LogError("_currentAnimation is null after SelectAnimation()");
  44. return;
  45. }
  46. this.animator.SetInteger("Direction", (int)this._currentAnimation.directionEnum);
  47. this.animator.SetBool("Moving", this._moving);
  48. if (this.disappearWhenStill)
  49. {
  50. this.spriteRenderer.enabled = this._moving;
  51. }
  52. }
  53. public void SetMoving(bool moving)
  54. {
  55. this._moving = moving;
  56. this.FitDirection();
  57. this.animator.SetBool("Moving", this._moving);
  58. if (this.disappearWhenStill)
  59. {
  60. this.spriteRenderer.enabled = this._moving;
  61. }
  62. }
  63. public CIGAnimatedCreature.DirectionDefinition[] directions;
  64. public bool randomizeSpeed = true;
  65. public bool disappearWhenStill;
  66. [SerializeField]
  67. private SpriteRenderer spriteRenderer;
  68. [SerializeField]
  69. private Animator animator;
  70. private Vector2 _currentVector;
  71. private bool _moving = true;
  72. private CIGAnimatedCreature.DirectionDefinition _currentAnimation;
  73. [Serializable]
  74. public class DirectionDefinition
  75. {
  76. public Vector2 vectorDir;
  77. public Vehicle.Direction directionEnum;
  78. }
  79. }