MapViewController.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * MapViewController.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 "MapViewController.h"
  12. #include "MapRendererContext.h"
  13. #include "MapViewModel.h"
  14. #include "../../lib/CConfigHandler.h"
  15. #include "../../lib/mapObjects/CGHeroInstance.h"
  16. #include "../../lib/mapObjects/MiscObjects.h"
  17. #include "../../lib/spells/ViewSpellInt.h"
  18. void MapViewController::setViewCenter(const int3 & position)
  19. {
  20. assert(context->isInMap(position));
  21. setViewCenter(Point(position) * model->getSingleTileSize(), position.z);
  22. }
  23. void MapViewController::setViewCenter(const Point & position, int level)
  24. {
  25. Point betterPosition = {
  26. vstd::clamp(position.x, 0, context->getMapSize().x * model->getSingleTileSize().x),
  27. vstd::clamp(position.y, 0, context->getMapSize().y * model->getSingleTileSize().y)
  28. };
  29. model->setViewCenter(betterPosition);
  30. model->setLevel(vstd::clamp(level, 0, context->getMapSize().z));
  31. }
  32. void MapViewController::setTileSize(const Point & tileSize)
  33. {
  34. model->setTileSize(tileSize);
  35. }
  36. MapViewController::MapViewController(std::shared_ptr<MapViewModel> model)
  37. : context(new MapRendererContext())
  38. , model(std::move(model))
  39. {
  40. }
  41. std::shared_ptr<const IMapRendererContext> MapViewController::getContext() const
  42. {
  43. return context;
  44. }
  45. void MapViewController::update(uint32_t timeDelta)
  46. {
  47. // confirmed to match H3 for
  48. // - hero embarking on boat (500 ms)
  49. // - hero disembarking from boat (500 ms)
  50. // - TODO: picking up resources
  51. // - TODO: killing mosters
  52. // - teleporting ( 250 ms)
  53. static const double fadeOutDuration = 500;
  54. static const double fadeInDuration = 500;
  55. static const double heroTeleportDuration = 250;
  56. //FIXME: remove code duplication?
  57. if(context->movementAnimation)
  58. {
  59. // TODO: enemyMoveTime
  60. double heroMoveTime = settings["adventure"]["heroMoveTime"].Float();
  61. context->movementAnimation->progress += timeDelta / heroMoveTime;
  62. Point positionFrom = Point(context->movementAnimation->tileFrom) * model->getSingleTileSize();
  63. Point positionDest = Point(context->movementAnimation->tileDest) * model->getSingleTileSize();
  64. Point positionCurr = vstd::lerp(positionFrom, positionDest, context->movementAnimation->progress);
  65. setViewCenter(positionCurr, context->movementAnimation->tileDest.z);
  66. if(context->movementAnimation->progress >= 1.0)
  67. {
  68. setViewCenter(context->movementAnimation->tileDest);
  69. context->removeObject(context->getObject(context->movementAnimation->target));
  70. context->addObject(context->getObject(context->movementAnimation->target));
  71. context->movementAnimation.reset();
  72. }
  73. }
  74. if(context->teleportAnimation)
  75. {
  76. context->teleportAnimation->progress += timeDelta / heroTeleportDuration;
  77. if(context->teleportAnimation->progress >= 1.0)
  78. context->teleportAnimation.reset();
  79. }
  80. if(context->fadeOutAnimation)
  81. {
  82. context->fadeOutAnimation->progress += timeDelta / fadeOutDuration;
  83. if(context->fadeOutAnimation->progress >= 1.0)
  84. {
  85. context->removeObject(context->getObject(context->fadeOutAnimation->target));
  86. context->fadeOutAnimation.reset();
  87. }
  88. }
  89. if(context->fadeInAnimation)
  90. {
  91. context->fadeInAnimation->progress += timeDelta / fadeInDuration;
  92. if(context->fadeInAnimation->progress >= 1.0)
  93. context->fadeInAnimation.reset();
  94. }
  95. context->animationTime += timeDelta;
  96. //context->tileSize = Point(32,32); //model->getSingleTileSize();
  97. context->worldViewModeActive = model->getSingleTileSize() != Point(32,32);
  98. }
  99. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  100. {
  101. assert(!context->fadeInAnimation);
  102. context->fadeInAnimation = FadingAnimationState{obj->id, 0.0};
  103. context->addObject(obj);
  104. }
  105. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  106. {
  107. assert(!context->fadeOutAnimation);
  108. context->fadeOutAnimation = FadingAnimationState{obj->id, 0.0};
  109. }
  110. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  111. {
  112. context->addObject(obj);
  113. };
  114. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  115. {
  116. context->removeObject(obj);
  117. };
  118. void MapViewController::onHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  119. {
  120. assert(!context->teleportAnimation);
  121. context->teleportAnimation = HeroAnimationState{obj->id, from, dest, 0.0};
  122. }
  123. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  124. {
  125. assert(!context->movementAnimation);
  126. const CGObjectInstance * movingObject = obj;
  127. if(obj->boat)
  128. movingObject = obj->boat;
  129. context->removeObject(movingObject);
  130. if(settings["adventure"]["heroMoveTime"].Float() > 1)
  131. {
  132. context->addMovingObject(movingObject, from, dest);
  133. context->movementAnimation = HeroAnimationState{movingObject->id, from, dest, 0.0};
  134. }
  135. else
  136. {
  137. // instant movement
  138. context->addObject(movingObject);
  139. }
  140. }
  141. void MapViewController::onHeroRotated(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  142. {
  143. //TODO
  144. }
  145. bool MapViewController::hasOngoingAnimations()
  146. {
  147. if(context->movementAnimation)
  148. return true;
  149. if(context->teleportAnimation)
  150. return true;
  151. if(context->fadeOutAnimation)
  152. return true;
  153. if(context->fadeInAnimation)
  154. return true;
  155. return false;
  156. }
  157. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  158. {
  159. context->showAllTerrain = showAllTerrain;
  160. }
  161. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  162. {
  163. context->additionalOverlayIcons = objectPositions;
  164. }