sfxModifier.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/simBase.h"
  23. #include "console/engineAPI.h"
  24. #include "sfx/sfxModifier.h"
  25. #include "sfx/sfxSource.h"
  26. //=============================================================================
  27. // SFXOneShotModifier.
  28. //=============================================================================
  29. //-----------------------------------------------------------------------------
  30. SFXOneShotModifier::SFXOneShotModifier( SFXSource* source, F32 triggerPos, bool removeWhenDone )
  31. : Parent( source, removeWhenDone ),
  32. mTriggerPos( triggerPos )
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. bool SFXOneShotModifier::update()
  37. {
  38. if( mSource->getElapsedPlayTimeCurrentCycle() >= mTriggerPos )
  39. {
  40. _onTrigger();
  41. return mRemoveWhenDone;
  42. }
  43. else
  44. return true;
  45. }
  46. //=============================================================================
  47. // SFXRangeModifier.
  48. //=============================================================================
  49. //-----------------------------------------------------------------------------
  50. SFXRangeModifier::SFXRangeModifier( SFXSource* source, F32 startTime, F32 endTime, bool removeWhenDone )
  51. : Parent( source, removeWhenDone ),
  52. mStartTime( startTime ),
  53. mEndTime( endTime ),
  54. mIsActive( false )
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. bool SFXRangeModifier::update()
  59. {
  60. if( !isActive() )
  61. {
  62. SFXStatus status = mSource->getStatus();
  63. if( ( status == SFXStatusPlaying || status == SFXStatusBlocked )
  64. && mSource->getElapsedPlayTimeCurrentCycle() >= mStartTime )
  65. {
  66. mIsActive = true;
  67. _onStart();
  68. }
  69. }
  70. if( isActive() )
  71. _onUpdate();
  72. if( isActive() )
  73. {
  74. SFXStatus status = mSource->getStatus();
  75. if( ( status == SFXStatusPlaying || status == SFXStatusBlocked )
  76. && mSource->getElapsedPlayTimeCurrentCycle() > mEndTime )
  77. {
  78. _onEnd();
  79. mIsActive = false;
  80. return mRemoveWhenDone;
  81. }
  82. }
  83. return true;
  84. }
  85. //=============================================================================
  86. // SFXFadeModifier.
  87. //=============================================================================
  88. //-----------------------------------------------------------------------------
  89. SFXFadeModifier::SFXFadeModifier( SFXSource* source, F32 time, F32 endVolume, F32 startTime, EOnEnd onEndDo, bool removeWhenDone )
  90. : Parent( source, startTime, startTime + time, removeWhenDone ),
  91. mEndVolume( endVolume ),
  92. mOnEnd( onEndDo )
  93. {
  94. }
  95. //-----------------------------------------------------------------------------
  96. SFXFadeModifier::~SFXFadeModifier()
  97. {
  98. // If the fade is still ongoing, restore the source's volume.
  99. // For fade-in, set to end volume. For fade-out, set to start volume.
  100. if( isActive() )
  101. {
  102. if( mStartVolume > mEndVolume )
  103. mSource->setVolume( mStartVolume );
  104. else
  105. mSource->setVolume( mEndVolume );
  106. }
  107. }
  108. //-----------------------------------------------------------------------------
  109. void SFXFadeModifier::_onStart()
  110. {
  111. mStartVolume = mSource->getVolume();
  112. mCurrentVolume = mStartVolume;
  113. }
  114. //-----------------------------------------------------------------------------
  115. void SFXFadeModifier::_onUpdate()
  116. {
  117. F32 multiplier = ( mSource->getElapsedPlayTimeCurrentCycle() - mStartTime ) / ( mEndTime - mStartTime );
  118. F32 newVolume;
  119. if( mStartVolume > mEndVolume )
  120. newVolume = mStartVolume - ( ( mStartVolume - mEndVolume ) * multiplier );
  121. else
  122. newVolume = mStartVolume + ( ( mEndVolume - mStartVolume ) * multiplier );
  123. if( newVolume != mCurrentVolume )
  124. {
  125. mCurrentVolume = newVolume;
  126. mSource->setVolume( mCurrentVolume );
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. void SFXFadeModifier::_onEnd()
  131. {
  132. mSource->setVolume( mEndVolume );
  133. switch( mOnEnd )
  134. {
  135. case ON_END_Pause:
  136. mSource->pause( 0.f ); // Pause without fade.
  137. break;
  138. case ON_END_Stop:
  139. mSource->stop( 0.f ); // Stop without fade.
  140. break;
  141. case ON_END_Nop: ;
  142. }
  143. }
  144. //=============================================================================
  145. // SFXMarkerModifier.
  146. //=============================================================================
  147. //-----------------------------------------------------------------------------
  148. SFXMarkerModifier::SFXMarkerModifier( SFXSource* source, const String& name, F32 pos, bool removeWhenDone )
  149. : Parent( source, pos, removeWhenDone ),
  150. mMarkerName( name )
  151. {
  152. }
  153. //-----------------------------------------------------------------------------
  154. void SFXMarkerModifier::_onTrigger()
  155. {
  156. Con::executef( mSource, "onMarkerPassed", mMarkerName.c_str() );
  157. }