BattleEffectsController.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "../CPlayerInterface.h"
  20. #include "../GameEngine.h"
  21. #include "../media/ISoundPlayer.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/networkPacks/PacksForClientBattle.h"
  29. #include "../../lib/CStack.h"
  30. #include "../../lib/IGameEventsReceiver.h"
  31. #include "../../lib/texts/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, AudioPath(), destTile);
  40. }
  41. void BattleEffectsController::displayEffect(EBattleEffect effect, const AudioPath & soundFile, const BattleHex & destTile, float transparencyFactor)
  42. {
  43. size_t effectID = static_cast<size_t>(effect);
  44. AnimationPath customAnim = AnimationPath::builtinTODO(graphics->battleACToDef[effectID][0]);
  45. ENGINE->sound().playSound( soundFile );
  46. owner.stacksController->addNewAnim(new EffectAnimation(owner, customAnim, destTile, 0, transparencyFactor));
  47. }
  48. void BattleEffectsController::battleTriggerEffect(const BattleTriggerEffect & bte)
  49. {
  50. owner.checkForAnimations();
  51. const CStack * stack = owner.getBattle()->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, AudioPath::builtin("REGENER"), stack->getPosition(), 0.5);
  62. break;
  63. case BonusType::MANA_DRAIN:
  64. displayEffect(EBattleEffect::MANA_DRAIN, AudioPath::builtin("MANADRAI"), stack->getPosition());
  65. break;
  66. case BonusType::POISON:
  67. displayEffect(EBattleEffect::POISON, AudioPath::builtin("POISON"), stack->getPosition());
  68. break;
  69. case BonusType::FEAR:
  70. displayEffect(EBattleEffect::FEAR, AudioPath::builtin("FEAR"), stack->getPosition(), 0.5);
  71. break;
  72. case BonusType::MORALE:
  73. {
  74. std::string hlp = LIBRARY->generaltexth->allTexts[33];
  75. boost::algorithm::replace_first(hlp,"%s",(stack->getName()));
  76. displayEffect(EBattleEffect::GOOD_MORALE, AudioPath::builtin("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.getBattle()->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, AudioPath::builtin("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. img->setAlpha(255 * elem.transparencyFactor);
  111. canvas.draw(img, elem.pos);
  112. });
  113. }
  114. }
  115. void BattleEffectsController::loadColorMuxers()
  116. {
  117. const JsonNode config(JsonPath::builtin("config/battleEffects.json"));
  118. for(auto & muxer : config["colorMuxers"].Struct())
  119. {
  120. ColorMuxerEffect effect;
  121. std::string identifier = muxer.first;
  122. for (const JsonNode & entry : muxer.second.Vector() )
  123. {
  124. effect.timePoints.push_back(entry["time"].Float());
  125. effect.effectColors.push_back(ColorRGBA(255*entry["color"][0].Float(), 255*entry["color"][1].Float(), 255*entry["color"][2].Float(), 255*entry["color"][3].Float()));
  126. effect.transparency.push_back(entry["alpha"].Float() * 255);
  127. }
  128. colorMuxerEffects[identifier] = effect;
  129. }
  130. }
  131. const ColorMuxerEffect & BattleEffectsController::getMuxerEffect(const std::string & name)
  132. {
  133. static const ColorMuxerEffect emptyEffect;
  134. if (colorMuxerEffects.count(name))
  135. return colorMuxerEffects[name];
  136. logAnim->error("Failed to find color muxer effect named '%s'!", name);
  137. return emptyEffect;
  138. }