afxPhrase.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  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 "afx/arcaneFX.h"
  25. #include "afx/afxEffectVector.h"
  26. #include "afx/afxPhrase.h"
  27. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  28. // afxPhrase
  29. void
  30. afxPhrase::init_fx(S32 group_index)
  31. {
  32. mFX->ev_init(mInit_chor, *mInit_fx_list, mOn_server, mWill_stop, mInit_time_factor, mInit_dur, group_index);
  33. }
  34. //~~~~~~~~~~~~~~~~~~~~//
  35. afxPhrase::afxPhrase(bool on_server, bool will_stop)
  36. {
  37. mOn_server = on_server;
  38. mWill_stop = will_stop;
  39. mInit_fx_list = NULL;
  40. mInit_dur = 0.0f;
  41. mInit_chor = NULL;
  42. mInit_time_factor = 1.0f;
  43. mFX = new afxEffectVector;
  44. mFX2 = NULL;
  45. mStartTime = 0;
  46. mDur = 0;
  47. mNum_loops = 1;
  48. mLoop_cnt = 1;
  49. mExtra_time = 0.0f;
  50. mExtra_stoptime = 0.0f;
  51. }
  52. afxPhrase::~afxPhrase()
  53. {
  54. delete mFX;
  55. delete mFX2;
  56. };
  57. void
  58. afxPhrase::init(afxEffectList& fx_list, F32 dur, afxChoreographer* chor, F32 time_factor,
  59. S32 n_loops, S32 group_index, F32 extra_time)
  60. {
  61. mInit_fx_list = &fx_list;
  62. mInit_dur = dur;
  63. mInit_chor = chor;
  64. mInit_time_factor = time_factor;
  65. mNum_loops = n_loops;
  66. mExtra_time = extra_time;
  67. mDur = (mInit_dur < 0) ? mInit_dur : mInit_dur*mInit_time_factor;
  68. init_fx(group_index);
  69. }
  70. void
  71. afxPhrase::start(F32 startstamp, F32 timestamp)
  72. {
  73. mStartTime = startstamp;
  74. F32 loopstart = timestamp - startstamp;
  75. if (mDur > 0 && loopstart > mDur)
  76. {
  77. mLoop_cnt += (S32) (loopstart/ mDur);
  78. loopstart = mFmod(loopstart, mDur);
  79. }
  80. if (!mFX->empty())
  81. mFX->start(loopstart);
  82. }
  83. void
  84. afxPhrase::update(F32 dt, F32 timestamp)
  85. {
  86. if (mFX->isActive())
  87. mFX->update(dt);
  88. if (mFX2 && mFX2->isActive())
  89. mFX2->update(dt);
  90. if (mExtra_stoptime > 0 && timestamp > mExtra_stoptime)
  91. {
  92. stop(timestamp);
  93. }
  94. }
  95. void
  96. afxPhrase::stop(F32 timestamp)
  97. {
  98. if (mExtra_time > 0 && !(mExtra_stoptime > 0))
  99. {
  100. mExtra_stoptime = timestamp + mExtra_time;
  101. return;
  102. }
  103. if (mFX->isActive())
  104. mFX->stop();
  105. if (mFX2 && mFX2->isActive())
  106. mFX2->stop();
  107. }
  108. bool
  109. afxPhrase::expired(F32 timestamp)
  110. {
  111. if (mDur < 0)
  112. return false;
  113. return ((timestamp - mStartTime) > mLoop_cnt*mDur);
  114. }
  115. F32
  116. afxPhrase::elapsed(F32 timestamp)
  117. {
  118. return (timestamp - mStartTime);
  119. }
  120. bool
  121. afxPhrase::recycle(F32 timestamp)
  122. {
  123. if (mNum_loops < 0 || mLoop_cnt < mNum_loops)
  124. {
  125. if (mFX2)
  126. delete mFX2;
  127. mFX2 = mFX;
  128. mFX = new afxEffectVector;
  129. init_fx();
  130. if (mFX2 && !mFX2->empty())
  131. mFX2->stop();
  132. if (!mFX->empty())
  133. mFX->start(0.0F);
  134. mLoop_cnt++;
  135. return true;
  136. }
  137. return false;
  138. }
  139. void
  140. afxPhrase::interrupt(F32 timestamp)
  141. {
  142. if (mFX->isActive())
  143. mFX->interrupt();
  144. if (mFX2 && mFX2->isActive())
  145. mFX2->interrupt();
  146. }
  147. F32 afxPhrase::calcDoneTime()
  148. {
  149. return mStartTime + mFX->getTotalDur();
  150. }
  151. F32 afxPhrase::calcAfterLife()
  152. {
  153. return mFX->getAfterLife();
  154. }
  155. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//