BattleObstacleController.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * BattleObstacleController.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 "BattleObstacleController.h"
  12. #include "BattleInterface.h"
  13. #include "BattleFieldController.h"
  14. #include "BattleAnimationClasses.h"
  15. #include "BattleStacksController.h"
  16. #include "BattleRenderer.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../gui/CAnimation.h"
  19. #include "../gui/Canvas.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/battle/CObstacleInstance.h"
  22. #include "../../lib/ObstacleHandler.h"
  23. BattleObstacleController::BattleObstacleController(BattleInterface & owner):
  24. owner(owner)
  25. {
  26. auto obst = owner.curInt->cb->battleGetAllObstacles();
  27. for(auto & elem : obst)
  28. {
  29. if ( elem->obstacleType == CObstacleInstance::MOAT )
  30. continue; // handled by siege controller;
  31. loadObstacleImage(*elem);
  32. }
  33. }
  34. void BattleObstacleController::loadObstacleImage(const CObstacleInstance & oi)
  35. {
  36. std::string animationName;
  37. if (auto spellObstacle = dynamic_cast<const SpellCreatedObstacle*>(&oi))
  38. {
  39. animationName = spellObstacle->animation;
  40. }
  41. else
  42. {
  43. assert( oi.obstacleType == CObstacleInstance::USUAL || oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE);
  44. animationName = oi.getInfo().animation;
  45. }
  46. if (animationsCache.count(animationName) == 0)
  47. {
  48. if (oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  49. {
  50. // obstacle uses single bitmap image for animations
  51. auto animation = std::make_shared<CAnimation>();
  52. animation->setCustom(animationName, 0, 0);
  53. animationsCache[animationName] = animation;
  54. animation->preload();
  55. }
  56. else
  57. {
  58. auto animation = std::make_shared<CAnimation>(animationName);
  59. animationsCache[animationName] = animation;
  60. animation->preload();
  61. }
  62. }
  63. obstacleAnimations[oi.uniqueID] = animationsCache[animationName];
  64. }
  65. void BattleObstacleController::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> & obstacles)
  66. {
  67. for (auto const & oi : obstacles)
  68. {
  69. auto spellObstacle = dynamic_cast<const SpellCreatedObstacle*>(oi.get());
  70. if (!spellObstacle)
  71. {
  72. logGlobal->error("I don't know how to animate appearing obstacle of type %d", (int)oi->obstacleType);
  73. continue;
  74. }
  75. auto animation = std::make_shared<CAnimation>(spellObstacle->appearAnimation);
  76. animation->preload();
  77. auto first = animation->getImage(0, 0);
  78. if(!first)
  79. continue;
  80. //we assume here that effect graphics have the same size as the usual obstacle image
  81. // -> if we know how to blit obstacle, let's blit the effect in the same place
  82. Point whereTo = getObstaclePosition(first, *oi);
  83. owner.stacksController->addNewAnim(new PointEffectAnimation(owner, spellObstacle->appearSound, spellObstacle->appearAnimation, whereTo, oi->pos));
  84. //so when multiple obstacles are added, they show up one after another
  85. owner.waitForAnimationCondition(EAnimationEvents::ACTION, false);
  86. loadObstacleImage(*spellObstacle);
  87. }
  88. }
  89. void BattleObstacleController::showAbsoluteObstacles(Canvas & canvas, const Point & offset)
  90. {
  91. //Blit absolute obstacles
  92. for(auto & oi : owner.curInt->cb->battleGetAllObstacles())
  93. {
  94. if(oi->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  95. {
  96. auto img = getObstacleImage(*oi);
  97. if(img)
  98. canvas.draw(img, Point(offset.x + oi->getInfo().width, offset.y + oi->getInfo().height));
  99. }
  100. }
  101. }
  102. void BattleObstacleController::collectRenderableObjects(BattleRenderer & renderer)
  103. {
  104. for (auto obstacle : owner.curInt->cb->battleGetAllObstacles())
  105. {
  106. if (obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  107. continue;
  108. if (obstacle->obstacleType == CObstacleInstance::MOAT)
  109. continue;
  110. renderer.insert(EBattleFieldLayer::OBSTACLES, obstacle->pos, [this, obstacle]( BattleRenderer::RendererRef canvas ){
  111. auto img = getObstacleImage(*obstacle);
  112. if(img)
  113. {
  114. Point p = getObstaclePosition(img, *obstacle);
  115. canvas.draw(img, p);
  116. }
  117. });
  118. }
  119. }
  120. std::shared_ptr<IImage> BattleObstacleController::getObstacleImage(const CObstacleInstance & oi)
  121. {
  122. int frameIndex = (owner.animCount+1) *25 / owner.getAnimSpeed();
  123. std::shared_ptr<CAnimation> animation;
  124. // obstacle is not loaded yet, don't show anything
  125. if (obstacleAnimations.count(oi.uniqueID) == 0)
  126. return nullptr;
  127. animation = obstacleAnimations[oi.uniqueID];
  128. assert(animation);
  129. if(animation)
  130. {
  131. frameIndex %= animation->size(0);
  132. return animation->getImage(frameIndex, 0);
  133. }
  134. return nullptr;
  135. }
  136. Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle)
  137. {
  138. int offset = obstacle.getAnimationYOffset(image->height());
  139. Rect r = owner.fieldController->hexPositionAbsolute(obstacle.pos);
  140. r.y += 42 - image->height() + offset;
  141. return r.topLeft();
  142. }