BattleObstacleController.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 = oi.getAnimation();
  41. if (animationsCache.count(animationName) == 0)
  42. {
  43. if (oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  44. {
  45. // obstacle uses single bitmap image for animations
  46. auto animation = std::make_shared<CAnimation>();
  47. animation->setCustom(animationName, 0, 0);
  48. animationsCache[animationName] = animation;
  49. animation->preload();
  50. }
  51. else
  52. {
  53. auto animation = std::make_shared<CAnimation>(animationName);
  54. animationsCache[animationName] = animation;
  55. animation->preload();
  56. }
  57. }
  58. obstacleAnimations[oi.uniqueID] = animationsCache[animationName];
  59. }
  60. void BattleObstacleController::obstacleRemoved(const std::vector<ObstacleChanges> & obstacles)
  61. {
  62. for(const auto & oi : obstacles)
  63. {
  64. auto & obstacle = oi.data["obstacle"];
  65. if (!obstacle.isStruct())
  66. {
  67. logGlobal->error("I don't know how to animate removal of this obstacle");
  68. continue;
  69. }
  70. auto animation = std::make_shared<CAnimation>(obstacle["appearAnimation"].String());
  71. animation->preload();
  72. auto first = animation->getImage(0, 0);
  73. if(!first)
  74. continue;
  75. //we assume here that effect graphics have the same size as the usual obstacle image
  76. // -> if we know how to blit obstacle, let's blit the effect in the same place
  77. Point whereTo = getObstaclePosition(first, obstacle);
  78. //AFAIK, in H3 there is no sound of obstacle removal
  79. owner.stacksController->addNewAnim(new EffectAnimation(owner, obstacle["appearAnimation"].String(), whereTo, obstacle["position"].Integer(), 0, true));
  80. obstacleAnimations.erase(oi.id);
  81. //so when multiple obstacles are removed, they show up one after another
  82. owner.waitForAnimations();
  83. }
  84. }
  85. void BattleObstacleController::obstaclePlaced(const std::vector<std::shared_ptr<const CObstacleInstance>> & obstacles)
  86. {
  87. for(const auto & oi : obstacles)
  88. {
  89. auto side = owner.curInt->cb->playerToSide(owner.curInt->playerID);
  90. if(!oi->visibleForSide(side.value(), owner.curInt->cb->battleHasNativeStack(side.value())))
  91. continue;
  92. auto animation = std::make_shared<CAnimation>(oi->getAppearAnimation());
  93. animation->preload();
  94. auto first = animation->getImage(0, 0);
  95. if(!first)
  96. continue;
  97. //we assume here that effect graphics have the same size as the usual obstacle image
  98. // -> if we know how to blit obstacle, let's blit the effect in the same place
  99. Point whereTo = getObstaclePosition(first, *oi);
  100. CCS->soundh->playSound( oi->getAppearSound() );
  101. owner.stacksController->addNewAnim(new EffectAnimation(owner, oi->getAppearAnimation(), whereTo, oi->pos));
  102. //so when multiple obstacles are added, they show up one after another
  103. owner.waitForAnimations();
  104. loadObstacleImage(*oi);
  105. }
  106. }
  107. void BattleObstacleController::showAbsoluteObstacles(Canvas & canvas)
  108. {
  109. //Blit absolute obstacles
  110. for(auto & oi : owner.curInt->cb->battleGetAllObstacles())
  111. {
  112. if(oi->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  113. {
  114. auto img = getObstacleImage(*oi);
  115. if(img)
  116. canvas.draw(img, Point(oi->getInfo().width, oi->getInfo().height));
  117. }
  118. }
  119. }
  120. void BattleObstacleController::collectRenderableObjects(BattleRenderer & renderer)
  121. {
  122. for (auto obstacle : owner.curInt->cb->battleGetAllObstacles())
  123. {
  124. if (obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  125. continue;
  126. if (obstacle->obstacleType == CObstacleInstance::MOAT)
  127. continue;
  128. renderer.insert(EBattleFieldLayer::OBSTACLES, obstacle->pos, [this, obstacle]( BattleRenderer::RendererRef canvas ){
  129. auto img = getObstacleImage(*obstacle);
  130. if(img)
  131. {
  132. Point p = getObstaclePosition(img, *obstacle);
  133. canvas.draw(img, p);
  134. }
  135. });
  136. }
  137. }
  138. void BattleObstacleController::update()
  139. {
  140. timePassed += GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
  141. }
  142. std::shared_ptr<IImage> BattleObstacleController::getObstacleImage(const CObstacleInstance & oi)
  143. {
  144. int framesCount = timePassed * AnimationControls::getObstaclesSpeed();
  145. std::shared_ptr<CAnimation> animation;
  146. // obstacle is not loaded yet, don't show anything
  147. if (obstacleAnimations.count(oi.uniqueID) == 0)
  148. return nullptr;
  149. animation = obstacleAnimations[oi.uniqueID];
  150. assert(animation);
  151. if(animation)
  152. {
  153. int frameIndex = framesCount % animation->size(0);
  154. return animation->getImage(frameIndex, 0);
  155. }
  156. return nullptr;
  157. }
  158. Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle)
  159. {
  160. int offset = obstacle.getAnimationYOffset(image->height());
  161. Rect r = owner.fieldController->hexPositionLocal(obstacle.pos);
  162. r.y += 42 - image->height() + offset;
  163. return r.topLeft();
  164. }
  165. Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const JsonNode & obstacle)
  166. {
  167. auto animationYOffset = obstacle["animationYOffset"].Integer();
  168. auto offset = image->height() % 42;
  169. if(obstacle["needAnimationOffsetFix"].Bool() && offset > 37)
  170. animationYOffset -= 42;
  171. offset += animationYOffset;
  172. Rect r = owner.fieldController->hexPositionLocal(obstacle["position"].Integer());
  173. r.y += 42 - image->height() + offset;
  174. return r.topLeft();
  175. }