CreatureAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * CCreatureAnimation.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 "CreatureAnimation.h"
  12. #include "../../lib/CConfigHandler.h"
  13. #include "../../lib/CCreatureHandler.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../render/CAnimation.h"
  16. #include "../render/Canvas.h"
  17. #include "../render/ColorFilter.h"
  18. #include "../render/Colors.h"
  19. #include "../render/IRenderHandler.h"
  20. static const ColorRGBA creatureBlueBorder = { 0, 255, 255, 255 };
  21. static const ColorRGBA creatureGoldBorder = { 255, 255, 0, 255 };
  22. static const ColorRGBA creatureNoBorder = { 0, 0, 0, 0 };
  23. static ColorRGBA genShadow(ui8 alpha)
  24. {
  25. return ColorRGBA(0, 0, 0, alpha);
  26. }
  27. ColorRGBA AnimationControls::getBlueBorder()
  28. {
  29. return creatureBlueBorder;
  30. }
  31. ColorRGBA AnimationControls::getGoldBorder()
  32. {
  33. return creatureGoldBorder;
  34. }
  35. ColorRGBA AnimationControls::getNoBorder()
  36. {
  37. return creatureNoBorder;
  38. }
  39. std::shared_ptr<CreatureAnimation> AnimationControls::getAnimation(const CCreature * creature)
  40. {
  41. auto func = std::bind(&AnimationControls::getCreatureAnimationSpeed, creature, _1, _2);
  42. return std::make_shared<CreatureAnimation>(creature->animDefName, func);
  43. }
  44. float AnimationControls::getAnimationSpeedFactor()
  45. {
  46. // according to testing, H3 ratios between slow/medium/fast might actually be 36/60/100 (x1.666)
  47. // exact value is hard to tell due to large rounding errors
  48. // however we will assume them to be 33/66/100 since these values are better for standard 60 fps displays:
  49. // with these numbers, base frame display duration will be 100/66/33 ms - exactly 6/4/2 frames
  50. return settings["battle"]["speedFactor"].Float();
  51. }
  52. float AnimationControls::getCreatureAnimationSpeed(const CCreature * creature, const CreatureAnimation * anim, ECreatureAnimType type)
  53. {
  54. assert(creature->animation.walkAnimationTime != 0);
  55. assert(creature->animation.attackAnimationTime != 0);
  56. assert(anim->framesInGroup(type) != 0);
  57. // possible new fields for creature format:
  58. //split "Attack time" into "Shoot Time" and "Cast Time"
  59. // base speed for all H3 animations on slow speed is 10 frames per second (or 100ms per frame)
  60. const float baseSpeed = 10.f;
  61. const float speed = baseSpeed * getAnimationSpeedFactor();
  62. switch (type)
  63. {
  64. case ECreatureAnimType::MOVING:
  65. return speed / creature->animation.walkAnimationTime;
  66. case ECreatureAnimType::MOUSEON:
  67. return baseSpeed;
  68. case ECreatureAnimType::HOLDING:
  69. return creature->animation.idleAnimationTime;
  70. case ECreatureAnimType::SHOOT_UP:
  71. case ECreatureAnimType::SHOOT_FRONT:
  72. case ECreatureAnimType::SHOOT_DOWN:
  73. case ECreatureAnimType::SPECIAL_UP:
  74. case ECreatureAnimType::SPECIAL_FRONT:
  75. case ECreatureAnimType::SPECIAL_DOWN:
  76. case ECreatureAnimType::CAST_DOWN:
  77. case ECreatureAnimType::CAST_FRONT:
  78. case ECreatureAnimType::CAST_UP:
  79. return speed / creature->animation.attackAnimationTime;
  80. // as strange as it looks like "attackAnimationTime" does not affects melee attacks
  81. // necessary because length of these animations must be same for all creatures for synchronization
  82. case ECreatureAnimType::ATTACK_UP:
  83. case ECreatureAnimType::ATTACK_FRONT:
  84. case ECreatureAnimType::ATTACK_DOWN:
  85. case ECreatureAnimType::HITTED:
  86. case ECreatureAnimType::DEFENCE:
  87. case ECreatureAnimType::DEATH:
  88. case ECreatureAnimType::DEATH_RANGED:
  89. case ECreatureAnimType::RESURRECTION:
  90. case ECreatureAnimType::GROUP_ATTACK_DOWN:
  91. case ECreatureAnimType::GROUP_ATTACK_FRONT:
  92. case ECreatureAnimType::GROUP_ATTACK_UP:
  93. return speed;
  94. case ECreatureAnimType::TURN_L:
  95. case ECreatureAnimType::TURN_R:
  96. return speed;
  97. case ECreatureAnimType::MOVE_START:
  98. case ECreatureAnimType::MOVE_END:
  99. return speed;
  100. case ECreatureAnimType::DEAD:
  101. case ECreatureAnimType::DEAD_RANGED:
  102. return speed;
  103. default:
  104. return speed;
  105. }
  106. }
  107. float AnimationControls::getProjectileSpeed()
  108. {
  109. // H3 speed: 1250/2500/3750 pixels per second
  110. return static_cast<float>(getAnimationSpeedFactor() * 1250);
  111. }
  112. float AnimationControls::getRayProjectileSpeed()
  113. {
  114. // H3 speed: 4000/8000/12000 pixels per second
  115. return static_cast<float>(getAnimationSpeedFactor() * 4000);
  116. }
  117. float AnimationControls::getCatapultSpeed()
  118. {
  119. // H3 speed: 200/400/600 pixels per second
  120. return static_cast<float>(getAnimationSpeedFactor() * 200);
  121. }
  122. float AnimationControls::getSpellEffectSpeed()
  123. {
  124. // H3 speed: 10/20/30 frames per second
  125. return static_cast<float>(getAnimationSpeedFactor() * 10);
  126. }
  127. float AnimationControls::getMovementRange(const CCreature * creature)
  128. {
  129. // H3 speed: 2/4/6 tiles per second
  130. return static_cast<float>( 2.0 * getAnimationSpeedFactor() / creature->animation.walkAnimationTime);
  131. }
  132. float AnimationControls::getFlightDistance(const CCreature * creature)
  133. {
  134. // Note: for whatever reason, H3 uses "Walk Animation Time" here, even though "Flight Animation Distance" also exists
  135. // H3 speed: 250/500/750 pixels per second
  136. return static_cast<float>( 250.0 * getAnimationSpeedFactor() / creature->animation.walkAnimationTime);
  137. }
  138. float AnimationControls::getFadeInDuration()
  139. {
  140. // H3 speed: 500/250/166 ms
  141. return 0.5f / getAnimationSpeedFactor();
  142. }
  143. float AnimationControls::getObstaclesSpeed()
  144. {
  145. // H3 speed: 20 frames per second, irregardless of speed setting.
  146. return 20.f;
  147. }
  148. ECreatureAnimType CreatureAnimation::getType() const
  149. {
  150. return type;
  151. }
  152. void CreatureAnimation::setType(ECreatureAnimType type)
  153. {
  154. this->type = type;
  155. currentFrame = 0;
  156. once = false;
  157. speed = speedController(this, type);
  158. }
  159. CreatureAnimation::CreatureAnimation(const AnimationPath & name_, TSpeedController controller)
  160. : name(name_),
  161. speed(0.1f),
  162. shadowAlpha(128),
  163. currentFrame(0),
  164. animationEnd(-1),
  165. elapsedTime(0),
  166. type(ECreatureAnimType::HOLDING),
  167. speedController(controller),
  168. once(false)
  169. {
  170. forward = GH.renderHandler().loadAnimation(name_, EImageBlitMode::WITH_SHADOW_AND_SELECTION);
  171. reverse = GH.renderHandler().loadAnimation(name_, EImageBlitMode::WITH_SHADOW_AND_SELECTION);
  172. // if necessary, add one frame into vcmi-only group DEAD
  173. if(forward->size(size_t(ECreatureAnimType::DEAD)) == 0)
  174. {
  175. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), forward->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  176. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), reverse->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  177. }
  178. if(forward->size(size_t(ECreatureAnimType::DEAD_RANGED)) == 0 && forward->size(size_t(ECreatureAnimType::DEATH_RANGED)) != 0)
  179. {
  180. forward->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), forward->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  181. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), reverse->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  182. }
  183. if(forward->size(size_t(ECreatureAnimType::FROZEN)) == 0)
  184. {
  185. forward->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  186. reverse->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  187. }
  188. if(forward->size(size_t(ECreatureAnimType::RESURRECTION)) == 0)
  189. {
  190. for (size_t i = 0; i < forward->size(size_t(ECreatureAnimType::DEATH)); ++i)
  191. {
  192. size_t current = forward->size(size_t(ECreatureAnimType::DEATH)) - 1 - i;
  193. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  194. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  195. }
  196. }
  197. //TODO: get dimensions form CAnimation
  198. auto first = forward->getImage(0, size_t(type), true);
  199. if(!first)
  200. {
  201. fullWidth = 0;
  202. fullHeight = 0;
  203. return;
  204. }
  205. fullWidth = first->width();
  206. fullHeight = first->height();
  207. reverse->verticalFlip();
  208. speed = speedController(this, type);
  209. }
  210. void CreatureAnimation::endAnimation()
  211. {
  212. once = false;
  213. auto copy = onAnimationReset;
  214. onAnimationReset.clear();
  215. copy();
  216. }
  217. bool CreatureAnimation::incrementFrame(float timePassed)
  218. {
  219. elapsedTime += timePassed;
  220. currentFrame += timePassed * speed;
  221. if (animationEnd >= 0)
  222. currentFrame = std::min(currentFrame, animationEnd);
  223. const auto framesNumber = framesInGroup(type);
  224. if(framesNumber <= 0)
  225. {
  226. endAnimation();
  227. }
  228. else if(currentFrame >= float(framesNumber))
  229. {
  230. // just in case of extremely low fps (or insanely high speed)
  231. while(currentFrame >= float(framesNumber))
  232. currentFrame -= framesNumber;
  233. if(once)
  234. setType(ECreatureAnimType::HOLDING);
  235. endAnimation();
  236. return true;
  237. }
  238. return false;
  239. }
  240. void CreatureAnimation::setBorderColor(ColorRGBA palette)
  241. {
  242. border = palette;
  243. }
  244. int CreatureAnimation::getWidth() const
  245. {
  246. return fullWidth;
  247. }
  248. int CreatureAnimation::getHeight() const
  249. {
  250. return fullHeight;
  251. }
  252. float CreatureAnimation::getCurrentFrame() const
  253. {
  254. return currentFrame;
  255. }
  256. void CreatureAnimation::playOnce( ECreatureAnimType type )
  257. {
  258. setType(type);
  259. once = true;
  260. }
  261. inline int getBorderStrength(float time)
  262. {
  263. float borderStrength = fabs(std::round(time) - time) * 2; // generate value in range 0-1
  264. return static_cast<int>(borderStrength * 155 + 100); // scale to 0-255
  265. }
  266. static ColorRGBA genBorderColor(ui8 alpha, const ColorRGBA & base)
  267. {
  268. return ColorRGBA(base.r, base.g, base.b, ui8(base.a * alpha / 256));
  269. }
  270. void CreatureAnimation::nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight)
  271. {
  272. ColorRGBA shadowTest = shifter.shiftColor(genShadow(128));
  273. shadowAlpha = shadowTest.a;
  274. size_t frame = static_cast<size_t>(floor(currentFrame));
  275. std::shared_ptr<IImage> image;
  276. if(facingRight)
  277. image = forward->getImage(frame, size_t(type));
  278. else
  279. image = reverse->getImage(frame, size_t(type));
  280. if(image)
  281. {
  282. if (isIdle())
  283. image->setOverlayColor(genBorderColor(getBorderStrength(elapsedTime), border));
  284. else
  285. image->setOverlayColor(Colors::TRANSPARENCY);
  286. image->adjustPalette(shifter, 0);
  287. canvas.draw(image, pos.topLeft(), Rect(0, 0, pos.w, pos.h));
  288. }
  289. }
  290. void CreatureAnimation::playUntil(size_t frameIndex)
  291. {
  292. animationEnd = frameIndex;
  293. }
  294. int CreatureAnimation::framesInGroup(ECreatureAnimType group) const
  295. {
  296. return static_cast<int>(forward->size(size_t(group)));
  297. }
  298. bool CreatureAnimation::isDead() const
  299. {
  300. return getType() == ECreatureAnimType::DEAD
  301. || getType() == ECreatureAnimType::DEAD_RANGED;
  302. }
  303. bool CreatureAnimation::isDying() const
  304. {
  305. return getType() == ECreatureAnimType::DEATH
  306. || getType() == ECreatureAnimType::DEATH_RANGED;
  307. }
  308. bool CreatureAnimation::isDeadOrDying() const
  309. {
  310. return getType() == ECreatureAnimType::DEAD
  311. || getType() == ECreatureAnimType::DEATH
  312. || getType() == ECreatureAnimType::DEAD_RANGED
  313. || getType() == ECreatureAnimType::DEATH_RANGED;
  314. }
  315. bool CreatureAnimation::isIdle() const
  316. {
  317. return getType() == ECreatureAnimType::HOLDING
  318. || getType() == ECreatureAnimType::MOUSEON;
  319. }
  320. bool CreatureAnimation::isMoving() const
  321. {
  322. return getType() == ECreatureAnimType::MOVE_START
  323. || getType() == ECreatureAnimType::MOVING
  324. || getType() == ECreatureAnimType::MOVE_END
  325. || getType() == ECreatureAnimType::TURN_L
  326. || getType() == ECreatureAnimType::TURN_R;
  327. }
  328. bool CreatureAnimation::isShooting() const
  329. {
  330. return getType() == ECreatureAnimType::SHOOT_UP
  331. || getType() == ECreatureAnimType::SHOOT_FRONT
  332. || getType() == ECreatureAnimType::SHOOT_DOWN;
  333. }