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