BattleEffectsController.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * BattleEffectsController.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 "BattleEffectsController.h"
  12. #include "BattleAnimationClasses.h"
  13. #include "BattleControlPanel.h"
  14. #include "BattleInterface.h"
  15. #include "BattleInterfaceClasses.h"
  16. #include "BattleFieldController.h"
  17. #include "BattleStacksController.h"
  18. #include "BattleRenderer.h"
  19. #include "../CMusicHandler.h"
  20. #include "../CGameInfo.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../gui/CAnimation.h"
  23. #include "../gui/Canvas.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/battle/BattleAction.h"
  26. #include "../../lib/NetPacks.h"
  27. #include "../../lib/CStack.h"
  28. #include "../../lib/IGameEventsReceiver.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. BattleEffectsController::BattleEffectsController(BattleInterface & owner):
  31. owner(owner)
  32. {}
  33. void BattleEffectsController::displayEffect(EBattleEffect::EBattleEffect effect, const BattleHex & destTile)
  34. {
  35. displayEffect(effect, soundBase::invalid, destTile);
  36. }
  37. void BattleEffectsController::displayEffect(EBattleEffect::EBattleEffect effect, uint32_t soundID, const BattleHex & destTile)
  38. {
  39. std::string customAnim = graphics->battleACToDef[effect][0];
  40. owner.stacksController->addNewAnim(new CPointEffectAnimation(owner, soundBase::soundID(soundID), customAnim, destTile));
  41. }
  42. void BattleEffectsController::displayCustomEffects(const std::vector<CustomEffectInfo> & customEffects)
  43. {
  44. for(const CustomEffectInfo & one : customEffects)
  45. {
  46. const CStack * s = owner.curInt->cb->battleGetStackByID(one.stack, false);
  47. assert(s);
  48. assert(one.effect != 0);
  49. displayEffect(EBattleEffect::EBattleEffect(one.effect), soundBase::soundID(one.sound), s->getPosition());
  50. }
  51. }
  52. void BattleEffectsController::battleTriggerEffect(const BattleTriggerEffect & bte)
  53. {
  54. const CStack * stack = owner.curInt->cb->battleGetStackByID(bte.stackID);
  55. if(!stack)
  56. {
  57. logGlobal->error("Invalid stack ID %d", bte.stackID);
  58. return;
  59. }
  60. //don't show animation when no HP is regenerated
  61. switch(bte.effect)
  62. {
  63. //TODO: move to bonus type handler
  64. case Bonus::HP_REGENERATION:
  65. displayEffect(EBattleEffect::REGENERATION, soundBase::REGENER, stack->getPosition());
  66. break;
  67. case Bonus::MANA_DRAIN:
  68. displayEffect(EBattleEffect::MANA_DRAIN, soundBase::MANADRAI, stack->getPosition());
  69. break;
  70. case Bonus::POISON:
  71. displayEffect(EBattleEffect::POISON, soundBase::POISON, stack->getPosition());
  72. break;
  73. case Bonus::FEAR:
  74. displayEffect(EBattleEffect::FEAR, soundBase::FEAR, stack->getPosition());
  75. break;
  76. case Bonus::MORALE:
  77. {
  78. std::string hlp = CGI->generaltexth->allTexts[33];
  79. boost::algorithm::replace_first(hlp,"%s",(stack->getName()));
  80. displayEffect(EBattleEffect::GOOD_MORALE, soundBase::GOODMRLE, stack->getPosition());
  81. owner.controlPanel->console->addText(hlp);
  82. break;
  83. }
  84. default:
  85. return;
  86. }
  87. //waitForAnims(); //fixme: freezes game :?
  88. }
  89. void BattleEffectsController::startAction(const BattleAction* action)
  90. {
  91. const CStack *stack = owner.curInt->cb->battleGetStackByID(action->stackNumber);
  92. switch(action->actionType)
  93. {
  94. case EActionType::WAIT:
  95. owner.controlPanel->console->addText(stack->formatGeneralMessage(136));
  96. break;
  97. case EActionType::BAD_MORALE:
  98. owner.controlPanel->console->addText(stack->formatGeneralMessage(-34));
  99. displayEffect(EBattleEffect::BAD_MORALE, soundBase::BADMRLE, stack->getPosition());
  100. break;
  101. }
  102. //displaying special abilities
  103. auto actionTarget = action->getTarget(owner.curInt->cb.get());
  104. switch(action->actionType)
  105. {
  106. case EActionType::STACK_HEAL:
  107. displayEffect(EBattleEffect::REGENERATION, soundBase::REGENER, actionTarget.at(0).hexValue);
  108. break;
  109. }
  110. }
  111. void BattleEffectsController::collectRenderableObjects(BattleRenderer & renderer)
  112. {
  113. for (auto & elem : battleEffects)
  114. {
  115. renderer.insert( EBattleFieldLayer::EFFECTS, elem.position, [&elem](BattleRenderer::RendererPtr canvas)
  116. {
  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. }
  123. }