CSpellEffectAnimation.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "StdInc.h"
  2. #include "CSpellEffectAnimation.h"
  3. #include "../Graphics.h"
  4. #include "CBattleInterface.h"
  5. #include "../CDefHandler.h"
  6. #include "../CPlayerInterface.h"
  7. #include "../../CCallback.h"
  8. #include "../../lib/BattleState.h"
  9. #include "../SDL_Extensions.h"
  10. //effect animation
  11. bool CSpellEffectAnimation::init()
  12. {
  13. if(!isEarliest(true))
  14. return false;
  15. if(effect == 12) //armageddon
  16. {
  17. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  18. {
  19. CDefHandler * anim;
  20. if(customAnim.size())
  21. anim = CDefHandler::giveDef(customAnim);
  22. else
  23. anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  24. if (Vflip)
  25. {
  26. for (size_t v = 0; v < anim->ourImages.size(); ++v)
  27. {
  28. CSDL_Ext::VflipSurf(anim->ourImages[v].bitmap);
  29. }
  30. }
  31. for(int i=0; i * anim->width < owner->pos.w ; ++i)
  32. {
  33. for(int j=0; j * anim->height < owner->pos.h ; ++j)
  34. {
  35. SBattleEffect be;
  36. be.effectID = ID;
  37. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  38. if (Vflip)
  39. {
  40. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  41. {
  42. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  43. }
  44. }
  45. be.frame = 0;
  46. be.maxFrame = be.anim->ourImages.size();
  47. be.x = i * anim->width + owner->pos.x;
  48. be.y = j * anim->height + owner->pos.y;
  49. owner->battleEffects.push_back(be);
  50. }
  51. }
  52. }
  53. else //there is nothing to play
  54. {
  55. endAnim();
  56. return false;
  57. }
  58. }
  59. else // Effects targeted at a specific creature/hex.
  60. {
  61. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  62. {
  63. const CStack* destStack = owner->curInt->cb->battleGetStackByPos(destTile, false);
  64. Rect &tilePos = owner->bfield[destTile].pos;
  65. SBattleEffect be;
  66. be.effectID = ID;
  67. if(customAnim.size())
  68. be.anim = CDefHandler::giveDef(customAnim);
  69. else
  70. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  71. if (Vflip)
  72. {
  73. for (size_t v = 0; v < be.anim->ourImages.size(); ++v)
  74. {
  75. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  76. }
  77. }
  78. be.frame = 0;
  79. be.maxFrame = be.anim->ourImages.size();
  80. if(effect == 1)
  81. be.maxFrame = 3;
  82. switch (effect)
  83. {
  84. case -1:
  85. be.x = x;
  86. be.y = y;
  87. break;
  88. case 0: // Prayer and Lightning Bolt.
  89. case 1:
  90. // Position effect with it's bottom center touching the bottom center of affected tile(s).
  91. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  92. be.y = tilePos.y + tilePos.h - be.anim->height;
  93. break;
  94. default:
  95. // Position effect with it's center touching the top center of affected tile(s).
  96. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  97. be.y = tilePos.y - be.anim->height/2;
  98. break;
  99. }
  100. // Correction for 2-hex creatures.
  101. if (destStack != NULL && destStack->doubleWide())
  102. be.x += (destStack->attackerOwned ? -1 : 1)*tilePos.w/2;
  103. owner->battleEffects.push_back(be);
  104. }
  105. else //there is nothing to play
  106. {
  107. endAnim();
  108. return false;
  109. }
  110. }
  111. //battleEffects
  112. return true;
  113. }
  114. void CSpellEffectAnimation::nextFrame()
  115. {
  116. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  117. for(std::list<SBattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  118. {
  119. if(it->effectID == ID)
  120. {
  121. ++(it->frame);
  122. if(it->frame == it->maxFrame)
  123. {
  124. endAnim();
  125. break;
  126. }
  127. else
  128. {
  129. it->x += dx;
  130. it->y += dy;
  131. }
  132. }
  133. }
  134. }
  135. void CSpellEffectAnimation::endAnim()
  136. {
  137. CBattleAnimation::endAnim();
  138. std::vector<std::list<SBattleEffect>::iterator> toDel;
  139. for(std::list<SBattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  140. {
  141. if(it->effectID == ID)
  142. {
  143. toDel.push_back(it);
  144. }
  145. }
  146. for(size_t b = 0; b < toDel.size(); ++b)
  147. {
  148. delete toDel[b]->anim;
  149. owner->battleEffects.erase(toDel[b]);
  150. }
  151. delete this;
  152. }
  153. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, ui32 _effect, SHexField _destTile, int _dx, int _dy, bool _Vflip)
  154. :CBattleAnimation(_owner), effect(_effect), destTile(_destTile), customAnim(""), dx(_dx), dy(_dy), Vflip(_Vflip)
  155. {
  156. }
  157. CSpellEffectAnimation::CSpellEffectAnimation(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip)
  158. :CBattleAnimation(_owner), effect(-1), destTile(0), customAnim(_customAnim), x(_x), y(_y), dx(_dx), dy(_dy), Vflip(_Vflip)
  159. {
  160. }