CreatureAnimation.cpp 11 KB

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