smokegrenadebase.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. enum ESmokeGrenadeState
  2. {
  3. NO_SMOKE = 0
  4. START = 1,
  5. LOOP = 2,
  6. END = 3,
  7. //! ---
  8. COUNT = 4
  9. }
  10. class SmokeGrenadeBase extends Grenade_Base
  11. {
  12. protected ref Timer m_TimerSmokeStart;
  13. protected ref Timer m_TimerSmokeLoop;
  14. protected ref Timer m_TimerSmokeEnd;
  15. protected ref Timer m_TimerDefer;
  16. protected ESmokeGrenadeState m_SmokeGrenadeState;
  17. protected ESmokeGrenadeState m_LastSmokeGrenadeState;
  18. //! particle
  19. protected Particle m_ParticleSmoke;
  20. protected int m_ParticleSmokeCurrentId;
  21. protected int m_ParticleSmokeStartId;
  22. protected int m_ParticleSmokeLoopId;
  23. protected int m_ParticleSmokeEndId;
  24. protected vector m_ParticleSmokePosition;
  25. //! sounds
  26. protected EffectSound m_SoundSmoke;
  27. protected string m_SoundSmokeStartId;
  28. protected string m_SoundSmokeLoopId;
  29. protected string m_SoundSmokeEndId;
  30. //! Noise
  31. ref NoiseParams m_NoisePar;
  32. void SetParticleSmokeCurrent(int particle)
  33. {
  34. m_ParticleSmokeCurrentId = particle;
  35. }
  36. void SetParticleSmokeStart(int particle)
  37. {
  38. m_ParticleSmokeStartId = particle;
  39. }
  40. void SetParticleSmokeLoop(int particle)
  41. {
  42. m_ParticleSmokeLoopId = particle;
  43. }
  44. void SetParticleSmokeEnd(int particle)
  45. {
  46. m_ParticleSmokeEndId = particle;
  47. }
  48. void SetSoundSmokeStart(string sound)
  49. {
  50. m_SoundSmokeStartId = sound;
  51. }
  52. void SetSoundSmokeLoop(string sound)
  53. {
  54. m_SoundSmokeLoopId = sound;
  55. }
  56. void SetSoundSmokeEnd(string sound)
  57. {
  58. m_SoundSmokeEndId = sound;
  59. }
  60. protected void PlaySmokeParticle()
  61. {
  62. m_ParticleSmoke = ParticleManager.GetInstance().PlayOnObject(m_ParticleSmokeCurrentId, this, m_ParticlePosition, vector.Zero, true);
  63. }
  64. protected void SoundSmokeStart()
  65. {
  66. PlaySoundSetLoop(m_SoundSmoke, m_SoundSmokeStartId, 0.3, 1.0);
  67. }
  68. protected void SoundSmokeLoop()
  69. {
  70. PlaySoundSetLoop(m_SoundSmoke, m_SoundSmokeLoopId, 0.3, 1.0);
  71. }
  72. protected void SoundSmokeEnd()
  73. {
  74. PlaySoundSetLoop(m_SoundSmoke, m_SoundSmokeEndId, 1.0, 1.0);
  75. }
  76. //Stop
  77. protected void SoundSmokeStop()
  78. {
  79. StopSoundSet(m_SoundSmoke);
  80. }
  81. protected void DestroySmokeGrenade()
  82. {
  83. SetSmokeGrenadeState(ESmokeGrenadeState.NO_SMOKE);
  84. if ( GetGame().IsServer() )
  85. {
  86. SetHealth("", "", 0);
  87. }
  88. }
  89. protected void RefreshParticlesAndSounds()
  90. {
  91. ESmokeGrenadeState state = GetSmokeGrenadeState();
  92. if ( m_LastSmokeGrenadeState != state )
  93. {
  94. if ( state == ESmokeGrenadeState.START )
  95. {
  96. //Print("RefreshAudioVisual:: START");
  97. SoundSmokeEnd();
  98. SoundSmokeStart();
  99. DestroyParticle(m_ParticleSmoke);
  100. SetParticleSmokeCurrent(m_ParticleSmokeStartId);
  101. PlaySmokeParticle();
  102. }
  103. else if ( state == ESmokeGrenadeState.LOOP )
  104. {
  105. //Print("RefreshAudioVisual:: LOOP");
  106. SoundSmokeStop();
  107. SoundSmokeLoop();
  108. DestroyParticle(m_ParticleSmoke);
  109. SetParticleSmokeCurrent(m_ParticleSmokeLoopId);
  110. PlaySmokeParticle();
  111. }
  112. else if ( state == ESmokeGrenadeState.END )
  113. {
  114. //Print("RefreshAudioVisual:: END");
  115. SoundSmokeStop();
  116. SoundSmokeEnd();
  117. DestroyParticle(m_ParticleSmoke);
  118. SetParticleSmokeCurrent(m_ParticleSmokeEndId);
  119. PlaySmokeParticle();
  120. }
  121. else if ( state == ESmokeGrenadeState.NO_SMOKE )
  122. {
  123. //Print("RefreshAudioVisual:: NO_SMOKE");
  124. SoundSmokeStop();
  125. DestroyParticle(m_ParticleSmoke);
  126. }
  127. m_LastSmokeGrenadeState = state;
  128. }
  129. }
  130. override void Unpin()
  131. {
  132. super.Unpin();
  133. Activate();
  134. }
  135. override void OnActivateFinished()
  136. {
  137. if ( GetCompEM() && GetCompEM().CanWork() )
  138. {
  139. GetCompEM().SwitchOn();
  140. }
  141. }
  142. // When smoke starts
  143. override void OnWorkStart()
  144. {
  145. SetSmokeGrenadeState(ESmokeGrenadeState.START);
  146. if ( GetGame().IsServer() )
  147. {
  148. m_NoisePar = new NoiseParams();
  149. m_NoisePar.LoadFromPath("cfgVehicles " + GetType() + " NoiseSmokeGrenade");
  150. NoiseSystem noise = GetGame().GetNoiseSystem();
  151. if ( noise )
  152. {
  153. noise.AddNoisePos( this, GetPosition(), m_NoisePar, NoiseAIEvaluate.GetNoiseReduction(GetGame().GetWeather()) );
  154. }
  155. }
  156. Param1<ESmokeGrenadeState> par = new Param1<ESmokeGrenadeState>(ESmokeGrenadeState.LOOP);
  157. m_TimerSmokeLoop.Run(5.0, this, "SetSmokeGrenadeState", par);
  158. }
  159. //When grenade makes smoke
  160. override void OnWork(float consumed_energy)
  161. {
  162. if ( GetGame().IsServer() )
  163. {
  164. NoiseSystem noise = GetGame().GetNoiseSystem();
  165. if ( noise )
  166. {
  167. noise.AddNoisePos( this, GetPosition(), m_NoisePar, NoiseAIEvaluate.GetNoiseReduction(GetGame().GetWeather()));
  168. }
  169. }
  170. }
  171. // When the smoke stops
  172. override void OnWorkStop()
  173. {
  174. SetSmokeGrenadeState(ESmokeGrenadeState.END);
  175. //! defer timer
  176. m_TimerDefer.Run(5.0, this, "DestroySmokeGrenade");
  177. }
  178. override bool CanPutInCargo( EntityAI parent )
  179. {
  180. return !GetCompEM().IsWorking();
  181. }
  182. override void OnActivatedByItem(notnull ItemBase item)
  183. {
  184. GetCompEM().SwitchOn();
  185. }
  186. override void SetActions()
  187. {
  188. super.SetActions();
  189. RemoveAction(ActionPin);
  190. }
  191. override void OnVariablesSynchronized()
  192. {
  193. super.OnVariablesSynchronized();
  194. RefreshParticlesAndSounds();
  195. }
  196. override void OnExplosionEffects(Object source, Object directHit, int componentIndex, string surface, vector pos, vector surfNormal, float energyFactor, float explosionFactor, bool isWater, string ammoType) {}
  197. ESmokeGrenadeState GetSmokeGrenadeState()
  198. {
  199. return m_SmokeGrenadeState;
  200. }
  201. override void EEDelete(EntityAI parent)
  202. {
  203. super.EEDelete(parent);
  204. SoundSmokeStop();
  205. DestroyParticle(m_ParticleSmoke);
  206. }
  207. void SetSmokeGrenadeState(ESmokeGrenadeState state)
  208. {
  209. //Print("Setting SGS to: " + typename.EnumToString(ESmokeGrenadeState, state));
  210. if ( GetGame().IsServer() )
  211. {
  212. if ( m_SmokeGrenadeState != state )
  213. {
  214. m_SmokeGrenadeState = state;
  215. //synchronize
  216. SetSynchDirty();
  217. }
  218. }
  219. }
  220. void SmokeGrenadeBase()
  221. {
  222. m_SmokeGrenadeState = ESmokeGrenadeState.NO_SMOKE;
  223. m_TimerSmokeLoop = new Timer;
  224. m_TimerDefer = new Timer;
  225. SetAmmoType("");
  226. SetPinnable(false);
  227. SetGrenadeType(EGrenadeType.CHEMICAL);
  228. SetParticleSmokeStart(ParticleList.INVALID); //! no effect on base
  229. SetParticleSmokeLoop(ParticleList.INVALID); //! no effect on base
  230. SetParticleSmokeEnd(ParticleList.INVALID); //! no effect on base
  231. RegisterNetSyncVariableInt("m_SmokeGrenadeState", ESmokeGrenadeState.NO_SMOKE, ESmokeGrenadeState.COUNT);
  232. }
  233. void ~SmokeGrenadeBase();
  234. }