CBattleHero.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "StdInc.h"
  2. #include "CBattleHero.h"
  3. #include "CBattleInterface.h"
  4. #include "../CGameInfo.h"
  5. #include "../CDefHandler.h"
  6. #include "../CCursorHandler.h"
  7. #include "../CPlayerInterface.h"
  8. #include "../../CCallback.h"
  9. #include "../SDL_Extensions.h"
  10. #include "../CSpellWindow.h"
  11. #include "../Graphics.h"
  12. #include "../CConfigHandler.h"
  13. void CBattleHero::show(SDL_Surface *to)
  14. {
  15. //animation of flag
  16. if(flip)
  17. {
  18. SDL_Rect temp_rect = genRect(
  19. flag->ourImages[flagAnim].bitmap->h,
  20. flag->ourImages[flagAnim].bitmap->w,
  21. pos.x + 61,
  22. pos.y + 39);
  23. CSDL_Ext::blit8bppAlphaTo24bpp(
  24. flag->ourImages[flagAnim].bitmap,
  25. NULL,
  26. screen,
  27. &temp_rect);
  28. }
  29. else
  30. {
  31. SDL_Rect temp_rect = genRect(
  32. flag->ourImages[flagAnim].bitmap->h,
  33. flag->ourImages[flagAnim].bitmap->w,
  34. pos.x + 72,
  35. pos.y + 39);
  36. CSDL_Ext::blit8bppAlphaTo24bpp(
  37. flag->ourImages[flagAnim].bitmap,
  38. NULL,
  39. screen,
  40. &temp_rect);
  41. }
  42. ++flagAnimCount;
  43. if(flagAnimCount%4==0)
  44. {
  45. ++flagAnim;
  46. flagAnim %= flag->ourImages.size();
  47. }
  48. //animation of hero
  49. int tick=-1;
  50. for(size_t i = 0; i < dh->ourImages.size(); ++i)
  51. {
  52. if(dh->ourImages[i].groupNumber==phase)
  53. ++tick;
  54. if(tick==image)
  55. {
  56. SDL_Rect posb = pos;
  57. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  58. if(phase != 4 || nextPhase != -1 || image < 4)
  59. {
  60. if(flagAnimCount%2==0)
  61. {
  62. ++image;
  63. }
  64. if(dh->ourImages[(i+1)%dh->ourImages.size()].groupNumber!=phase) //back to appropriate frame
  65. {
  66. image = 0;
  67. }
  68. }
  69. if(phase == 4 && nextPhase != -1 && image == 7)
  70. {
  71. phase = nextPhase;
  72. nextPhase = -1;
  73. image = 0;
  74. }
  75. break;
  76. }
  77. }
  78. }
  79. void CBattleHero::activate()
  80. {
  81. activateLClick();
  82. }
  83. void CBattleHero::deactivate()
  84. {
  85. deactivateLClick();
  86. }
  87. void CBattleHero::setPhase(int newPhase)
  88. {
  89. if(phase != 4)
  90. {
  91. phase = newPhase;
  92. image = 0;
  93. }
  94. else
  95. {
  96. nextPhase = newPhase;
  97. }
  98. }
  99. void CBattleHero::clickLeft(tribool down, bool previousState)
  100. {
  101. if(myOwner->spellDestSelectMode) //we are casting a spell
  102. return;
  103. if(!down && myHero != NULL && myOwner->myTurn && myOwner->curInt->cb->battleCanCastSpell()) //check conditions
  104. {
  105. for(int it=0; it<GameConstants::BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  106. {
  107. if(myOwner->bfield[it].hovered && myOwner->bfield[it].strictHovered)
  108. return;
  109. }
  110. CCS->curh->changeGraphic(0,0);
  111. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), myHero, myOwner->curInt);
  112. GH.pushInt(spellWindow);
  113. }
  114. }
  115. CBattleHero::CBattleHero(const std::string & defName, int phaseG, int imageG, bool flipG, ui8 player, const CGHeroInstance * hero, const CBattleInterface * owner): flip(flipG), myHero(hero), myOwner(owner), phase(phaseG), nextPhase(-1), image(imageG), flagAnim(0), flagAnimCount(0)
  116. {
  117. dh = CDefHandler::giveDef( defName );
  118. for(size_t i = 0; i < dh->ourImages.size(); ++i) //transforming images
  119. {
  120. if(flip)
  121. {
  122. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  123. SDL_FreeSurface(dh->ourImages[i].bitmap);
  124. dh->ourImages[i].bitmap = hlp;
  125. }
  126. CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  127. }
  128. if(flip)
  129. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  130. else
  131. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  132. //coloring flag and adding transparency
  133. for(size_t i = 0; i < flag->ourImages.size(); ++i)
  134. {
  135. CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  136. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  137. }
  138. }
  139. CBattleHero::~CBattleHero()
  140. {
  141. delete dh;
  142. delete flag;
  143. }