2
0

BattleEffectsController.cpp 4.9 KB

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