CBattleEffectsController.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * CBattleEffectsController.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CBattleEffectsController.h"
  12. #include "CBattleAnimations.h"
  13. #include "CBattleControlPanel.h"
  14. #include "CBattleInterface.h"
  15. #include "CBattleInterfaceClasses.h"
  16. #include "CBattleStacksController.h"
  17. #include "../CMusicHandler.h"
  18. #include "../CGameInfo.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../gui/CAnimation.h"
  21. #include "../gui/CCanvas.h"
  22. #include "../../CCallback.h"
  23. #include "../../lib/battle/BattleAction.h"
  24. #include "../../lib/NetPacks.h"
  25. #include "../../lib/CStack.h"
  26. #include "../../lib/IGameEventsReceiver.h"
  27. #include "../../lib/CGeneralTextHandler.h"
  28. CBattleEffectsController::CBattleEffectsController(CBattleInterface * owner):
  29. owner(owner)
  30. {}
  31. void CBattleEffectsController::displayEffect(EBattleEffect::EBattleEffect effect, const BattleHex & destTile)
  32. {
  33. displayEffect(effect, soundBase::invalid, destTile);
  34. }
  35. void CBattleEffectsController::displayEffect(EBattleEffect::EBattleEffect effect, uint32_t soundID, const BattleHex & destTile)
  36. {
  37. std::string customAnim = graphics->battleACToDef[effect][0];
  38. owner->stacksController->addNewAnim(new CPointEffectAnimation(owner, soundBase::soundID(soundID), customAnim, destTile));
  39. }
  40. void CBattleEffectsController::displayCustomEffects(const std::vector<CustomEffectInfo> & customEffects)
  41. {
  42. for(const CustomEffectInfo & one : customEffects)
  43. {
  44. const CStack * s = owner->curInt->cb->battleGetStackByID(one.stack, false);
  45. assert(s);
  46. assert(one.effect != 0);
  47. displayEffect(EBattleEffect::EBattleEffect(one.effect), soundBase::soundID(one.sound), s->getPosition());
  48. }
  49. }
  50. void CBattleEffectsController::battleTriggerEffect(const BattleTriggerEffect & bte)
  51. {
  52. const CStack * stack = owner->curInt->cb->battleGetStackByID(bte.stackID);
  53. if(!stack)
  54. {
  55. logGlobal->error("Invalid stack ID %d", bte.stackID);
  56. return;
  57. }
  58. //don't show animation when no HP is regenerated
  59. switch(bte.effect)
  60. {
  61. //TODO: move to bonus type handler
  62. case Bonus::HP_REGENERATION:
  63. displayEffect(EBattleEffect::REGENERATION, soundBase::REGENER, stack->getPosition());
  64. break;
  65. case Bonus::MANA_DRAIN:
  66. displayEffect(EBattleEffect::MANA_DRAIN, soundBase::MANADRAI, stack->getPosition());
  67. break;
  68. case Bonus::POISON:
  69. displayEffect(EBattleEffect::POISON, soundBase::POISON, stack->getPosition());
  70. break;
  71. case Bonus::FEAR:
  72. displayEffect(EBattleEffect::FEAR, soundBase::FEAR, stack->getPosition());
  73. break;
  74. case Bonus::MORALE:
  75. {
  76. std::string hlp = CGI->generaltexth->allTexts[33];
  77. boost::algorithm::replace_first(hlp,"%s",(stack->getName()));
  78. displayEffect(EBattleEffect::GOOD_MORALE, soundBase::GOODMRLE, stack->getPosition());
  79. owner->controlPanel->console->addText(hlp);
  80. break;
  81. }
  82. default:
  83. return;
  84. }
  85. //waitForAnims(); //fixme: freezes game :?
  86. }
  87. void CBattleEffectsController::startAction(const BattleAction* action)
  88. {
  89. const CStack *stack = owner->curInt->cb->battleGetStackByID(action->stackNumber);
  90. switch(action->actionType)
  91. {
  92. case EActionType::WAIT:
  93. owner->controlPanel->console->addText(stack->formatGeneralMessage(136));
  94. break;
  95. case EActionType::BAD_MORALE:
  96. owner->controlPanel->console->addText(stack->formatGeneralMessage(-34));
  97. displayEffect(EBattleEffect::BAD_MORALE, soundBase::BADMRLE, stack->getPosition());
  98. break;
  99. }
  100. //displaying special abilities
  101. auto actionTarget = action->getTarget(owner->curInt->cb.get());
  102. switch(action->actionType)
  103. {
  104. case EActionType::STACK_HEAL:
  105. displayEffect(EBattleEffect::REGENERATION, soundBase::REGENER, actionTarget.at(0).hexValue);
  106. break;
  107. }
  108. }
  109. void CBattleEffectsController::showBattlefieldObjects(std::shared_ptr<CCanvas> canvas, const BattleHex & destTile)
  110. {
  111. for (auto & elem : battleEffects)
  112. {
  113. if (!elem.position.isValid() && destTile != BattleHex::HEX_AFTER_ALL)
  114. continue;
  115. if (elem.position.isValid() && elem.position != destTile)
  116. continue;
  117. int currentFrame = static_cast<int>(floor(elem.currentFrame));
  118. currentFrame %= elem.animation->size();
  119. auto img = elem.animation->getImage(currentFrame);
  120. canvas->draw(img, Point(elem.x, elem.y));
  121. }
  122. }