BattleObstacleController.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "../CGameInfo.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../media/ISoundPlayer.h"
  22. #include "../render/CAnimation.h"
  23. #include "../render/Canvas.h"
  24. #include "../render/IRenderHandler.h"
  25. #include "../../CCallback.h"
  26. #include "../../lib/battle/CObstacleInstance.h"
  27. #include "../../lib/ObstacleHandler.h"
  28. #include "../../lib/serializer/JsonDeserializer.h"
  29. BattleObstacleController::BattleObstacleController(BattleInterface & owner):
  30. owner(owner),
  31. timePassed(0.f)
  32. {
  33. auto obst = owner.getBattle()->battleGetAllObstacles();
  34. for(auto & elem : obst)
  35. {
  36. if ( elem->obstacleType == CObstacleInstance::MOAT )
  37. continue; // handled by siege controller;
  38. loadObstacleImage(*elem);
  39. }
  40. }
  41. void BattleObstacleController::loadObstacleImage(const CObstacleInstance & oi)
  42. {
  43. AnimationPath animationName = oi.getAnimation();
  44. if (oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  45. {
  46. // obstacle uses single bitmap image for animations
  47. obstacleImages[oi.uniqueID] = GH.renderHandler().loadImage(animationName.toType<EResType::IMAGE>(), EImageBlitMode::COLORKEY);
  48. }
  49. else
  50. {
  51. obstacleAnimations[oi.uniqueID] = GH.renderHandler().loadAnimation(animationName, EImageBlitMode::COLORKEY);
  52. obstacleImages[oi.uniqueID] = obstacleAnimations[oi.uniqueID]->getImage(0);
  53. }
  54. }
  55. void BattleObstacleController::obstacleRemoved(const std::vector<ObstacleChanges> & obstacles)
  56. {
  57. for(const auto & oi : obstacles)
  58. {
  59. auto & obstacle = oi.data["obstacle"];
  60. if (!obstacle.isStruct())
  61. {
  62. logGlobal->error("I don't know how to animate removal of this obstacle");
  63. continue;
  64. }
  65. AnimationPath animationPath;
  66. JsonDeserializer ser(nullptr, obstacle);
  67. ser.serializeStruct("appearAnimation", animationPath);
  68. if(animationPath.empty())
  69. continue;
  70. auto animation = GH.renderHandler().loadAnimation(animationPath, EImageBlitMode::COLORKEY);
  71. auto first = animation->getImage(0, 0);
  72. if(!first)
  73. continue;
  74. //we assume here that effect graphics have the same size as the usual obstacle image
  75. // -> if we know how to blit obstacle, let's blit the effect in the same place
  76. Point whereTo = getObstaclePosition(first, obstacle);
  77. //AFAIK, in H3 there is no sound of obstacle removal
  78. owner.stacksController->addNewAnim(new EffectAnimation(owner, animationPath, whereTo, obstacle["position"].Integer(), 0, true));
  79. obstacleAnimations.erase(oi.id);
  80. obstacleImages.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.getBattle()->playerToSide(owner.curInt->playerID);
  90. if(!oi->visibleForSide(side, owner.getBattle()->battleHasNativeStack(side)))
  91. continue;
  92. auto animation = GH.renderHandler().loadAnimation(oi->getAppearAnimation(), EImageBlitMode::ALPHA);
  93. auto first = animation->getImage(0, 0);
  94. if(!first)
  95. continue;
  96. //we assume here that effect graphics have the same size as the usual obstacle image
  97. // -> if we know how to blit obstacle, let's blit the effect in the same place
  98. Point whereTo = getObstaclePosition(first, *oi);
  99. CCS->soundh->playSound( oi->getAppearSound() );
  100. owner.stacksController->addNewAnim(new EffectAnimation(owner, oi->getAppearAnimation(), whereTo, oi->pos));
  101. //so when multiple obstacles are added, they show up one after another
  102. owner.waitForAnimations();
  103. loadObstacleImage(*oi);
  104. }
  105. }
  106. void BattleObstacleController::showAbsoluteObstacles(Canvas & canvas)
  107. {
  108. //Blit absolute obstacles
  109. for(auto & obstacle : owner.getBattle()->battleGetAllObstacles())
  110. {
  111. if(obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  112. {
  113. auto img = getObstacleImage(*obstacle);
  114. if(img)
  115. canvas.draw(img, Point(obstacle->getInfo().width, obstacle->getInfo().height));
  116. }
  117. if (obstacle->obstacleType == CObstacleInstance::USUAL)
  118. {
  119. if (obstacle->getInfo().isForegroundObstacle)
  120. continue;
  121. auto img = getObstacleImage(*obstacle);
  122. if(img)
  123. {
  124. Point p = getObstaclePosition(img, *obstacle);
  125. canvas.draw(img, p);
  126. }
  127. }
  128. }
  129. }
  130. void BattleObstacleController::collectRenderableObjects(BattleRenderer & renderer)
  131. {
  132. for (auto obstacle : owner.getBattle()->battleGetAllObstacles())
  133. {
  134. if (obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  135. continue;
  136. if (obstacle->obstacleType == CObstacleInstance::MOAT)
  137. continue;
  138. if (obstacle->obstacleType == CObstacleInstance::USUAL && !obstacle->getInfo().isForegroundObstacle)
  139. continue;
  140. renderer.insert(EBattleFieldLayer::OBSTACLES, obstacle->pos, [this, obstacle]( BattleRenderer::RendererRef canvas ){
  141. auto img = getObstacleImage(*obstacle);
  142. if(img)
  143. {
  144. Point p = getObstaclePosition(img, *obstacle);
  145. canvas.draw(img, p);
  146. }
  147. });
  148. }
  149. }
  150. void BattleObstacleController::tick(uint32_t msPassed)
  151. {
  152. timePassed += msPassed / 1000.f;
  153. int framesCount = timePassed * AnimationControls::getObstaclesSpeed();
  154. for(auto & animation : obstacleAnimations)
  155. {
  156. int frameIndex = framesCount % animation.second->size(0);
  157. obstacleImages[animation.first] = animation.second->getImage(frameIndex, 0);
  158. }
  159. }
  160. std::shared_ptr<IImage> BattleObstacleController::getObstacleImage(const CObstacleInstance & oi)
  161. {
  162. // obstacle is not loaded yet, don't show anything
  163. if (obstacleImages.count(oi.uniqueID) == 0)
  164. return nullptr;
  165. return obstacleImages[oi.uniqueID];
  166. }
  167. Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle)
  168. {
  169. int offset = obstacle.getAnimationYOffset(image->height());
  170. Rect r = owner.fieldController->hexPositionLocal(obstacle.pos);
  171. r.y += 42 - image->height() + offset;
  172. return r.topLeft();
  173. }
  174. Point BattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const JsonNode & obstacle)
  175. {
  176. auto animationYOffset = obstacle["animationYOffset"].Integer();
  177. auto offset = image->height() % 42;
  178. if(obstacle["needAnimationOffsetFix"].Bool() && offset > 37)
  179. animationYOffset -= 42;
  180. offset += animationYOffset;
  181. Rect r = owner.fieldController->hexPositionLocal(obstacle["position"].Integer());
  182. r.y += 42 - image->height() + offset;
  183. return r.topLeft();
  184. }