BattleObstacleController.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 & obstacle : owner.curInt->cb->battleGetAllObstacles())
  111. {
  112. if(obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  113. {
  114. auto img = getObstacleImage(*obstacle);
  115. if(img)
  116. canvas.draw(img, Point(obstacle->getInfo().width, obstacle->getInfo().height));
  117. }
  118. if (obstacle->obstacleType == CObstacleInstance::USUAL)
  119. {
  120. if (obstacle->getInfo().isForegroundObstacle)
  121. continue;
  122. auto img = getObstacleImage(*obstacle);
  123. if(img)
  124. {
  125. Point p = getObstaclePosition(img, *obstacle);
  126. canvas.draw(img, p);
  127. }
  128. }
  129. }
  130. }
  131. void BattleObstacleController::collectRenderableObjects(BattleRenderer & renderer)
  132. {
  133. for (auto obstacle : owner.curInt->cb->battleGetAllObstacles())
  134. {
  135. if (obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  136. continue;
  137. if (obstacle->obstacleType == CObstacleInstance::MOAT)
  138. continue;
  139. if (obstacle->obstacleType == CObstacleInstance::USUAL && !obstacle->getInfo().isForegroundObstacle)
  140. continue;
  141. renderer.insert(EBattleFieldLayer::OBSTACLES, obstacle->pos, [this, obstacle]( BattleRenderer::RendererRef canvas ){
  142. auto img = getObstacleImage(*obstacle);
  143. if(img)
  144. {
  145. Point p = getObstaclePosition(img, *obstacle);
  146. canvas.draw(img, p);
  147. }
  148. });
  149. }
  150. }
  151. void BattleObstacleController::tick(uint32_t msPassed)
  152. {
  153. timePassed += msPassed / 1000.f;
  154. }
  155. std::shared_ptr<IImage> BattleObstacleController::getObstacleImage(const CObstacleInstance & oi)
  156. {
  157. int framesCount = timePassed * AnimationControls::getObstaclesSpeed();
  158. std::shared_ptr<CAnimation> animation;
  159. // obstacle is not loaded yet, don't show anything
  160. if (obstacleAnimations.count(oi.uniqueID) == 0)
  161. return nullptr;
  162. animation = obstacleAnimations[oi.uniqueID];
  163. assert(animation);
  164. if(animation)
  165. {
  166. int frameIndex = framesCount % animation->size(0);
  167. return animation->getImage(frameIndex, 0);
  168. }
  169. return nullptr;
  170. }
  171. Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle)
  172. {
  173. int offset = obstacle.getAnimationYOffset(image->height());
  174. Rect r = owner.fieldController->hexPositionLocal(obstacle.pos);
  175. r.y += 42 - image->height() + offset;
  176. return r.topLeft();
  177. }
  178. Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const JsonNode & obstacle)
  179. {
  180. auto animationYOffset = obstacle["animationYOffset"].Integer();
  181. auto offset = image->height() % 42;
  182. if(obstacle["needAnimationOffsetFix"].Bool() && offset > 37)
  183. animationYOffset -= 42;
  184. offset += animationYOffset;
  185. Rect r = owner.fieldController->hexPositionLocal(obstacle["position"].Integer());
  186. r.y += 42 - image->height() + offset;
  187. return r.topLeft();
  188. }