CBattleHero.cpp 3.7 KB

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