VSoundEffectEvent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  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. #include "Verve/Core/VGroup.h"
  24. #include "Verve/Extension/SoundEffect/VSoundEffectEvent.h"
  25. #include "Verve/Extension/SoundEffect/VSoundEffectTrack.h"
  26. #include "console/consoleTypes.h"
  27. //-----------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT( VSoundEffectEvent );
  29. //-----------------------------------------------------------------------------
  30. VSoundEffectEvent::VSoundEffectEvent( void ) :
  31. mSoundEffect( NULL )
  32. {
  33. setLabel( "SoundEvent" );
  34. }
  35. void VSoundEffectEvent::initPersistFields()
  36. {
  37. docsURL;
  38. Parent::initPersistFields();
  39. addProtectedField( "SoundEffect", TYPEID<VTorque::SoundEffectType>(), Offset( mSoundEffect, VSoundEffectEvent ), &setSoundData, &defaultProtectedGetFn, "" );
  40. }
  41. //-----------------------------------------------------------------------------
  42. //
  43. // Callback Methods.
  44. //
  45. //-----------------------------------------------------------------------------
  46. //-----------------------------------------------------------------------------
  47. //
  48. // VSoundEffectEvent::onTrigger( pTime, pDelta );
  49. //
  50. // Play the target sound effect. If this track belongs to a SceneObjectGroup,
  51. // then the sound will play with the reference object's transform. If this is
  52. // not the case, then a 2D sound will be played.
  53. //
  54. //-----------------------------------------------------------------------------
  55. void VSoundEffectEvent::onTrigger( const S32 &pTime, const S32 &pDelta )
  56. {
  57. Parent::onTrigger( pTime, pDelta );
  58. // Fetch Track.
  59. VSoundEffectTrack *track;
  60. if ( !getTrack( track ) )
  61. {
  62. return;
  63. }
  64. // Position & Pitch.
  65. U32 position = mAbs( ( pTime + pDelta ) - getStartTime() );
  66. F32 pitch = mFabs( getControllerTimeScale() );
  67. if ( position < SFXStartBuffer )
  68. {
  69. // Zero.
  70. position = 0;
  71. }
  72. VSceneObjectGroup *group;
  73. if ( getGroup( group ) )
  74. {
  75. // Play Sound With Reference.
  76. track->mSource = VTorque::playSound( mSoundEffect, group->getSceneObject(), position, pitch );
  77. }
  78. else
  79. {
  80. // Play Sound.
  81. track->mSource = VTorque::playSound( mSoundEffect, position, pitch );
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. //
  86. // Property Methods.
  87. //
  88. //-----------------------------------------------------------------------------
  89. //-----------------------------------------------------------------------------
  90. //
  91. // VSoundEffectEvent::setDuration( pDuration );
  92. //
  93. // This event's duration is always set to the sound object's duration.
  94. //
  95. //-----------------------------------------------------------------------------
  96. void VSoundEffectEvent::setDuration( const S32 &pDuration )
  97. {
  98. // Clear Duration.
  99. mDuration = VTorque::getSoundDuration( mSoundEffect );
  100. }
  101. //-----------------------------------------------------------------------------
  102. //
  103. // Static Field Methods.
  104. //
  105. //-----------------------------------------------------------------------------
  106. bool VSoundEffectEvent::setSoundData( void *pObject, const char *pArray, const char *pData )
  107. {
  108. // Fetch Event.
  109. VSoundEffectEvent *event = static_cast<VSoundEffectEvent*>( pObject );
  110. // Use Object.
  111. event->mSoundEffect = dynamic_cast<VTorque::SoundEffectType*>( Sim::findObject( pData ) );
  112. // Set Duration.
  113. event->setDuration( 0 );
  114. return false;
  115. }