BattleEffectsController.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "BattleWindow.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/filesystem/ResourceID.h"
  27. #include "../../lib/NetPacks.h"
  28. #include "../../lib/CStack.h"
  29. #include "../../lib/IGameEventsReceiver.h"
  30. #include "../../lib/CGeneralTextHandler.h"
  31. BattleEffectsController::BattleEffectsController(BattleInterface & owner):
  32. owner(owner)
  33. {
  34. loadColorMuxers();
  35. }
  36. void BattleEffectsController::displayEffect(EBattleEffect effect, const BattleHex & destTile)
  37. {
  38. displayEffect(effect, "", destTile);
  39. }
  40. void BattleEffectsController::displayEffect(EBattleEffect effect, std::string soundFile, const BattleHex & destTile)
  41. {
  42. size_t effectID = static_cast<size_t>(effect);
  43. std::string customAnim = graphics->battleACToDef[effectID][0];
  44. owner.stacksController->addNewAnim(new PointEffectAnimation(owner, soundFile, customAnim, destTile));
  45. }
  46. void BattleEffectsController::battleTriggerEffect(const BattleTriggerEffect & bte)
  47. {
  48. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  49. const CStack * stack = owner.curInt->cb->battleGetStackByID(bte.stackID);
  50. if(!stack)
  51. {
  52. logGlobal->error("Invalid stack ID %d", bte.stackID);
  53. return;
  54. }
  55. //don't show animation when no HP is regenerated
  56. switch(bte.effect)
  57. {
  58. case Bonus::HP_REGENERATION:
  59. displayEffect(EBattleEffect::REGENERATION, "REGENER", stack->getPosition());
  60. break;
  61. case Bonus::MANA_DRAIN:
  62. displayEffect(EBattleEffect::MANA_DRAIN, "MANADRAI", stack->getPosition());
  63. break;
  64. case Bonus::POISON:
  65. displayEffect(EBattleEffect::POISON, "POISON", stack->getPosition());
  66. break;
  67. case Bonus::FEAR:
  68. displayEffect(EBattleEffect::FEAR, "FEAR", stack->getPosition());
  69. break;
  70. case Bonus::MORALE:
  71. {
  72. std::string hlp = CGI->generaltexth->allTexts[33];
  73. boost::algorithm::replace_first(hlp,"%s",(stack->getName()));
  74. displayEffect(EBattleEffect::GOOD_MORALE, "GOODMRLE", stack->getPosition());
  75. owner.appendBattleLog(hlp);
  76. break;
  77. }
  78. default:
  79. return;
  80. }
  81. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  82. }
  83. void BattleEffectsController::startAction(const BattleAction* action)
  84. {
  85. assert(owner.getAnimationCondition(EAnimationEvents::ACTION) == false);
  86. const CStack *stack = owner.curInt->cb->battleGetStackByID(action->stackNumber);
  87. switch(action->actionType)
  88. {
  89. case EActionType::WAIT:
  90. owner.appendBattleLog(stack->formatGeneralMessage(136));
  91. break;
  92. case EActionType::BAD_MORALE:
  93. owner.appendBattleLog(stack->formatGeneralMessage(-34));
  94. displayEffect(EBattleEffect::BAD_MORALE, "BADMRLE", stack->getPosition());
  95. break;
  96. }
  97. //displaying special abilities
  98. auto actionTarget = action->getTarget(owner.curInt->cb.get());
  99. switch(action->actionType)
  100. {
  101. case EActionType::STACK_HEAL:
  102. displayEffect(EBattleEffect::REGENERATION, "REGENER", actionTarget.at(0).hexValue);
  103. break;
  104. }
  105. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  106. }
  107. void BattleEffectsController::collectRenderableObjects(BattleRenderer & renderer)
  108. {
  109. for (auto & elem : battleEffects)
  110. {
  111. renderer.insert( EBattleFieldLayer::EFFECTS, elem.position, [&elem](BattleRenderer::RendererRef canvas)
  112. {
  113. int currentFrame = static_cast<int>(floor(elem.currentFrame));
  114. currentFrame %= elem.animation->size();
  115. auto img = elem.animation->getImage(currentFrame);
  116. canvas.draw(img, Point(elem.x, elem.y));
  117. });
  118. }
  119. }
  120. void BattleEffectsController::loadColorMuxers()
  121. {
  122. const JsonNode config(ResourceID("config/battleEffects.json"));
  123. for(auto & muxer : config["colorMuxers"].Struct())
  124. {
  125. ColorMuxerEffect effect;
  126. std::string identifier = muxer.first;
  127. for (const JsonNode & entry : muxer.second.Vector() )
  128. {
  129. effect.timePoints.push_back(entry["time"].Float());
  130. effect.filters.push_back(ColorFilter::genFromJson(entry));
  131. }
  132. colorMuxerEffects[identifier] = effect;
  133. }
  134. }
  135. const ColorMuxerEffect & BattleEffectsController::getMuxerEffect(const std::string & name)
  136. {
  137. static const ColorMuxerEffect emptyEffect;
  138. if (colorMuxerEffects.count(name))
  139. return colorMuxerEffects[name];
  140. logAnim->error("Failed to find color muxer effect named '%s'!", name);
  141. return emptyEffect;
  142. }