CBattleEffectsController.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. std::string customAnim = graphics->battleACToDef[effect][0];
  34. owner->stacksController->addNewAnim(new CEffectAnimation(owner, customAnim, destTile));//FIXME: check positioning for double-hex creatures
  35. }
  36. void CBattleEffectsController::displayEffect(EBattleEffect::EBattleEffect effect, uint32_t soundID, const BattleHex & destTile)
  37. {
  38. displayEffect(effect, destTile);
  39. if(soundBase::soundID(soundID) != soundBase::invalid )
  40. CCS->soundh->playSound(soundBase::soundID(soundID));
  41. }
  42. void CBattleEffectsController::displayCustomEffects(const std::vector<CustomEffectInfo> & customEffects)
  43. {
  44. for(const CustomEffectInfo & one : customEffects)
  45. {
  46. if(one.sound != 0)
  47. CCS->soundh->playSound(soundBase::soundID(one.sound));
  48. const CStack * s = owner->curInt->cb->battleGetStackByID(one.stack, false);
  49. if(s && one.effect != 0)
  50. displayEffect(EBattleEffect::EBattleEffect(one.effect), s->getPosition());
  51. }
  52. }
  53. void CBattleEffectsController::battleTriggerEffect(const BattleTriggerEffect & bte)
  54. {
  55. const CStack * stack = owner->curInt->cb->battleGetStackByID(bte.stackID);
  56. if(!stack)
  57. {
  58. logGlobal->error("Invalid stack ID %d", bte.stackID);
  59. return;
  60. }
  61. //don't show animation when no HP is regenerated
  62. switch(bte.effect)
  63. {
  64. //TODO: move to bonus type handler
  65. case Bonus::HP_REGENERATION:
  66. displayEffect(EBattleEffect::REGENERATION, soundBase::REGENER, stack->getPosition());
  67. break;
  68. case Bonus::MANA_DRAIN:
  69. displayEffect(EBattleEffect::MANA_DRAIN, soundBase::MANADRAI, stack->getPosition());
  70. break;
  71. case Bonus::POISON:
  72. displayEffect(EBattleEffect::POISON, soundBase::POISON, stack->getPosition());
  73. break;
  74. case Bonus::FEAR:
  75. displayEffect(EBattleEffect::FEAR, soundBase::FEAR, stack->getPosition());
  76. break;
  77. case Bonus::MORALE:
  78. {
  79. std::string hlp = CGI->generaltexth->allTexts[33];
  80. boost::algorithm::replace_first(hlp,"%s",(stack->getName()));
  81. displayEffect(EBattleEffect::GOOD_MORALE, soundBase::GOODMRLE, stack->getPosition());
  82. owner->controlPanel->console->addText(hlp);
  83. break;
  84. }
  85. default:
  86. return;
  87. }
  88. //waitForAnims(); //fixme: freezes game :?
  89. }
  90. void CBattleEffectsController::startAction(const BattleAction* action)
  91. {
  92. const CStack *stack = owner->curInt->cb->battleGetStackByID(action->stackNumber);
  93. switch(action->actionType)
  94. {
  95. case EActionType::WAIT:
  96. owner->controlPanel->console->addText(stack->formatGeneralMessage(136));
  97. break;
  98. case EActionType::BAD_MORALE:
  99. owner->controlPanel->console->addText(stack->formatGeneralMessage(-34));
  100. displayEffect(EBattleEffect::BAD_MORALE, soundBase::BADMRLE, stack->getPosition());
  101. break;
  102. }
  103. //displaying special abilities
  104. auto actionTarget = action->getTarget(owner->curInt->cb.get());
  105. switch(action->actionType)
  106. {
  107. case EActionType::STACK_HEAL:
  108. displayEffect(EBattleEffect::REGENERATION, soundBase::REGENER, actionTarget.at(0).hexValue);
  109. break;
  110. }
  111. }
  112. void CBattleEffectsController::showBattlefieldObjects(std::shared_ptr<CCanvas> canvas, const BattleHex & destTile)
  113. {
  114. for (auto & elem : battleEffects)
  115. {
  116. if (!elem.position.isValid() && destTile != BattleHex::HEX_AFTER_ALL)
  117. continue;
  118. if (elem.position.isValid() && elem.position != destTile)
  119. continue;
  120. int currentFrame = static_cast<int>(floor(elem.currentFrame));
  121. currentFrame %= elem.animation->size();
  122. auto img = elem.animation->getImage(currentFrame);
  123. canvas->draw(img, Point(elem.x, elem.y));
  124. }
  125. }