BattleObstacleController.cpp 5.5 KB

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