Detonator.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("Detonator/Detonator")]
  4. public class Detonator : MonoBehaviour
  5. {
  6. private void Awake()
  7. {
  8. this.FillDefaultMaterials();
  9. this.components = base.GetComponents(typeof(DetonatorComponent));
  10. foreach (DetonatorComponent detonatorComponent in this.components)
  11. {
  12. if (detonatorComponent is DetonatorFireball)
  13. {
  14. this._fireball = (detonatorComponent as DetonatorFireball);
  15. }
  16. if (detonatorComponent is DetonatorSparks)
  17. {
  18. this._sparks = (detonatorComponent as DetonatorSparks);
  19. }
  20. if (detonatorComponent is DetonatorShockwave)
  21. {
  22. this._shockwave = (detonatorComponent as DetonatorShockwave);
  23. }
  24. if (detonatorComponent is DetonatorSmoke)
  25. {
  26. this._smoke = (detonatorComponent as DetonatorSmoke);
  27. }
  28. if (detonatorComponent is DetonatorGlow)
  29. {
  30. this._glow = (detonatorComponent as DetonatorGlow);
  31. }
  32. if (detonatorComponent is DetonatorLight)
  33. {
  34. this._light = (detonatorComponent as DetonatorLight);
  35. }
  36. if (detonatorComponent is DetonatorForce)
  37. {
  38. this._force = (detonatorComponent as DetonatorForce);
  39. }
  40. if (detonatorComponent is DetonatorHeatwave)
  41. {
  42. this._heatwave = (detonatorComponent as DetonatorHeatwave);
  43. }
  44. }
  45. if (!this._fireball && this.autoCreateFireball)
  46. {
  47. this._fireball = base.gameObject.AddComponent<DetonatorFireball>();
  48. this._fireball.Reset();
  49. }
  50. if (!this._smoke && this.autoCreateSmoke)
  51. {
  52. this._smoke = base.gameObject.AddComponent<DetonatorSmoke>();
  53. this._smoke.Reset();
  54. }
  55. if (!this._sparks && this.autoCreateSparks)
  56. {
  57. this._sparks = base.gameObject.AddComponent<DetonatorSparks>();
  58. this._sparks.Reset();
  59. }
  60. if (!this._shockwave && this.autoCreateShockwave)
  61. {
  62. this._shockwave = base.gameObject.AddComponent<DetonatorShockwave>();
  63. this._shockwave.Reset();
  64. }
  65. if (!this._glow && this.autoCreateGlow)
  66. {
  67. this._glow = base.gameObject.AddComponent<DetonatorGlow>();
  68. this._glow.Reset();
  69. }
  70. if (!this._light && this.autoCreateLight)
  71. {
  72. this._light = base.gameObject.AddComponent<DetonatorLight>();
  73. this._light.Reset();
  74. }
  75. if (!this._force && this.autoCreateForce)
  76. {
  77. this._force = base.gameObject.AddComponent<DetonatorForce>();
  78. this._force.Reset();
  79. }
  80. if (!this._heatwave && this.autoCreateHeatwave && SystemInfo.supportsImageEffects)
  81. {
  82. this._heatwave = base.gameObject.AddComponent<DetonatorHeatwave>();
  83. this._heatwave.Reset();
  84. }
  85. this.components = base.GetComponents(typeof(DetonatorComponent));
  86. }
  87. private void FillDefaultMaterials()
  88. {
  89. if (!this.fireballAMaterial)
  90. {
  91. this.fireballAMaterial = Detonator.DefaultFireballAMaterial();
  92. }
  93. if (!this.fireballBMaterial)
  94. {
  95. this.fireballBMaterial = Detonator.DefaultFireballBMaterial();
  96. }
  97. if (!this.smokeAMaterial)
  98. {
  99. this.smokeAMaterial = Detonator.DefaultSmokeAMaterial();
  100. }
  101. if (!this.smokeBMaterial)
  102. {
  103. this.smokeBMaterial = Detonator.DefaultSmokeBMaterial();
  104. }
  105. if (!this.shockwaveMaterial)
  106. {
  107. this.shockwaveMaterial = Detonator.DefaultShockwaveMaterial();
  108. }
  109. if (!this.sparksMaterial)
  110. {
  111. this.sparksMaterial = Detonator.DefaultSparksMaterial();
  112. }
  113. if (!this.glowMaterial)
  114. {
  115. this.glowMaterial = Detonator.DefaultGlowMaterial();
  116. }
  117. if (!this.heatwaveMaterial)
  118. {
  119. this.heatwaveMaterial = Detonator.DefaultHeatwaveMaterial();
  120. }
  121. }
  122. private void Start()
  123. {
  124. if (this.explodeOnStart)
  125. {
  126. this.UpdateComponents();
  127. this.Explode();
  128. }
  129. }
  130. private void Update()
  131. {
  132. if (this.destroyTime > 0f && this._lastExplosionTime + this.destroyTime <= Time.time)
  133. {
  134. UnityEngine.Object.Destroy(base.gameObject);
  135. }
  136. }
  137. private void UpdateComponents()
  138. {
  139. if (this._firstComponentUpdate)
  140. {
  141. foreach (DetonatorComponent detonatorComponent in this.components)
  142. {
  143. detonatorComponent.Init();
  144. detonatorComponent.SetStartValues();
  145. }
  146. this._firstComponentUpdate = false;
  147. }
  148. if (!this._firstComponentUpdate)
  149. {
  150. float num = this.size / Detonator._baseSize;
  151. Vector3 vector = new Vector3(this.direction.x * num, this.direction.y * num, this.direction.z * num);
  152. float timeScale = this.duration / Detonator._baseDuration;
  153. foreach (DetonatorComponent detonatorComponent2 in this.components)
  154. {
  155. if (detonatorComponent2.detonatorControlled)
  156. {
  157. detonatorComponent2.size = detonatorComponent2.startSize * num;
  158. detonatorComponent2.timeScale = timeScale;
  159. detonatorComponent2.detail = detonatorComponent2.startDetail * this.detail;
  160. detonatorComponent2.force = new Vector3(detonatorComponent2.startForce.x * num + vector.x, detonatorComponent2.startForce.y * num + vector.y, detonatorComponent2.startForce.z * num + vector.z);
  161. detonatorComponent2.velocity = new Vector3(detonatorComponent2.startVelocity.x * num + vector.x, detonatorComponent2.startVelocity.y * num + vector.y, detonatorComponent2.startVelocity.z * num + vector.z);
  162. detonatorComponent2.color = Color.Lerp(detonatorComponent2.startColor, this.color, this.color.a);
  163. }
  164. }
  165. }
  166. }
  167. public void Explode()
  168. {
  169. this._lastExplosionTime = Time.time;
  170. foreach (DetonatorComponent detonatorComponent in this.components)
  171. {
  172. this.UpdateComponents();
  173. detonatorComponent.Explode();
  174. }
  175. }
  176. public void Reset()
  177. {
  178. this.size = 10f;
  179. this.color = Detonator._baseColor;
  180. this.duration = Detonator._baseDuration;
  181. this.FillDefaultMaterials();
  182. }
  183. public static Material DefaultFireballAMaterial()
  184. {
  185. if (Detonator.defaultFireballAMaterial != null)
  186. {
  187. return Detonator.defaultFireballAMaterial;
  188. }
  189. Detonator.defaultFireballAMaterial = new Material(Shader.Find("Particles/Additive"));
  190. Detonator.defaultFireballAMaterial.name = "FireballA-Default";
  191. Texture2D mainTexture = Resources.Load("Detonator/Textures/Fireball") as Texture2D;
  192. Detonator.defaultFireballAMaterial.SetColor("_TintColor", Color.white);
  193. Detonator.defaultFireballAMaterial.mainTexture = mainTexture;
  194. Detonator.defaultFireballAMaterial.mainTextureScale = new Vector2(0.5f, 1f);
  195. return Detonator.defaultFireballAMaterial;
  196. }
  197. public static Material DefaultFireballBMaterial()
  198. {
  199. if (Detonator.defaultFireballBMaterial != null)
  200. {
  201. return Detonator.defaultFireballBMaterial;
  202. }
  203. Detonator.defaultFireballBMaterial = new Material(Shader.Find("Particles/Additive"));
  204. Detonator.defaultFireballBMaterial.name = "FireballB-Default";
  205. Texture2D mainTexture = Resources.Load("Detonator/Textures/Fireball") as Texture2D;
  206. Detonator.defaultFireballBMaterial.SetColor("_TintColor", Color.white);
  207. Detonator.defaultFireballBMaterial.mainTexture = mainTexture;
  208. Detonator.defaultFireballBMaterial.mainTextureScale = new Vector2(0.5f, 1f);
  209. Detonator.defaultFireballBMaterial.mainTextureOffset = new Vector2(0.5f, 0f);
  210. return Detonator.defaultFireballBMaterial;
  211. }
  212. public static Material DefaultSmokeAMaterial()
  213. {
  214. if (Detonator.defaultSmokeAMaterial != null)
  215. {
  216. return Detonator.defaultSmokeAMaterial;
  217. }
  218. Detonator.defaultSmokeAMaterial = new Material(Shader.Find("Particles/Alpha Blended"));
  219. Detonator.defaultSmokeAMaterial.name = "SmokeA-Default";
  220. Texture2D mainTexture = Resources.Load("Detonator/Textures/Smoke") as Texture2D;
  221. Detonator.defaultSmokeAMaterial.SetColor("_TintColor", Color.white);
  222. Detonator.defaultSmokeAMaterial.mainTexture = mainTexture;
  223. Detonator.defaultSmokeAMaterial.mainTextureScale = new Vector2(0.5f, 1f);
  224. return Detonator.defaultSmokeAMaterial;
  225. }
  226. public static Material DefaultSmokeBMaterial()
  227. {
  228. if (Detonator.defaultSmokeBMaterial != null)
  229. {
  230. return Detonator.defaultSmokeBMaterial;
  231. }
  232. Detonator.defaultSmokeBMaterial = new Material(Shader.Find("Particles/Alpha Blended"));
  233. Detonator.defaultSmokeBMaterial.name = "SmokeB-Default";
  234. Texture2D mainTexture = Resources.Load("Detonator/Textures/Smoke") as Texture2D;
  235. Detonator.defaultSmokeBMaterial.SetColor("_TintColor", Color.white);
  236. Detonator.defaultSmokeBMaterial.mainTexture = mainTexture;
  237. Detonator.defaultSmokeBMaterial.mainTextureScale = new Vector2(0.5f, 1f);
  238. Detonator.defaultSmokeBMaterial.mainTextureOffset = new Vector2(0.5f, 0f);
  239. return Detonator.defaultSmokeBMaterial;
  240. }
  241. public static Material DefaultSparksMaterial()
  242. {
  243. if (Detonator.defaultSparksMaterial != null)
  244. {
  245. return Detonator.defaultSparksMaterial;
  246. }
  247. Detonator.defaultSparksMaterial = new Material(Shader.Find("Particles/Additive"));
  248. Detonator.defaultSparksMaterial.name = "Sparks-Default";
  249. Texture2D mainTexture = Resources.Load("Detonator/Textures/GlowDot") as Texture2D;
  250. Detonator.defaultSparksMaterial.SetColor("_TintColor", Color.white);
  251. Detonator.defaultSparksMaterial.mainTexture = mainTexture;
  252. return Detonator.defaultSparksMaterial;
  253. }
  254. public static Material DefaultShockwaveMaterial()
  255. {
  256. if (Detonator.defaultShockwaveMaterial != null)
  257. {
  258. return Detonator.defaultShockwaveMaterial;
  259. }
  260. Detonator.defaultShockwaveMaterial = new Material(Shader.Find("Particles/Additive"));
  261. Detonator.defaultShockwaveMaterial.name = "Shockwave-Default";
  262. Texture2D mainTexture = Resources.Load("Detonator/Textures/Shockwave") as Texture2D;
  263. Detonator.defaultShockwaveMaterial.SetColor("_TintColor", new Color(0.1f, 0.1f, 0.1f, 1f));
  264. Detonator.defaultShockwaveMaterial.mainTexture = mainTexture;
  265. return Detonator.defaultShockwaveMaterial;
  266. }
  267. public static Material DefaultGlowMaterial()
  268. {
  269. if (Detonator.defaultGlowMaterial != null)
  270. {
  271. return Detonator.defaultGlowMaterial;
  272. }
  273. Detonator.defaultGlowMaterial = new Material(Shader.Find("Particles/Additive"));
  274. Detonator.defaultGlowMaterial.name = "Glow-Default";
  275. Texture2D mainTexture = Resources.Load("Detonator/Textures/Glow") as Texture2D;
  276. Detonator.defaultGlowMaterial.SetColor("_TintColor", Color.white);
  277. Detonator.defaultGlowMaterial.mainTexture = mainTexture;
  278. return Detonator.defaultGlowMaterial;
  279. }
  280. public static Material DefaultHeatwaveMaterial()
  281. {
  282. if (!SystemInfo.supportsImageEffects)
  283. {
  284. return null;
  285. }
  286. if (Detonator.defaultHeatwaveMaterial != null)
  287. {
  288. return Detonator.defaultHeatwaveMaterial;
  289. }
  290. Detonator.defaultHeatwaveMaterial = new Material(Shader.Find("HeatDistort"));
  291. Detonator.defaultHeatwaveMaterial.name = "Heatwave-Default";
  292. Texture2D value = Resources.Load("Detonator/Textures/Heatwave") as Texture2D;
  293. Detonator.defaultHeatwaveMaterial.SetTexture("_BumpMap", value);
  294. return Detonator.defaultHeatwaveMaterial;
  295. }
  296. private static float _baseSize = 30f;
  297. private static Color _baseColor = new Color(1f, 0.423f, 0f, 0.5f);
  298. private static float _baseDuration = 3f;
  299. public float size = 10f;
  300. public Color color = Detonator._baseColor;
  301. public bool explodeOnStart = true;
  302. public float duration = Detonator._baseDuration;
  303. public float detail = 1f;
  304. public float upwardsBias;
  305. public float destroyTime = 7f;
  306. public bool useWorldSpace = true;
  307. public Vector3 direction = Vector3.zero;
  308. public Material fireballAMaterial;
  309. public Material fireballBMaterial;
  310. public Material smokeAMaterial;
  311. public Material smokeBMaterial;
  312. public Material shockwaveMaterial;
  313. public Material sparksMaterial;
  314. public Material glowMaterial;
  315. public Material heatwaveMaterial;
  316. private Component[] components;
  317. private DetonatorFireball _fireball;
  318. private DetonatorSparks _sparks;
  319. private DetonatorShockwave _shockwave;
  320. private DetonatorSmoke _smoke;
  321. private DetonatorGlow _glow;
  322. private DetonatorLight _light;
  323. private DetonatorForce _force;
  324. private DetonatorHeatwave _heatwave;
  325. public bool autoCreateFireball = true;
  326. public bool autoCreateSparks = true;
  327. public bool autoCreateShockwave = true;
  328. public bool autoCreateSmoke = true;
  329. public bool autoCreateGlow = true;
  330. public bool autoCreateLight = true;
  331. public bool autoCreateForce = true;
  332. public bool autoCreateHeatwave;
  333. private float _lastExplosionTime = 1000f;
  334. private bool _firstComponentUpdate = true;
  335. private Component[] _subDetonators;
  336. public static Material defaultFireballAMaterial;
  337. public static Material defaultFireballBMaterial;
  338. public static Material defaultSmokeAMaterial;
  339. public static Material defaultSmokeBMaterial;
  340. public static Material defaultShockwaveMaterial;
  341. public static Material defaultSparksMaterial;
  342. public static Material defaultGlowMaterial;
  343. public static Material defaultHeatwaveMaterial;
  344. }