VCameraShake.cpp 6.8 KB

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