2
0

MapViewController.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "../adventureMap/CAdvMapInt.h"
  15. #include "../../lib/CConfigHandler.h"
  16. #include "../../lib/mapObjects/CGHeroInstance.h"
  17. #include "../../lib/mapObjects/MiscObjects.h"
  18. #include "../../lib/spells/ViewSpellInt.h"
  19. void MapViewController::setViewCenter(const int3 & position)
  20. {
  21. assert(context->isInMap(position));
  22. setViewCenter(Point(position) * model->getSingleTileSize(), position.z);
  23. }
  24. void MapViewController::setViewCenter(const Point & position, int level)
  25. {
  26. Point betterPosition = {
  27. vstd::clamp(position.x, 0, context->getMapSize().x * model->getSingleTileSize().x),
  28. vstd::clamp(position.y, 0, context->getMapSize().y * model->getSingleTileSize().y)
  29. };
  30. model->setViewCenter(betterPosition);
  31. model->setLevel(vstd::clamp(level, 0, context->getMapSize().z));
  32. }
  33. void MapViewController::setTileSize(const Point & tileSize)
  34. {
  35. model->setTileSize(tileSize);
  36. }
  37. MapViewController::MapViewController(std::shared_ptr<MapViewModel> model)
  38. : context(new MapRendererContext())
  39. , model(std::move(model))
  40. {
  41. }
  42. std::shared_ptr<const IMapRendererContext> MapViewController::getContext() const
  43. {
  44. return context;
  45. }
  46. void MapViewController::moveFocusToSelection()
  47. {
  48. const auto * army = adventureInt->curArmy();
  49. if (army)
  50. setViewCenter(army->getSightCenter());
  51. }
  52. void MapViewController::update(uint32_t timeDelta)
  53. {
  54. // confirmed to match H3 for
  55. // - hero embarking on boat (500 ms)
  56. // - hero disembarking from boat (500 ms)
  57. // - TODO: picking up resources
  58. // - TODO: killing mosters
  59. // - teleporting ( 250 ms)
  60. static const double fadeOutDuration = 500;
  61. static const double fadeInDuration = 500;
  62. static const double heroTeleportDuration = 250;
  63. //FIXME: remove code duplication?
  64. if(context->movementAnimation)
  65. {
  66. const auto * object = context->getObject(context->movementAnimation->target);
  67. const auto * hero = dynamic_cast<const CGHeroInstance*>(object);
  68. const auto * boat = dynamic_cast<const CGBoat*>(object);
  69. assert(boat || hero);
  70. if (!hero)
  71. hero = boat->hero;
  72. // TODO: enemyMoveTime
  73. double heroMoveTime = settings["adventure"]["heroMoveTime"].Float();
  74. context->movementAnimation->progress += timeDelta / heroMoveTime;
  75. Point positionFrom = Point(hero->convertToVisitablePos(context->movementAnimation->tileFrom)) * model->getSingleTileSize();
  76. Point positionDest = Point(hero->convertToVisitablePos(context->movementAnimation->tileDest)) * model->getSingleTileSize();
  77. Point positionCurr = vstd::lerp(positionFrom, positionDest, context->movementAnimation->progress);
  78. setViewCenter(positionCurr, context->movementAnimation->tileDest.z);
  79. if(context->movementAnimation->progress >= 1.0)
  80. {
  81. setViewCenter(hero->getSightCenter());
  82. context->removeObject(context->getObject(context->movementAnimation->target));
  83. context->addObject(context->getObject(context->movementAnimation->target));
  84. context->movementAnimation.reset();
  85. }
  86. }
  87. if(context->teleportAnimation)
  88. {
  89. context->teleportAnimation->progress += timeDelta / heroTeleportDuration;
  90. moveFocusToSelection();
  91. if(context->teleportAnimation->progress >= 1.0)
  92. context->teleportAnimation.reset();
  93. }
  94. if(context->fadeOutAnimation)
  95. {
  96. context->fadeOutAnimation->progress += timeDelta / fadeOutDuration;
  97. moveFocusToSelection();
  98. if(context->fadeOutAnimation->progress >= 1.0)
  99. {
  100. context->removeObject(context->getObject(context->fadeOutAnimation->target));
  101. context->fadeOutAnimation.reset();
  102. }
  103. }
  104. if(context->fadeInAnimation)
  105. {
  106. context->fadeInAnimation->progress += timeDelta / fadeInDuration;
  107. moveFocusToSelection();
  108. if(context->fadeInAnimation->progress >= 1.0)
  109. context->fadeInAnimation.reset();
  110. }
  111. context->animationTime += timeDelta;
  112. context->worldViewModeActive = model->getSingleTileSize() != Point(32,32);
  113. context->settingsSessionSpectate = settings["session"]["spectate"].Bool();
  114. context->settingsAdventureObjectAnimation = settings["adventure"]["objectAnimation"].Bool();
  115. context->settingsAdventureTerrainAnimation = settings["adventure"]["terrainAnimation"].Bool();
  116. context->settingsSessionShowGrid = settings["gameTweaks"]["showGrid"].Bool();
  117. context->settingsSessionShowVisitable = settings["session"]["showVisitable"].Bool();
  118. context->settingsSessionShowBlockable = settings["session"]["showBlockable"].Bool();
  119. }
  120. void MapViewController::onObjectFadeIn(const CGObjectInstance * obj)
  121. {
  122. bool actionVisible = context->isVisible(obj->pos);
  123. assert(!context->fadeInAnimation);
  124. if (actionVisible)
  125. context->fadeInAnimation = FadingAnimationState{obj->id, 0.0};
  126. context->addObject(obj);
  127. }
  128. void MapViewController::onObjectFadeOut(const CGObjectInstance * obj)
  129. {
  130. bool actionVisible = context->isVisible(obj->pos);
  131. assert(!context->fadeOutAnimation);
  132. if (actionVisible)
  133. context->fadeOutAnimation = FadingAnimationState{obj->id, 0.0};
  134. else
  135. context->removeObject(obj);
  136. }
  137. void MapViewController::onObjectInstantAdd(const CGObjectInstance * obj)
  138. {
  139. context->addObject(obj);
  140. };
  141. void MapViewController::onObjectInstantRemove(const CGObjectInstance * obj)
  142. {
  143. context->removeObject(obj);
  144. };
  145. void MapViewController::onHeroTeleported(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  146. {
  147. assert(!context->teleportAnimation);
  148. bool actionVisible = context->isVisible(from) || context->isVisible(dest);
  149. if (actionVisible)
  150. {
  151. context->teleportAnimation = HeroAnimationState{obj->id, from, dest, 0.0};
  152. }
  153. else
  154. {
  155. context->removeObject(obj);
  156. context->addObject(obj);
  157. }
  158. }
  159. void MapViewController::onHeroMoved(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  160. {
  161. assert(!context->movementAnimation);
  162. bool actionVisible = context->isVisible(from) || context->isVisible(dest);
  163. const CGObjectInstance * movingObject = obj;
  164. if(obj->boat)
  165. movingObject = obj->boat;
  166. context->removeObject(movingObject);
  167. if(settings["adventure"]["heroMoveTime"].Float() > 1 && actionVisible)
  168. {
  169. context->addMovingObject(movingObject, from, dest);
  170. context->movementAnimation = HeroAnimationState{movingObject->id, from, dest, 0.0};
  171. }
  172. else
  173. {
  174. // instant movement
  175. context->addObject(movingObject);
  176. if (actionVisible)
  177. setViewCenter(movingObject->visitablePos());
  178. }
  179. }
  180. void MapViewController::onHeroRotated(const CGHeroInstance * obj, const int3 & from, const int3 & dest)
  181. {
  182. //TODO. Or no-op?
  183. }
  184. bool MapViewController::hasOngoingAnimations()
  185. {
  186. if(context->movementAnimation)
  187. return true;
  188. if(context->teleportAnimation)
  189. return true;
  190. if(context->fadeOutAnimation)
  191. return true;
  192. if(context->fadeInAnimation)
  193. return true;
  194. return false;
  195. }
  196. void MapViewController::setTerrainVisibility(bool showAllTerrain)
  197. {
  198. context->showAllTerrain = showAllTerrain;
  199. }
  200. void MapViewController::setOverlayVisibility(const std::vector<ObjectPosInfo> & objectPositions)
  201. {
  202. context->additionalOverlayIcons = objectPositions;
  203. }