CBattleObstacleController.cpp 5.5 KB

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