SinMove.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using CIS;
  3. using UnityEngine;
  4. public class SinMove : MonoBehaviour, ILevelItem
  5. {
  6. private void Awake()
  7. {
  8. this.myTrans = base.transform;
  9. this.initPos = this.myTrans.position;
  10. this.originalPos = this.initPos;
  11. if (this.autoRotate)
  12. {
  13. this.radius = Vector2.Distance(this.initPos, this.rotateCenterTrans.position);
  14. }
  15. this.sinDirectionTimer = 0f;
  16. this.sinDirectionTime = UnityEngine.Random.Range(2f, 3f);
  17. }
  18. public void SetInitPos(Vector2 pos)
  19. {
  20. this.initPos = pos;
  21. if (this.autoRotate)
  22. {
  23. this.radius = Vector2.Distance(this.initPos, this.rotateCenterTrans.position);
  24. Vector2 vector = this.initPos - new Vector2(this.rotateCenterTrans.position.x, this.rotateCenterTrans.position.y);
  25. this.autoRotateTimer = Mathf.Atan2(vector.y, vector.x);
  26. }
  27. }
  28. public Vector2 GetCurrentPos()
  29. {
  30. return new Vector2(this.initPos.x + this.bias * Mathf.Cos(this.timer), this.initPos.y + this.bias * Mathf.Sin(this.timer + 0.3926991f));
  31. }
  32. private void Start()
  33. {
  34. if (this.randomStart)
  35. {
  36. this.waitTime = UnityEngine.Random.Range(0.3f, 4f);
  37. }
  38. else
  39. {
  40. this.waitTime = 0f;
  41. }
  42. this.timer = this.waitTime;
  43. if (this.autoRotate)
  44. {
  45. Vector2 vector = this.myTrans.position - this.rotateCenterTrans.position;
  46. this.autoRotateTimer = Mathf.Atan2(vector.y, vector.x);
  47. }
  48. }
  49. public void OnReset()
  50. {
  51. this.direction = 1;
  52. this.myTrans.position = this.originalPos;
  53. this.initPos = this.originalPos;
  54. this.sinDirectionTimer = 0f;
  55. this.sinDirection = 1;
  56. }
  57. public void OnPause(bool isPause)
  58. {
  59. }
  60. public Vector2 GetRotateLinearVel()
  61. {
  62. if (this.autoRotate)
  63. {
  64. Vector2 v = this.rotateCenterTrans.position - this.myTrans.position;
  65. return Quaternion.Euler(0f, 0f, 90f) * v * this.rotateSpeed * (float)(-(float)this.direction);
  66. }
  67. return Vector2.zero;
  68. }
  69. private void Update()
  70. {
  71. if (SingletonMonoBehaviourClass<GameLogicMgr>.instance.IsPause())
  72. {
  73. return;
  74. }
  75. this.timer += SingletonMonoBehaviourClass<TimeMgr>.instance.deltaTime;
  76. this.sinDirectionTimer += SingletonMonoBehaviourClass<TimeMgr>.instance.deltaTime;
  77. if (this.sinDirectionTimer > this.sinDirectionTime)
  78. {
  79. this.sinDirection *= -1;
  80. this.sinDirectionTimer = 0f;
  81. this.sinDirectionTime = UnityEngine.Random.Range(2f, 3f);
  82. }
  83. if (this.autoRotate)
  84. {
  85. this.autoRotateTimer += this.rotateSpeed * SingletonMonoBehaviourClass<TimeMgr>.instance.deltaTime * (float)this.direction;
  86. this.initPos.x = this.rotateCenterTrans.position.x + this.radius * Mathf.Cos(this.autoRotateTimer);
  87. this.initPos.y = this.rotateCenterTrans.position.y + this.radius * Mathf.Sin(this.autoRotateTimer);
  88. }
  89. this.myTrans.position = new Vector2(this.initPos.x + this.bias * Mathf.Cos(this.timer), this.initPos.y + this.bias * Mathf.Sin(this.timer + 0.3926991f));
  90. }
  91. public void OnEntertBlock(ILevelBlock block)
  92. {
  93. }
  94. public void OnLeaveBlock(ILevelBlock block)
  95. {
  96. }
  97. public void OnPickingTime(float time)
  98. {
  99. }
  100. public bool randomStart;
  101. public float bias;
  102. public bool autoRotate;
  103. public Transform rotateCenterTrans;
  104. public float rotateSpeed;
  105. public int direction = 1;
  106. private Transform myTrans;
  107. private Vector2 initPos;
  108. private Vector2 originalPos;
  109. private float waitTime;
  110. private float timer;
  111. private float autoRotateTimer;
  112. private float radius;
  113. private int sinDirection = 1;
  114. private float sinDirectionTimer;
  115. private float sinDirectionTime;
  116. }