CBattleObstacleController.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "../CPlayerInterface.h"
  17. #include "../gui/CAnimation.h"
  18. #include "../gui/CCanvas.h"
  19. #include "../../CCallback.h"
  20. #include "../../lib/battle/CObstacleInstance.h"
  21. #include "../../lib/ObstacleHandler.h"
  22. CBattleObstacleController::CBattleObstacleController(CBattleInterface * owner):
  23. owner(owner)
  24. {
  25. auto obst = owner->curInt->cb->battleGetAllObstacles();
  26. for(auto & elem : obst)
  27. {
  28. if(elem->obstacleType == CObstacleInstance::USUAL)
  29. {
  30. std::string animationName = elem->getInfo().animation;
  31. auto cached = animationsCache.find(animationName);
  32. if(cached == animationsCache.end())
  33. {
  34. auto animation = std::make_shared<CAnimation>(animationName);
  35. animationsCache[animationName] = animation;
  36. obstacleAnimations[elem->uniqueID] = animation;
  37. animation->preload();
  38. }
  39. else
  40. {
  41. obstacleAnimations[elem->uniqueID] = cached->second;
  42. }
  43. }
  44. else if (elem->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  45. {
  46. std::string animationName = elem->getInfo().animation;
  47. auto cached = animationsCache.find(animationName);
  48. if(cached == animationsCache.end())
  49. {
  50. auto animation = std::make_shared<CAnimation>();
  51. animation->setCustom(animationName, 0, 0);
  52. animationsCache[animationName] = animation;
  53. obstacleAnimations[elem->uniqueID] = animation;
  54. animation->preload();
  55. }
  56. else
  57. {
  58. obstacleAnimations[elem->uniqueID] = cached->second;
  59. }
  60. }
  61. }
  62. }
  63. void CBattleObstacleController::obstaclePlaced(const CObstacleInstance & oi)
  64. {
  65. //so when multiple obstacles are added, they show up one after another
  66. owner->waitForAnims();
  67. //soundBase::soundID sound; // FIXME(v.markovtsev): soundh->playSound() is commented in the end => warning
  68. std::string defname;
  69. switch(oi.obstacleType)
  70. {
  71. case CObstacleInstance::SPELL_CREATED:
  72. {
  73. auto &spellObstacle = dynamic_cast<const SpellCreatedObstacle&>(oi);
  74. defname = spellObstacle.appearAnimation;
  75. //TODO: sound
  76. //soundBase::QUIKSAND
  77. //soundBase::LANDMINE
  78. //soundBase::FORCEFLD
  79. //soundBase::fireWall
  80. }
  81. break;
  82. default:
  83. logGlobal->error("I don't know how to animate appearing obstacle of type %d", (int)oi.obstacleType);
  84. return;
  85. }
  86. auto animation = std::make_shared<CAnimation>(defname);
  87. animation->preload();
  88. auto first = animation->getImage(0, 0);
  89. if(!first)
  90. return;
  91. //we assume here that effect graphics have the same size as the usual obstacle image
  92. // -> if we know how to blit obstacle, let's blit the effect in the same place
  93. Point whereTo = getObstaclePosition(first, oi);
  94. owner->stacksController->addNewAnim(new CEffectAnimation(owner, animation, whereTo.x, whereTo.y));
  95. //TODO we need to wait after playing sound till it's finished, otherwise it overlaps and sounds really bad
  96. //CCS->soundh->playSound(sound);
  97. }
  98. void CBattleObstacleController::showAbsoluteObstacles(std::shared_ptr<CCanvas> canvas, const Point & offset)
  99. {
  100. //Blit absolute obstacles
  101. for(auto & oi : owner->curInt->cb->battleGetAllObstacles())
  102. {
  103. if(oi->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  104. {
  105. auto img = getObstacleImage(*oi);
  106. if(img)
  107. canvas->draw(img, Point(offset.x + oi->getInfo().width, offset.y + oi->getInfo().height));
  108. }
  109. }
  110. }
  111. void CBattleObstacleController::showBattlefieldObjects(std::shared_ptr<CCanvas> canvas, const BattleHex & location )
  112. {
  113. for (auto &obstacle : owner->curInt->cb->battleGetAllObstacles())
  114. {
  115. if (obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  116. continue;
  117. if (obstacle->obstacleType == CObstacleInstance::MOAT)
  118. continue;
  119. if ( obstacle->pos != location)
  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. std::shared_ptr<IImage> CBattleObstacleController::getObstacleImage(const CObstacleInstance & oi)
  130. {
  131. int frameIndex = (owner->animCount+1) *25 / owner->getAnimSpeed();
  132. std::shared_ptr<CAnimation> animation;
  133. if(oi.obstacleType == CObstacleInstance::USUAL || oi.obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  134. {
  135. animation = obstacleAnimations[oi.uniqueID];
  136. }
  137. else if(oi.obstacleType == CObstacleInstance::SPELL_CREATED)
  138. {
  139. const SpellCreatedObstacle * spellObstacle = dynamic_cast<const SpellCreatedObstacle *>(&oi);
  140. if(!spellObstacle)
  141. return std::shared_ptr<IImage>();
  142. std::string animationName = spellObstacle->animation;
  143. auto cacheIter = animationsCache.find(animationName);
  144. if(cacheIter == animationsCache.end())
  145. {
  146. logAnim->trace("Creating obstacle animation %s", animationName);
  147. animation = std::make_shared<CAnimation>(animationName);
  148. animation->preload();
  149. animationsCache[animationName] = animation;
  150. }
  151. else
  152. {
  153. animation = cacheIter->second;
  154. }
  155. }
  156. if(animation)
  157. {
  158. frameIndex %= animation->size(0);
  159. return animation->getImage(frameIndex, 0);
  160. }
  161. return nullptr;
  162. }
  163. Point CBattleObstacleController::getObstaclePosition(std::shared_ptr<IImage> image, const CObstacleInstance & obstacle)
  164. {
  165. int offset = obstacle.getAnimationYOffset(image->height());
  166. Rect r = owner->fieldController->hexPosition(obstacle.pos);
  167. r.y += 42 - image->height() + offset;
  168. return r.topLeft();
  169. }