RocketController.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using UnityEngine;
  3. public class RocketController : BaseBehaviour
  4. {
  5. private void GetEffectSettingsComponent(Transform tr)
  6. {
  7. Transform parent = tr.parent;
  8. if (parent != null)
  9. {
  10. this.effectSettings = parent.GetComponentInChildren<EffectSettings>();
  11. if (this.effectSettings == null)
  12. {
  13. this.GetEffectSettingsComponent(parent.transform);
  14. }
  15. }
  16. }
  17. private void Start()
  18. {
  19. this.t = base.transform;
  20. this.GetEffectSettingsComponent(this.t);
  21. if (this.effectSettings == null)
  22. {
  23. UnityEngine.Debug.Log("Prefab root or children have not script \"PrefabSettings\"");
  24. }
  25. if (!this.IsRootMove)
  26. {
  27. this.startParentPosition = base.transform.parent.position;
  28. }
  29. if (this.GoLight != null)
  30. {
  31. this.tLight = this.GoLight.transform;
  32. }
  33. this.InitializeDefault();
  34. this.isInitializedOnStart = true;
  35. }
  36. private void OnEnable()
  37. {
  38. if (this.isInitializedOnStart)
  39. {
  40. this.InitializeDefault();
  41. }
  42. }
  43. private void OnDisable()
  44. {
  45. if (this.ResetParentPositionOnDisable && this.isInitializedOnStart && !this.IsRootMove)
  46. {
  47. base.transform.parent.position = this.startParentPosition;
  48. }
  49. }
  50. private void InitializeDefault()
  51. {
  52. this.hit = default(RaycastHit);
  53. this.onCollision = false;
  54. this.smootRandomPos = default(Vector3);
  55. this.oldSmootRandomPos = default(Vector3);
  56. this.deltaSpeed = 0f;
  57. this.startTime = 0f;
  58. this.randomSpeed = 0f;
  59. this.randomRadiusX = 0f;
  60. this.randomRadiusY = 0f;
  61. this.randomDirection1 = 0;
  62. this.randomDirection2 = 0;
  63. this.randomDirection3 = 0;
  64. this.frameDroped = false;
  65. this.tRoot = ((!this.IsRootMove) ? base.transform.parent : this.effectSettings.transform);
  66. this.startPosition = this.tRoot.position;
  67. if (this.effectSettings.Target != null)
  68. {
  69. this.tTarget = this.effectSettings.Target.transform;
  70. }
  71. else if (!this.effectSettings.UseMoveVector)
  72. {
  73. UnityEngine.Debug.Log("You must setup the the target or the motion vector");
  74. }
  75. if ((double)this.effectSettings.EffectRadius > 0.001)
  76. {
  77. Vector2 vector = UnityEngine.Random.insideUnitCircle * this.effectSettings.EffectRadius;
  78. this.randomTargetOffsetXZVector = new Vector3(vector.x, 0f, vector.y);
  79. }
  80. else
  81. {
  82. this.randomTargetOffsetXZVector = Vector3.zero;
  83. }
  84. if (!this.effectSettings.UseMoveVector)
  85. {
  86. this.forwardDirection = this.tRoot.position + (this.tTarget.position + this.randomTargetOffsetXZVector - this.tRoot.position).normalized * this.effectSettings.MoveDistance;
  87. this.GetTargetHit();
  88. }
  89. else
  90. {
  91. this.forwardDirection = this.tRoot.position + this.effectSettings.MoveVector * this.effectSettings.MoveDistance;
  92. }
  93. if (this.IsLookAt)
  94. {
  95. if (!this.effectSettings.UseMoveVector)
  96. {
  97. this.tRoot.LookAt(this.tTarget);
  98. }
  99. else
  100. {
  101. this.tRoot.LookAt(this.forwardDirection);
  102. }
  103. }
  104. this.InitRandomVariables();
  105. }
  106. private void Update()
  107. {
  108. if (!this.frameDroped)
  109. {
  110. this.frameDroped = true;
  111. return;
  112. }
  113. if (((!this.effectSettings.UseMoveVector && this.tTarget == null) || this.onCollision) && this.frameDroped)
  114. {
  115. return;
  116. }
  117. Vector3 vector;
  118. if (!this.effectSettings.UseMoveVector)
  119. {
  120. vector = ((!this.effectSettings.IsHomingMove) ? this.forwardDirection : this.tTarget.position);
  121. }
  122. else
  123. {
  124. vector = this.forwardDirection;
  125. }
  126. float num = Vector3.Distance(this.tRoot.position, vector);
  127. float num2 = this.effectSettings.MoveSpeed * Time.deltaTime;
  128. if (num2 > num)
  129. {
  130. num2 = num;
  131. }
  132. if (num <= this.effectSettings.ColliderRadius)
  133. {
  134. this.hit = default(RaycastHit);
  135. this.CollisionEnter();
  136. }
  137. Vector3 normalized = (vector - this.tRoot.position).normalized;
  138. RaycastHit raycastHit;
  139. if (Physics.Raycast(this.tRoot.position, normalized, out raycastHit, num2 + this.effectSettings.ColliderRadius, this.effectSettings.LayerMask))
  140. {
  141. this.hit = raycastHit;
  142. vector = raycastHit.point - normalized * this.effectSettings.ColliderRadius;
  143. this.CollisionEnter();
  144. }
  145. if (this.IsCenterLightPosition && this.GoLight != null)
  146. {
  147. this.tLight.position = (this.startPosition + this.tRoot.position) / 2f;
  148. }
  149. Vector3 b = default(Vector3);
  150. if (this.RandomMoveCoordinates != RandomMoveCoordinates.None)
  151. {
  152. this.UpdateSmootRandomhPos();
  153. b = this.smootRandomPos - this.oldSmootRandomPos;
  154. }
  155. float num3 = 1f;
  156. if (this.Acceleration.length > 0)
  157. {
  158. float time = (Time.time - this.startTime) / this.AcceleraionTime;
  159. num3 = this.Acceleration.Evaluate(time);
  160. }
  161. Vector3 vector2 = Vector3.MoveTowards(this.tRoot.position, vector, this.effectSettings.MoveSpeed * Time.deltaTime * num3);
  162. Vector3 vector3 = vector2 + b;
  163. if (this.IsLookAt && this.effectSettings.IsHomingMove)
  164. {
  165. this.tRoot.LookAt(vector3);
  166. }
  167. if (this.IsLocalSpaceRandomMove && this.IsRootMove)
  168. {
  169. this.tRoot.position = vector2;
  170. this.t.localPosition += b;
  171. }
  172. else
  173. {
  174. this.tRoot.position = vector3;
  175. }
  176. this.oldSmootRandomPos = this.smootRandomPos;
  177. }
  178. private void CollisionEnter()
  179. {
  180. if (this.EffectOnHitObject != null && this.hit.transform != null)
  181. {
  182. Transform transform = this.hit.transform;
  183. Renderer componentInChildren = transform.GetComponentInChildren<Renderer>();
  184. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.EffectOnHitObject);
  185. gameObject.transform.parent = componentInChildren.transform;
  186. gameObject.transform.localPosition = Vector3.zero;
  187. gameObject.GetComponent<AddMaterialOnHit>().UpdateMaterial(this.hit);
  188. }
  189. if (this.AttachAfterCollision)
  190. {
  191. this.tRoot.parent = this.hit.transform;
  192. }
  193. if (this.SendCollisionMessage)
  194. {
  195. CollisionInfo e = new CollisionInfo
  196. {
  197. Hit = this.hit
  198. };
  199. this.effectSettings.OnCollisionHandler(e);
  200. if (this.hit.transform != null)
  201. {
  202. ShieldCollisionBehaviour component = this.hit.transform.GetComponent<ShieldCollisionBehaviour>();
  203. if (component != null)
  204. {
  205. component.ShieldCollisionEnter(e);
  206. }
  207. }
  208. }
  209. this.onCollision = true;
  210. }
  211. private void InitRandomVariables()
  212. {
  213. this.deltaSpeed = this.RandomMoveSpeed * UnityEngine.Random.Range(1f, 1000f * this.RandomRange + 1f) / 1000f - 1f;
  214. this.startTime = Time.time;
  215. this.randomRadiusX = UnityEngine.Random.Range(this.RandomMoveRadius / 20f, this.RandomMoveRadius * 100f) / 100f;
  216. this.randomRadiusY = UnityEngine.Random.Range(this.RandomMoveRadius / 20f, this.RandomMoveRadius * 100f) / 100f;
  217. this.randomSpeed = UnityEngine.Random.Range(this.RandomMoveSpeed / 20f, this.RandomMoveSpeed * 100f) / 100f;
  218. this.randomDirection1 = ((UnityEngine.Random.Range(0, 2) != 0) ? -1 : 1);
  219. this.randomDirection2 = ((UnityEngine.Random.Range(0, 2) != 0) ? -1 : 1);
  220. this.randomDirection3 = ((UnityEngine.Random.Range(0, 2) != 0) ? -1 : 1);
  221. }
  222. private void GetTargetHit()
  223. {
  224. Ray ray = new Ray(this.tRoot.position, Vector3.Normalize(this.tTarget.position + this.randomTargetOffsetXZVector - this.tRoot.position));
  225. Collider componentInChildren = this.tTarget.GetComponentInChildren<Collider>();
  226. RaycastHit raycastHit;
  227. if (componentInChildren != null && componentInChildren.Raycast(ray, out raycastHit, this.effectSettings.MoveDistance))
  228. {
  229. this.hit = raycastHit;
  230. }
  231. }
  232. private void UpdateSmootRandomhPos()
  233. {
  234. float num = Time.time - this.startTime;
  235. float num2 = num * this.randomSpeed;
  236. float f = num * this.deltaSpeed;
  237. float num4;
  238. float num5;
  239. if (this.IsDeviation)
  240. {
  241. float num3 = Vector3.Distance(this.tRoot.position, this.hit.point) / this.effectSettings.MoveDistance;
  242. num4 = (float)this.randomDirection2 * Mathf.Sin(num2) * this.randomRadiusX * num3;
  243. num5 = (float)this.randomDirection3 * Mathf.Sin(num2 + (float)this.randomDirection1 * 3.14159274f / 2f * num + Mathf.Sin(f)) * this.randomRadiusY * num3;
  244. }
  245. else
  246. {
  247. num4 = (float)this.randomDirection2 * Mathf.Sin(num2) * this.randomRadiusX;
  248. num5 = (float)this.randomDirection3 * Mathf.Sin(num2 + (float)this.randomDirection1 * 3.14159274f / 2f * num + Mathf.Sin(f)) * this.randomRadiusY;
  249. }
  250. if (this.RandomMoveCoordinates == RandomMoveCoordinates.XY)
  251. {
  252. this.smootRandomPos = new Vector3(num4, num5, 0f);
  253. }
  254. if (this.RandomMoveCoordinates == RandomMoveCoordinates.XZ)
  255. {
  256. this.smootRandomPos = new Vector3(num4, 0f, num5);
  257. }
  258. if (this.RandomMoveCoordinates == RandomMoveCoordinates.YZ)
  259. {
  260. this.smootRandomPos = new Vector3(0f, num4, num5);
  261. }
  262. if (this.RandomMoveCoordinates == RandomMoveCoordinates.XYZ)
  263. {
  264. this.smootRandomPos = new Vector3(num4, num5, (num4 + num5) / 2f * (float)this.randomDirection1);
  265. }
  266. }
  267. public float RandomMoveRadius;
  268. public float RandomMoveSpeed;
  269. public float RandomRange;
  270. public RandomMoveCoordinates RandomMoveCoordinates;
  271. public GameObject EffectOnHitObject;
  272. public GameObject GoLight;
  273. public AnimationCurve Acceleration;
  274. public float AcceleraionTime = 1f;
  275. public bool IsCenterLightPosition;
  276. public bool IsLookAt;
  277. public bool AttachAfterCollision;
  278. public bool IsRootMove = true;
  279. public bool IsLocalSpaceRandomMove;
  280. public bool IsDeviation;
  281. public bool SendCollisionMessage = true;
  282. public bool ResetParentPositionOnDisable;
  283. private EffectSettings effectSettings;
  284. private Transform tRoot;
  285. private Transform tTarget;
  286. private Transform t;
  287. private Transform tLight;
  288. private Vector3 forwardDirection;
  289. private Vector3 startPosition;
  290. private Vector3 startParentPosition;
  291. private RaycastHit hit;
  292. private Vector3 smootRandomPos;
  293. private Vector3 oldSmootRandomPos;
  294. private float deltaSpeed;
  295. private float startTime;
  296. private float randomSpeed;
  297. private float randomRadiusX;
  298. private float randomRadiusY;
  299. private int randomDirection1;
  300. private int randomDirection2;
  301. private int randomDirection3;
  302. private bool onCollision;
  303. private bool isInitializedOnStart;
  304. private Vector3 randomTargetOffsetXZVector;
  305. private bool frameDroped;
  306. }