CMovementAnimation.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "StdInc.h"
  2. #include "CMovementAnimation.h"
  3. #include "CBattleInterface.h"
  4. #include "CCreatureAnimation.h"
  5. #include "../../lib/BattleState.h"
  6. #include "../CGameInfo.h"
  7. #include "../CMusicHandler.h"
  8. #include "CReverseAnimation.h"
  9. #include "CMovementEndAnimation.h"
  10. #include "CClickableHex.h"
  11. bool CMovementAnimation::init()
  12. {
  13. if( !isEarliest(false) )
  14. return false;
  15. //a few useful variables
  16. steps = static_cast<int>(myAnim()->framesInGroup(CCreatureAnim::MOVING) * owner->getAnimSpeedMultiplier() - 1);
  17. if(steps == 0) //this creature seems to have no move animation so we can end it immediately
  18. {
  19. endAnim();
  20. return false;
  21. }
  22. whichStep = 0;
  23. int hexWbase = 44, hexHbase = 42;
  24. const CStack * movedStack = stack;
  25. if(!movedStack || myAnim()->getType() == 5)
  26. {
  27. endAnim();
  28. return false;
  29. }
  30. //bool twoTiles = movedStack->doubleWide();
  31. SPoint begPosition = CClickableHex::getXYUnitAnim(curStackPos, movedStack->attackerOwned, movedStack, owner);
  32. SPoint endPosition = CClickableHex::getXYUnitAnim(nextHex, movedStack->attackerOwned, movedStack, owner);
  33. int mutPos = SBattleHex::mutualPosition(curStackPos, nextHex);
  34. //reverse unit if necessary
  35. if((begPosition.x > endPosition.x) && owner->creDir[stack->ID] == true)
  36. {
  37. owner->addNewAnim(new CReverseAnimation(owner, stack, curStackPos, true));
  38. return false;
  39. }
  40. else if ((begPosition.x < endPosition.x) && owner->creDir[stack->ID] == false)
  41. {
  42. owner->addNewAnim(new CReverseAnimation(owner, stack, curStackPos, true));
  43. return false;
  44. }
  45. if(myAnim()->getType() != CCreatureAnim::MOVING)
  46. {
  47. myAnim()->setType(CCreatureAnim::MOVING);
  48. }
  49. //unit reversed
  50. // if(owner->moveSh <= 0)
  51. // owner->moveSh = CCS->soundh->playSound(battle_sound(movedStack->getCreature(), move), -1);
  52. //step shift calculation
  53. posX = myAnim()->pos.x, posY = myAnim()->pos.y; // for precise calculations ;]
  54. if(mutPos == -1 && movedStack->hasBonusOfType(Bonus::FLYING))
  55. {
  56. steps *= distance;
  57. steps /= 2; //to make animation faster
  58. stepX = (endPosition.x - begPosition.x) / static_cast<double>(steps);
  59. stepY = (endPosition.y - begPosition.y) / static_cast<double>(steps);
  60. }
  61. else
  62. {
  63. switch(mutPos)
  64. {
  65. case 0:
  66. stepX = -1.0 * (hexWbase / (2.0 * steps));
  67. stepY = -1.0 * (hexHbase / (static_cast<double>(steps)));
  68. break;
  69. case 1:
  70. stepX = hexWbase / (2.0 * steps);
  71. stepY = -1.0 * hexHbase / (static_cast<double>(steps));
  72. break;
  73. case 2:
  74. stepX = hexWbase / static_cast<double>(steps);
  75. stepY = 0.0;
  76. break;
  77. case 3:
  78. stepX = hexWbase / (2.0 * steps);
  79. stepY = hexHbase / static_cast<double>(steps);
  80. break;
  81. case 4:
  82. stepX = -1.0 * hexWbase / (2.0 * steps);
  83. stepY = hexHbase / static_cast<double>(steps);
  84. break;
  85. case 5:
  86. stepX = -1.0 * hexWbase / static_cast<double>(steps);
  87. stepY = 0.0;
  88. break;
  89. }
  90. }
  91. //step shifts calculated
  92. return true;
  93. }
  94. void CMovementAnimation::nextFrame()
  95. {
  96. //moving instructions
  97. posX += stepX;
  98. myAnim()->pos.x = static_cast<Sint16>(posX);
  99. posY += stepY;
  100. myAnim()->pos.y = static_cast<Sint16>(posY);
  101. // Increments step count and check if we are finished with current animation
  102. ++whichStep;
  103. if(whichStep == steps)
  104. {
  105. // Sets the position of the creature animation sprites
  106. SPoint coords = CClickableHex::getXYUnitAnim(nextHex, owner->creDir[stack->ID], stack, owner);
  107. myAnim()->pos = coords;
  108. // true if creature haven't reached the final destination hex
  109. if ((nextPos + 1) < destTiles.size())
  110. {
  111. // update the next hex field which has to be reached by the stack
  112. nextPos++;
  113. curStackPos = nextHex;
  114. nextHex = destTiles[nextPos];
  115. // update position of double wide creatures
  116. bool twoTiles = stack->doubleWide();
  117. if(twoTiles && bool(stack->attackerOwned) && (owner->creDir[stack->ID] != bool(stack->attackerOwned) )) //big attacker creature is reversed
  118. myAnim()->pos.x -= 44;
  119. else if(twoTiles && (! bool(stack->attackerOwned) ) && (owner->creDir[stack->ID] != bool(stack->attackerOwned) )) //big defender creature is reversed
  120. myAnim()->pos.x += 44;
  121. // re-init animation
  122. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  123. {
  124. if (it->first == this)
  125. {
  126. it->second = false;
  127. break;
  128. }
  129. }
  130. }
  131. else
  132. endAnim();
  133. }
  134. }
  135. void CMovementAnimation::endAnim()
  136. {
  137. const CStack * movedStack = stack;
  138. CBattleAnimation::endAnim();
  139. if(movedStack)
  140. owner->addNewAnim(new CMovementEndAnimation(owner, stack, nextHex));
  141. if(owner->moveSh >= 0)
  142. {
  143. CCS->soundh->stopSound(owner->moveSh);
  144. owner->moveSh = -1;
  145. }
  146. delete this;
  147. }
  148. CMovementAnimation::CMovementAnimation(CBattleInterface *_owner, const CStack *_stack, std::vector<SBattleHex> _destTiles, int _distance)
  149. : CBattleStackAnimation(_owner, _stack), destTiles(_destTiles), nextPos(0), distance(_distance), stepX(0.0), stepY(0.0)
  150. {
  151. curStackPos = stack->position;
  152. nextHex = destTiles.front();
  153. }