GlobalWindGenerator.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using CIS;
  3. using UnityEngine;
  4. public class GlobalWindGenerator : SingletonMonoBehaviourClass<GlobalWindGenerator>, IBlowee
  5. {
  6. public float maxPower
  7. {
  8. get
  9. {
  10. return this.initPower;
  11. }
  12. }
  13. public Vector2 GetDir(IBlowable blowable, Vector2 pos)
  14. {
  15. return this._dir;
  16. }
  17. public float GetPower(IBlowable blowable, Vector2 pos)
  18. {
  19. return this.power;
  20. }
  21. public float GetMaxAccelSqr()
  22. {
  23. return float.MaxValue;
  24. }
  25. public void ParserIntegrator(Integrator integrator)
  26. {
  27. }
  28. public Vector2 dir
  29. {
  30. get
  31. {
  32. return this._dir;
  33. }
  34. }
  35. public GameObject GetGO()
  36. {
  37. return base.gameObject;
  38. }
  39. private void Awake()
  40. {
  41. this.initPower = this.power;
  42. this.initDir = this.windDir.normalized;
  43. this.alterInitDir = this.alterWindDir.normalized;
  44. this.initWindAccel = this.windAccel;
  45. this.power = 0f;
  46. this.generateP = this.wavePerSecond / 60f;
  47. this.state = GlobalWindGenerator.State.Generating;
  48. }
  49. private void Update()
  50. {
  51. if (this.state == GlobalWindGenerator.State.Generating)
  52. {
  53. float num = UnityEngine.Random.Range(0f, 1f);
  54. if (num < this.generateP)
  55. {
  56. this.power = 0.01f;
  57. this.state = GlobalWindGenerator.State.Accel;
  58. this.windAccel = UnityEngine.Random.Range(0.5f * this.initWindAccel, 2f * this.initWindAccel);
  59. Vector2 vector = new Vector2(UnityEngine.Random.Range(this.initDir.x, this.alterInitDir.x), UnityEngine.Random.Range(this.alterInitDir.y, this.initDir.y));
  60. this.alterWindDir = vector.normalized;
  61. }
  62. }
  63. else if (this.state == GlobalWindGenerator.State.Accel)
  64. {
  65. if (this.power < this.initPower)
  66. {
  67. this.power += this.windAccel * SingletonMonoBehaviourClass<TimeMgr>.instance.deltaTime;
  68. }
  69. else
  70. {
  71. this.state = GlobalWindGenerator.State.Waving;
  72. this.waveTime = UnityEngine.Random.Range(6f, 12f);
  73. }
  74. }
  75. else if (this.state == GlobalWindGenerator.State.Waving)
  76. {
  77. this.waveTimer += SingletonMonoBehaviourClass<TimeMgr>.instance.deltaTime * this.waveSpeed;
  78. if (this.waveTimer < this.waveTime * this.waveSpeed)
  79. {
  80. float num2 = (1f + Mathf.Cos(this.waveTimer)) / 2f;
  81. this.power = this.initPower / 6f + 5f * this.initPower / 6f * num2;
  82. this._dir = Vector2.Lerp(this.initDir, this.alterWindDir, num2).normalized;
  83. }
  84. else
  85. {
  86. this.waveTimer = 0f;
  87. this.state = GlobalWindGenerator.State.Fall;
  88. this.fallTime = UnityEngine.Random.Range(0.5f, 1f);
  89. this.initFallPower = this.power;
  90. this.initFallDir = this._dir;
  91. }
  92. }
  93. else if (this.state == GlobalWindGenerator.State.Fall)
  94. {
  95. this.fallTimer += SingletonMonoBehaviourClass<TimeMgr>.instance.deltaTime;
  96. if (this.fallTimer < this.fallTime)
  97. {
  98. float t = this.fallTimer / this.fallTime;
  99. this.power = Mathf.Lerp(this.initFallPower, 0f, t);
  100. this._dir = Vector2.Lerp(this.initFallDir, -Vector2.up, t).normalized;
  101. }
  102. else
  103. {
  104. this.fallTimer = 0f;
  105. this.power = 0f;
  106. this.state = GlobalWindGenerator.State.Generating;
  107. }
  108. }
  109. }
  110. private void OnGUI()
  111. {
  112. if (!this.debug)
  113. {
  114. return;
  115. }
  116. GUI.BeginGroup(new Rect(10f, 100f, 200f, 200f));
  117. GUILayout.Label("Win Dir: " + this._dir, new GUILayoutOption[0]);
  118. GUILayout.Label("Win power: " + this.power, new GUILayoutOption[0]);
  119. GUILayout.Label("Win state: " + this.state, new GUILayoutOption[0]);
  120. if (this._dir.x != 0f || this._dir.y != 0f)
  121. {
  122. Vector2 vector = new Vector2(50f, 100f);
  123. Vector2 dir = this._dir;
  124. dir.y *= -1f;
  125. GuiHelper.DrawLine(vector, vector + dir * 80f, Color.green, 1);
  126. dir = this.initDir;
  127. dir.y *= -1f;
  128. GuiHelper.DrawLine(vector, vector + dir * 80f, Color.red, 1);
  129. dir = this.alterWindDir;
  130. dir.y *= -1f;
  131. GuiHelper.DrawLine(vector, vector + dir * 80f, Color.red, 1);
  132. }
  133. GUI.EndGroup();
  134. }
  135. public Vector2 windDir;
  136. public Vector2 alterWindDir;
  137. public float power;
  138. public float wavePerSecond;
  139. public float windAccel;
  140. public float waveSpeed;
  141. private float generateP;
  142. private float initWindAccel;
  143. private float initPower;
  144. private Vector2 initDir;
  145. private Vector2 alterInitDir;
  146. private Vector2 _dir;
  147. private float waveTime;
  148. private float waveTimer;
  149. private float fallTimer;
  150. private float initFallPower;
  151. private Vector2 initFallDir;
  152. private float fallTime;
  153. private GlobalWindGenerator.State state;
  154. private float windGenerateCondition;
  155. public bool debug;
  156. public enum State
  157. {
  158. Generating,
  159. Accel,
  160. Waving,
  161. Fall
  162. }
  163. }