CSpellEffectAnimation.cpp 4.6 KB

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