MapViewController.cpp 5.3 KB

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