sfxModifier.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. mIsActive( false ),
  54. mEndTime( endTime )
  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. mStartVolume(source->getVolume()),
  92. mCurrentVolume(source->getVolume()),
  93. mEndVolume( endVolume ),
  94. mOnEnd( onEndDo )
  95. {
  96. }
  97. //-----------------------------------------------------------------------------
  98. SFXFadeModifier::~SFXFadeModifier()
  99. {
  100. // If the fade is still ongoing, restore the source's volume.
  101. // For fade-in, set to end volume. For fade-out, set to start volume.
  102. if( isActive() )
  103. {
  104. if( mStartVolume > mEndVolume )
  105. mSource->setVolume( mStartVolume );
  106. else
  107. mSource->setVolume( mEndVolume );
  108. }
  109. }
  110. //-----------------------------------------------------------------------------
  111. void SFXFadeModifier::_onStart()
  112. {
  113. mStartVolume = mSource->getVolume();
  114. mCurrentVolume = mStartVolume;
  115. }
  116. //-----------------------------------------------------------------------------
  117. void SFXFadeModifier::_onUpdate()
  118. {
  119. F32 multiplier = ( mSource->getElapsedPlayTimeCurrentCycle() - mStartTime ) / ( mEndTime - mStartTime );
  120. F32 newVolume;
  121. if( mStartVolume > mEndVolume )
  122. newVolume = mStartVolume - ( ( mStartVolume - mEndVolume ) * multiplier );
  123. else
  124. newVolume = mStartVolume + ( ( mEndVolume - mStartVolume ) * multiplier );
  125. if( newVolume != mCurrentVolume )
  126. {
  127. mCurrentVolume = newVolume;
  128. mSource->setVolume( mCurrentVolume );
  129. }
  130. }
  131. //-----------------------------------------------------------------------------
  132. void SFXFadeModifier::_onEnd()
  133. {
  134. mSource->setVolume( mEndVolume );
  135. switch( mOnEnd )
  136. {
  137. case ON_END_Pause:
  138. mSource->pause( 0.f ); // Pause without fade.
  139. break;
  140. case ON_END_Stop:
  141. mSource->stop( 0.f ); // Stop without fade.
  142. break;
  143. case ON_END_Nop: ;
  144. }
  145. }
  146. //=============================================================================
  147. // SFXMarkerModifier.
  148. //=============================================================================
  149. //-----------------------------------------------------------------------------
  150. SFXMarkerModifier::SFXMarkerModifier( SFXSource* source, const String& name, F32 pos, bool removeWhenDone )
  151. : Parent( source, pos, removeWhenDone ),
  152. mMarkerName( name )
  153. {
  154. }
  155. //-----------------------------------------------------------------------------
  156. void SFXMarkerModifier::_onTrigger()
  157. {
  158. Con::executef( mSource, "onMarkerPassed", mMarkerName.c_str() );
  159. }