VCameraShake.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/Torque3D/VCameraShake.h"
  24. #include "T3D/gameBase/gameConnection.h"
  25. #include "core/stream/bitStream.h"
  26. //-----------------------------------------------------------------------------
  27. IMPLEMENT_CO_CLIENTEVENT_V1( VCameraShakeNetEvent );
  28. //-----------------------------------------------------------------------------
  29. // ShakeCamera( 1, 10, "1 0 0", "1 0 0" );
  30. DefineEngineFunction( ShakeCamera, void, (F32 duration, F32 falloff, VectorF amplitude, VectorF frequency), (0,0,VectorF::Zero, VectorF::Zero), "( pDuration, pFalloff, pAmplitude, pFrequency )" )
  31. {
  32. // Shake Camera.
  33. VTorque::startCameraShake( duration, falloff, amplitude, frequency );
  34. }
  35. void VTorque::startCameraShake( const F32 &pDuration, const F32 &pFalloff, const VectorF &pAmplitude, const VectorF &pFrequency )
  36. {
  37. #ifdef VT_EDITOR
  38. // Create FX Event
  39. CameraShake *camShake = new CameraShake();
  40. // Set Duration.
  41. camShake->setDuration( pDuration );
  42. // Set Falloff.
  43. camShake->setFalloff( pFalloff );
  44. // Set Amplitude.
  45. VectorF amp = pAmplitude;
  46. camShake->setAmplitude( amp );
  47. // Set Frequency.
  48. VectorF freq = pFrequency;
  49. camShake->setFrequency( freq );
  50. // Initialise.
  51. camShake->init();
  52. // Add to Manager.
  53. gCamFXMgr.addFX( camShake );
  54. #else
  55. // Fetch Client Group.
  56. SimGroup* clientGroup = Sim::getClientGroup();
  57. for ( SimGroup::iterator itr = clientGroup->begin(); itr != clientGroup->end(); itr++ )
  58. {
  59. NetConnection *connection = static_cast<NetConnection*>( *itr );
  60. if ( connection )
  61. {
  62. // Create Event.
  63. VCameraShakeNetEvent *event = new VCameraShakeNetEvent();
  64. // Setup Event.
  65. event->mEventType |= ( VCameraShakeNetEvent::k_TypeClear | VCameraShakeNetEvent::k_TypeMake );
  66. event->mDuration = pDuration;
  67. event->mFalloff = pFalloff;
  68. event->mAmplitude = pAmplitude;
  69. event->mFrequency = pFrequency;
  70. // Post Event.
  71. connection->postNetEvent( event );
  72. }
  73. }
  74. #endif
  75. }
  76. void VTorque::stopCameraShake( void )
  77. {
  78. #ifdef VT_EDITOR
  79. // Clear Manager.
  80. gCamFXMgr.clear();
  81. #else
  82. // Fetch Client Group.
  83. SimGroup* clientGroup = Sim::getClientGroup();
  84. for ( SimGroup::iterator itr = clientGroup->begin(); itr != clientGroup->end(); itr++ )
  85. {
  86. NetConnection *connection = static_cast<NetConnection*>( *itr );
  87. if ( connection )
  88. {
  89. // Create Event.
  90. VCameraShakeNetEvent *event = new VCameraShakeNetEvent();
  91. // Setup Event.
  92. event->mEventType |= VCameraShakeNetEvent::k_TypeClear;
  93. // Post Event.
  94. connection->postNetEvent( event );
  95. }
  96. }
  97. #endif
  98. }
  99. //-----------------------------------------------------------------------------
  100. VCameraShakeNetEvent::VCameraShakeNetEvent( void ) : mEventType( 0 ),
  101. mDuration( 0.f ),
  102. mFalloff( 10.f ),
  103. mAmplitude( Point3F::Zero ),
  104. mFrequency( Point3F::Zero )
  105. {
  106. // Void.
  107. }
  108. void VCameraShakeNetEvent::write( NetConnection *pConnection, BitStream *pStream )
  109. {
  110. // Void.
  111. }
  112. void VCameraShakeNetEvent::pack( NetConnection *pConnection, BitStream *pStream )
  113. {
  114. // Clear Manager?
  115. pStream->write( mEventType & k_TypeClear );
  116. // Make Event?
  117. if ( pStream->write( mEventType & k_TypeMake ) )
  118. {
  119. // Duration.
  120. pStream->write( mDuration );
  121. // Falloff.
  122. pStream->write( mFalloff );
  123. // Amplitude.
  124. pStream->write( mAmplitude.x );
  125. pStream->write( mAmplitude.y );
  126. pStream->write( mAmplitude.z );
  127. // Frequency.
  128. pStream->write( mFrequency.x );
  129. pStream->write( mFrequency.y );
  130. pStream->write( mFrequency.z );
  131. }
  132. }
  133. void VCameraShakeNetEvent::unpack( NetConnection *pConnection, BitStream *pStream )
  134. {
  135. // Clear Manager?
  136. if ( pStream->readFlag() )
  137. {
  138. // Update State.
  139. mEventType |= k_TypeClear;
  140. }
  141. // Make Event?
  142. if ( pStream->readFlag() )
  143. {
  144. // Update State.
  145. mEventType |= k_TypeMake;
  146. // Duration.
  147. pStream->read( &mDuration );
  148. // Falloff.
  149. pStream->read( &mFalloff );
  150. // Amplitude.
  151. pStream->read( &mAmplitude.x );
  152. pStream->read( &mAmplitude.y );
  153. pStream->read( &mAmplitude.z );
  154. // Frequency.
  155. pStream->read( &mFrequency.x );
  156. pStream->read( &mFrequency.y );
  157. pStream->read( &mFrequency.z );
  158. }
  159. }
  160. void VCameraShakeNetEvent::process( NetConnection *pConnection )
  161. {
  162. if ( mEventType & k_TypeClear )
  163. {
  164. // Clear Manager.
  165. gCamFXMgr.clear();
  166. }
  167. if ( mEventType & k_TypeMake )
  168. {
  169. // Create FX Event
  170. CameraShake *camShake = new CameraShake();
  171. // Set Duration.
  172. camShake->setDuration( mDuration );
  173. // Set Falloff.
  174. camShake->setFalloff( mFalloff );
  175. // Set Amplitude.
  176. camShake->setAmplitude( mAmplitude );
  177. // Set Frequency.
  178. camShake->setFrequency( mFrequency );
  179. // Initialise.
  180. camShake->init();
  181. // Add to Manager.
  182. gCamFXMgr.addFX( camShake );
  183. }
  184. }