CDefenceAnimation.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "StdInc.h"
  2. #include "CDefenceAnimation.h"
  3. #include "CBattleInterface.h"
  4. #include "../CGameInfo.h"
  5. #include "CCreatureAnimation.h"
  6. #include "../CPlayerInterface.h"
  7. #include "../CMusicHandler.h"
  8. #include "../../lib/BattleState.h"
  9. #include "CReverseAnimation.h"
  10. #include "CAttackAnimation.h"
  11. #include "CShootingAnimation.h"
  12. bool CDefenceAnimation::init()
  13. {
  14. //checking initial conditions
  15. //if(owner->creAnims[stackID]->getType() != 2)
  16. //{
  17. // return false;
  18. //}
  19. if(attacker == NULL && owner->battleEffects.size() > 0)
  20. return false;
  21. ui32 lowestMoveID = owner->animIDhelper + 5;
  22. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  23. {
  24. CDefenceAnimation * defAnim = dynamic_cast<CDefenceAnimation *>(it->first);
  25. if(defAnim && defAnim->stack->ID != stack->ID)
  26. continue;
  27. CAttackAnimation * attAnim = dynamic_cast<CAttackAnimation *>(it->first);
  28. if(attAnim && attAnim->stack->ID != stack->ID)
  29. continue;
  30. if(attacker != NULL)
  31. {
  32. int attackerAnimType = owner->creAnims[attacker->ID]->getType();
  33. if( attackerAnimType == 11 && attackerAnimType == 12 && attackerAnimType == 13 && owner->creAnims[attacker->ID]->getFrame() < attacker->getCreature()->attackClimaxFrame )
  34. return false;
  35. }
  36. CReverseAnimation * animAsRev = dynamic_cast<CReverseAnimation *>(it->first);
  37. if(animAsRev && animAsRev->priority)
  38. return false;
  39. if(it->first)
  40. vstd::amin(lowestMoveID, it->first->ID);
  41. }
  42. if(ID > lowestMoveID)
  43. return false;
  44. //reverse unit if necessary
  45. if(attacker && isToReverse(stack->position, attacker->position, owner->creDir[stack->ID], attacker->doubleWide(), owner->creDir[attacker->ID]))
  46. {
  47. owner->addNewAnim(new CReverseAnimation(owner, stack, stack->position, true));
  48. return false;
  49. }
  50. //unit reversed
  51. if(byShooting) //delay hit animation
  52. {
  53. for(std::list<SProjectileInfo>::const_iterator it = owner->projectiles.begin(); it != owner->projectiles.end(); ++it)
  54. {
  55. if(it->creID == attacker->getCreature()->idNumber)
  56. {
  57. return false;
  58. }
  59. }
  60. }
  61. //initializing
  62. if(killed)
  63. {
  64. CCS->soundh->playSound(battle_sound(stack->getCreature(), killed));
  65. myAnim()->setType(CCreatureAnim::DEATH); //death
  66. }
  67. else
  68. {
  69. // TODO: this block doesn't seems correct if the unit is defending.
  70. CCS->soundh->playSound(battle_sound(stack->getCreature(), wince));
  71. myAnim()->setType(CCreatureAnim::HITTED); //getting hit
  72. }
  73. return true; //initialized successfuly
  74. }
  75. void CDefenceAnimation::nextFrame()
  76. {
  77. if(!killed && myAnim()->getType() != CCreatureAnim::HITTED)
  78. {
  79. myAnim()->setType(CCreatureAnim::HITTED);
  80. }
  81. if(!myAnim()->onLastFrameInGroup())
  82. {
  83. if( myAnim()->getType() == CCreatureAnim::DEATH && (owner->animCount+1)%(4/owner->curInt->sysOpts.animSpeed)==0
  84. && !myAnim()->onLastFrameInGroup() )
  85. {
  86. myAnim()->incrementFrame();
  87. }
  88. }
  89. else
  90. {
  91. endAnim();
  92. }
  93. }
  94. void CDefenceAnimation::endAnim()
  95. {
  96. //restoring animType
  97. if(myAnim()->getType() == CCreatureAnim::HITTED)
  98. myAnim()->setType(CCreatureAnim::HOLDING);
  99. //printing info to console
  100. //if(attacker!=NULL)
  101. // owner->printConsoleAttacked(stack, dmg, amountKilled, attacker);
  102. //const CStack * attacker = owner->curInt->cb->battleGetStackByID(IDby, false);
  103. //const CStack * attacked = owner->curInt->cb->battleGetStackByID(stackID, false);
  104. CBattleAnimation::endAnim();
  105. delete this;
  106. }
  107. CDefenceAnimation::CDefenceAnimation(SStackAttackedInfo _attackedInfo, CBattleInterface * _owner)
  108. : CBattleStackAnimation(_owner, _attackedInfo.defender), dmg(_attackedInfo.dmg),
  109. amountKilled(_attackedInfo.amountKilled), attacker(_attackedInfo.attacker), byShooting(_attackedInfo.byShooting),
  110. killed(_attackedInfo.killed)
  111. {
  112. }