BattleObstacleController.cpp 6.8 KB

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