flaresimulation.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. class FlareSimulation : Managed
  2. {
  3. protected Particle m_ParMainFire;
  4. protected EffectSound m_BurningSound;
  5. protected FlareLight m_FlareLight;
  6. const static float MAX_FARLIGHT_DIST = 40;
  7. const static float MIN_FARLIGHT_DIST = 5;
  8. static ref NoiseParams m_NoisePar; // Contains the noise data ( template and strength )
  9. float m_LastNoiseTime = -1;
  10. float m_NoiseTimer = 0;
  11. const float NOISE_DELAY = 5; // How much time between two consecutive noise pings
  12. // flare effect rotation
  13. protected const float FLARE_SPIN_RATE = 1.15; // in degrees per simul tick
  14. protected const float FLARE_SPIN_RADIUS = 0.18; // radius of circling motion
  15. protected Entity m_Flare;
  16. protected vector m_RotationPoint;
  17. protected vector m_FlarePosition;
  18. protected float m_RotationDegrees;
  19. static protected typename m_ScriptedLight;
  20. static protected int m_ParticleId;
  21. void FlareSimulation()
  22. {
  23. m_ScriptedLight = FlareLight;
  24. m_ParticleId = ParticleList.FLAREPROJ_ACTIVATE;
  25. }
  26. void OnActivation(Entity flare)
  27. {
  28. if ( !GetGame().IsServer() || !GetGame().IsMultiplayer() )
  29. {
  30. m_FlareLight = FlareLight.Cast( ScriptedLightBase.CreateLight( m_ScriptedLight, Vector(0,0,0) ) );
  31. if ( m_FlareLight )
  32. m_FlareLight.AttachOnObject( flare );
  33. if (m_ParMainFire)
  34. m_ParMainFire.Stop();
  35. m_Flare = flare;
  36. m_RotationPoint = m_Flare.GetOrigin();
  37. m_FlarePosition = m_Flare.GetOrigin() + Vector(FLARE_SPIN_RADIUS,0,FLARE_SPIN_RADIUS);
  38. m_ParMainFire = ParticleManager.GetInstance().PlayInWorld( m_ParticleId, m_FlarePosition );
  39. flare.PlaySoundSetLoop( m_BurningSound, "roadflareLoop_SoundSet", 0, 0 );
  40. }
  41. if ( GetGame().IsServer() )
  42. {
  43. // Create and load noise parameters
  44. m_NoisePar = new NoiseParams();
  45. m_NoisePar.LoadFromPath("cfgWeapons Flaregun_Base NoiseFlare");
  46. }
  47. }
  48. void OnTermination(Entity flare)
  49. {
  50. //MiscGameplayFunctions.GenerateAINoiseAtPosition(flare.GetPosition(), NOISE_DELAY, m_NoisePar);
  51. }
  52. void OnFire( Entity flare)
  53. {
  54. //m_ParMainFire = ParticleManager.GetInstance().PlayOnObject( ParticleList.FLAREPROJ_FIRE, flare);
  55. //m_ParMainFire.SetWiggle( 7, 0.3);
  56. }
  57. void Simulate( Entity flare )
  58. {
  59. DayZPlayer player = GetGame().GetPlayer();
  60. if ( player )
  61. vector playerPos = player.GetPosition();
  62. float dist = vector.DistanceSq(flare.GetPosition(), playerPos);
  63. if ( ( dist <= MAX_FARLIGHT_DIST * MAX_FARLIGHT_DIST ) && ( dist > MIN_FARLIGHT_DIST * MIN_FARLIGHT_DIST ) )
  64. m_ParMainFire.SetParameter( 0, EmitorParam.SIZE, MiscGameplayFunctions.Normalize(dist, MAX_FARLIGHT_DIST * MAX_FARLIGHT_DIST) );
  65. if ( dist <= MIN_FARLIGHT_DIST * MIN_FARLIGHT_DIST )
  66. TurnOffDistantLight();
  67. //CastFlareAINoise( flare.GetPosition() );
  68. FlareParticleUpdate();
  69. }
  70. // Rotate flare particle and set its new position
  71. protected void FlareParticleUpdate()
  72. {
  73. m_RotationDegrees += FLARE_SPIN_RATE;
  74. if (m_RotationDegrees > 360)
  75. m_RotationDegrees = 0;
  76. float angleRad = m_RotationDegrees * Math.DEG2RAD;
  77. float sin = Math.Sin(angleRad);
  78. float cos = Math.Cos(angleRad);
  79. vector newFlarePos = m_ParMainFire.GetOrigin();
  80. float surfacePos = GetGame().SurfaceY(newFlarePos[0], newFlarePos[2]);
  81. if (newFlarePos[1] - surfacePos < 1) // reached ground
  82. {
  83. if (m_Flare.GetOrigin()[1] <= surfacePos) // actual flare pos might not match and height could be underground
  84. m_ParMainFire.SetPosition(Vector(newFlarePos[0], surfacePos, newFlarePos[2]));
  85. else
  86. m_ParMainFire.SetPosition(Vector(newFlarePos[0], m_Flare.GetOrigin()[1] ,newFlarePos[2]));
  87. return;
  88. }
  89. // rotate vector around point
  90. float xRotated = ((m_FlarePosition[0] - m_RotationPoint[0]) * cos) - ((m_FlarePosition[2] - m_RotationPoint[2]) * sin) + m_RotationPoint[0];
  91. float yRotated = ((m_FlarePosition[0] - m_RotationPoint[0]) * sin) + ((m_FlarePosition[2] - m_RotationPoint[2]) * cos) + m_RotationPoint[2];
  92. newFlarePos[0] = xRotated;
  93. newFlarePos[1] = m_Flare.GetOrigin()[1];
  94. newFlarePos[2] = yRotated;
  95. m_ParMainFire.SetPosition(newFlarePos);
  96. }
  97. void CastFlareAINoise( vector position )
  98. {
  99. if ( m_LastNoiseTime < 0 )
  100. m_LastNoiseTime = GetGame().GetTime() * 0.0033;
  101. float delta_time = GetGame().GetTime() * 0.0033 - m_LastNoiseTime;
  102. m_LastNoiseTime = GetGame().GetTime() * 0.0033;
  103. m_NoiseTimer += delta_time;
  104. if ( m_NoiseTimer >= NOISE_DELAY )
  105. {
  106. MiscGameplayFunctions.GenerateAINoiseAtPosition(position, NOISE_DELAY, m_NoisePar);
  107. m_NoiseTimer = 0;
  108. }
  109. }
  110. void TurnOffDistantLight()
  111. {
  112. if (m_ParMainFire)
  113. {
  114. m_ParMainFire.SetParameter(0, EmitorParam.LIFETIME, 0);
  115. m_ParMainFire.SetParameter(0, EmitorParam.LIFETIME_RND, 0);
  116. m_ParMainFire.SetParameter(0, EmitorParam.REPEAT, 0);
  117. m_ParMainFire.SetParameter(0, EmitorParam.SIZE, 0);
  118. }
  119. }
  120. void ~FlareSimulation()
  121. {
  122. if (m_ParMainFire)
  123. m_ParMainFire.Stop();
  124. if (m_BurningSound)
  125. m_BurningSound.SoundStop();
  126. if (m_FlareLight)
  127. m_FlareLight.FadeOut();
  128. }
  129. }
  130. class FlareSimulation_Red : FlareSimulation
  131. {
  132. void FlareSimulation_Red()
  133. {
  134. m_ScriptedLight = FlareLightRed;
  135. m_ParticleId = ParticleList.FLAREPROJ_ACTIVATE_RED;
  136. }
  137. }
  138. class FlareSimulation_Green : FlareSimulation
  139. {
  140. void FlareSimulation_Green()
  141. {
  142. m_ScriptedLight = FlareLightGreen;
  143. m_ParticleId = ParticleList.FLAREPROJ_ACTIVATE_GREEN;
  144. }
  145. }
  146. class FlareSimulation_Blue : FlareSimulation
  147. {
  148. void FlareSimulation_Blue()
  149. {
  150. m_ScriptedLight = FlareLightBlue;
  151. m_ParticleId = ParticleList.FLAREPROJ_ACTIVATE_BLUE;
  152. }
  153. }