BattleEffectsController.cpp 4.6 KB

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