BattleObstacleController.cpp 5.2 KB

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