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. fx->ev_init(init_chor, *init_fx_list, on_server, will_stop, init_time_factor, init_dur, group_index);
  33. }
  34. //~~~~~~~~~~~~~~~~~~~~//
  35. afxPhrase::afxPhrase(bool on_server, bool will_stop)
  36. {
  37. this->on_server = on_server;
  38. this->will_stop = will_stop;
  39. init_fx_list = NULL;
  40. init_dur = 0.0f;
  41. init_chor = NULL;
  42. init_time_factor = 1.0f;
  43. fx = new afxEffectVector;
  44. fx2 = NULL;
  45. starttime = 0;
  46. dur = 0;
  47. n_loops = 1;
  48. loop_cnt = 1;
  49. extra_time = 0.0f;
  50. extra_stoptime = 0.0f;
  51. }
  52. afxPhrase::~afxPhrase()
  53. {
  54. delete fx;
  55. delete fx2;
  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. init_fx_list = &fx_list;
  62. init_dur = dur;
  63. init_chor = chor;
  64. init_time_factor = time_factor;
  65. this->n_loops = n_loops;
  66. this->extra_time = extra_time;
  67. this->dur = (init_dur < 0) ? init_dur : init_dur*init_time_factor;
  68. init_fx(group_index);
  69. }
  70. void
  71. afxPhrase::start(F32 startstamp, F32 timestamp)
  72. {
  73. starttime = startstamp;
  74. F32 loopstart = timestamp - startstamp;
  75. if (dur > 0 && loopstart > dur)
  76. {
  77. loop_cnt += (S32) (loopstart/dur);
  78. loopstart = mFmod(loopstart, dur);
  79. }
  80. if (!fx->empty())
  81. fx->start(loopstart);
  82. }
  83. void
  84. afxPhrase::update(F32 dt, F32 timestamp)
  85. {
  86. if (fx->isActive())
  87. fx->update(dt);
  88. if (fx2 && fx2->isActive())
  89. fx2->update(dt);
  90. if (extra_stoptime > 0 && timestamp > extra_stoptime)
  91. {
  92. stop(timestamp);
  93. }
  94. }
  95. void
  96. afxPhrase::stop(F32 timestamp)
  97. {
  98. if (extra_time > 0 && !(extra_stoptime > 0))
  99. {
  100. extra_stoptime = timestamp + extra_time;
  101. return;
  102. }
  103. if (fx->isActive())
  104. fx->stop();
  105. if (fx2 && fx2->isActive())
  106. fx2->stop();
  107. }
  108. bool
  109. afxPhrase::expired(F32 timestamp)
  110. {
  111. if (dur < 0)
  112. return false;
  113. return ((timestamp - starttime) > loop_cnt*dur);
  114. }
  115. F32
  116. afxPhrase::elapsed(F32 timestamp)
  117. {
  118. return (timestamp - starttime);
  119. }
  120. bool
  121. afxPhrase::recycle(F32 timestamp)
  122. {
  123. if (n_loops < 0 || loop_cnt < n_loops)
  124. {
  125. if (fx2)
  126. delete fx2;
  127. fx2 = fx;
  128. fx = new afxEffectVector;
  129. init_fx();
  130. if (fx2 && !fx2->empty())
  131. fx2->stop();
  132. if (!fx->empty())
  133. fx->start(0.0F);
  134. loop_cnt++;
  135. return true;
  136. }
  137. return false;
  138. }
  139. void
  140. afxPhrase::interrupt(F32 timestamp)
  141. {
  142. if (fx->isActive())
  143. fx->interrupt();
  144. if (fx2 && fx2->isActive())
  145. fx2->interrupt();
  146. }
  147. F32 afxPhrase::calcDoneTime()
  148. {
  149. return starttime + fx->getTotalDur();
  150. }
  151. F32 afxPhrase::calcAfterLife()
  152. {
  153. return fx->getAfterLife();
  154. }
  155. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//