CBattleObstacleController.cpp 6.0 KB

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