sfxModifier.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "sfx/sfxModifier.h"
  23. #include "sfx/sfxSource.h"
  24. //=============================================================================
  25. // SFXOneShotModifier.
  26. //=============================================================================
  27. //-----------------------------------------------------------------------------
  28. SFXOneShotModifier::SFXOneShotModifier( SFXSource* source, F32 triggerPos, bool removeWhenDone )
  29. : Parent( source, removeWhenDone ),
  30. mTriggerPos( triggerPos )
  31. {
  32. }
  33. //-----------------------------------------------------------------------------
  34. bool SFXOneShotModifier::update()
  35. {
  36. if( mSource->getElapsedPlayTimeCurrentCycle() >= mTriggerPos )
  37. {
  38. _onTrigger();
  39. return mRemoveWhenDone;
  40. }
  41. else
  42. return true;
  43. }
  44. //=============================================================================
  45. // SFXRangeModifier.
  46. //=============================================================================
  47. //-----------------------------------------------------------------------------
  48. SFXRangeModifier::SFXRangeModifier( SFXSource* source, F32 startTime, F32 endTime, bool removeWhenDone )
  49. : Parent( source, removeWhenDone ),
  50. mStartTime( startTime ),
  51. mEndTime( endTime ),
  52. mIsActive( false )
  53. {
  54. }
  55. //-----------------------------------------------------------------------------
  56. bool SFXRangeModifier::update()
  57. {
  58. if( !isActive() )
  59. {
  60. SFXStatus status = mSource->getStatus();
  61. if( ( status == SFXStatusPlaying || status == SFXStatusBlocked )
  62. && mSource->getElapsedPlayTimeCurrentCycle() >= mStartTime )
  63. {
  64. mIsActive = true;
  65. _onStart();
  66. }
  67. }
  68. if( isActive() )
  69. _onUpdate();
  70. if( isActive() )
  71. {
  72. SFXStatus status = mSource->getStatus();
  73. if( ( status == SFXStatusPlaying || status == SFXStatusBlocked )
  74. && mSource->getElapsedPlayTimeCurrentCycle() > mEndTime )
  75. {
  76. _onEnd();
  77. mIsActive = false;
  78. return mRemoveWhenDone;
  79. }
  80. }
  81. return true;
  82. }
  83. //=============================================================================
  84. // SFXFadeModifier.
  85. //=============================================================================
  86. //-----------------------------------------------------------------------------
  87. SFXFadeModifier::SFXFadeModifier( SFXSource* source, F32 time, F32 endVolume, F32 startTime, EOnEnd onEndDo, bool removeWhenDone )
  88. : Parent( source, startTime, startTime + time, removeWhenDone ),
  89. mEndVolume( endVolume ),
  90. mOnEnd( onEndDo )
  91. {
  92. }
  93. //-----------------------------------------------------------------------------
  94. SFXFadeModifier::~SFXFadeModifier()
  95. {
  96. // If the fade is still ongoing, restore the source's volume.
  97. // For fade-in, set to end volume. For fade-out, set to start volume.
  98. if( isActive() )
  99. {
  100. if( mStartVolume > mEndVolume )
  101. mSource->setVolume( mStartVolume );
  102. else
  103. mSource->setVolume( mEndVolume );
  104. }
  105. }
  106. //-----------------------------------------------------------------------------
  107. void SFXFadeModifier::_onStart()
  108. {
  109. mStartVolume = mSource->getVolume();
  110. mCurrentVolume = mStartVolume;
  111. }
  112. //-----------------------------------------------------------------------------
  113. void SFXFadeModifier::_onUpdate()
  114. {
  115. F32 multiplier = ( mSource->getElapsedPlayTimeCurrentCycle() - mStartTime ) / ( mEndTime - mStartTime );
  116. F32 newVolume;
  117. if( mStartVolume > mEndVolume )
  118. newVolume = mStartVolume - ( ( mStartVolume - mEndVolume ) * multiplier );
  119. else
  120. newVolume = mStartVolume + ( ( mEndVolume - mStartVolume ) * multiplier );
  121. if( newVolume != mCurrentVolume )
  122. {
  123. mCurrentVolume = newVolume;
  124. mSource->setVolume( mCurrentVolume );
  125. }
  126. }
  127. //-----------------------------------------------------------------------------
  128. void SFXFadeModifier::_onEnd()
  129. {
  130. mSource->setVolume( mEndVolume );
  131. switch( mOnEnd )
  132. {
  133. case ON_END_Pause:
  134. mSource->pause( 0.f ); // Pause without fade.
  135. break;
  136. case ON_END_Stop:
  137. mSource->stop( 0.f ); // Stop without fade.
  138. break;
  139. case ON_END_Nop: ;
  140. }
  141. }
  142. //=============================================================================
  143. // SFXMarkerModifier.
  144. //=============================================================================
  145. //-----------------------------------------------------------------------------
  146. SFXMarkerModifier::SFXMarkerModifier( SFXSource* source, const String& name, F32 pos, bool removeWhenDone )
  147. : Parent( source, pos, removeWhenDone ),
  148. mMarkerName( name )
  149. {
  150. }
  151. //-----------------------------------------------------------------------------
  152. void SFXMarkerModifier::_onTrigger()
  153. {
  154. Con::executef( mSource, "onMarkerPassed", mMarkerName.c_str() );
  155. }