BattleObstacleController.cpp 5.2 KB

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