CreatureAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 (forward->size(size_t(ECreatureAnimType::DEATH)) == 0)
  168. throw std::runtime_error("Animation '" + name_.getOriginalName() + "' has empty death animation!");
  169. if (forward->size(size_t(ECreatureAnimType::HOLDING)) == 0)
  170. throw std::runtime_error("Animation '" + name_.getOriginalName() + "' has empty holding animation!");
  171. // if necessary, add one frame into vcmi-only group DEAD
  172. if(forward->size(size_t(ECreatureAnimType::DEAD)) == 0)
  173. {
  174. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), forward->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  175. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), reverse->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  176. }
  177. if(forward->size(size_t(ECreatureAnimType::DEAD_RANGED)) == 0 && forward->size(size_t(ECreatureAnimType::DEATH_RANGED)) != 0)
  178. {
  179. forward->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), forward->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  180. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), reverse->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  181. }
  182. if(forward->size(size_t(ECreatureAnimType::FROZEN)) == 0)
  183. {
  184. forward->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  185. reverse->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  186. }
  187. if(forward->size(size_t(ECreatureAnimType::RESURRECTION)) == 0)
  188. {
  189. for (size_t i = 0; i < forward->size(size_t(ECreatureAnimType::DEATH)); ++i)
  190. {
  191. size_t current = forward->size(size_t(ECreatureAnimType::DEATH)) - 1 - i;
  192. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  193. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  194. }
  195. }
  196. //TODO: get dimensions form CAnimation
  197. auto first = forward->getImage(0, size_t(type), true);
  198. if(!first)
  199. {
  200. fullWidth = 0;
  201. fullHeight = 0;
  202. return;
  203. }
  204. fullWidth = first->width();
  205. fullHeight = first->height();
  206. reverse->verticalFlip();
  207. speed = speedController(this, type);
  208. }
  209. void CreatureAnimation::endAnimation()
  210. {
  211. once = false;
  212. auto copy = onAnimationReset;
  213. onAnimationReset.clear();
  214. copy();
  215. }
  216. bool CreatureAnimation::incrementFrame(float timePassed)
  217. {
  218. elapsedTime += timePassed;
  219. currentFrame += timePassed * speed;
  220. if (animationEnd >= 0)
  221. currentFrame = std::min(currentFrame, animationEnd);
  222. const auto framesNumber = framesInGroup(type);
  223. if(framesNumber <= 0)
  224. {
  225. endAnimation();
  226. }
  227. else if(currentFrame >= float(framesNumber))
  228. {
  229. // just in case of extremely low fps (or insanely high speed)
  230. while(currentFrame >= float(framesNumber))
  231. currentFrame -= framesNumber;
  232. if(once)
  233. setType(ECreatureAnimType::HOLDING);
  234. endAnimation();
  235. return true;
  236. }
  237. return false;
  238. }
  239. void CreatureAnimation::setBorderColor(ColorRGBA palette)
  240. {
  241. border = palette;
  242. }
  243. int CreatureAnimation::getWidth() const
  244. {
  245. return fullWidth;
  246. }
  247. int CreatureAnimation::getHeight() const
  248. {
  249. return fullHeight;
  250. }
  251. float CreatureAnimation::getCurrentFrame() const
  252. {
  253. return currentFrame;
  254. }
  255. void CreatureAnimation::playOnce( ECreatureAnimType type )
  256. {
  257. setType(type);
  258. once = true;
  259. }
  260. inline int getBorderStrength(float time)
  261. {
  262. float borderStrength = fabs(std::round(time) - time) * 2; // generate value in range 0-1
  263. return static_cast<int>(borderStrength * 155 + 100); // scale to 0-255
  264. }
  265. static ColorRGBA genBorderColor(ui8 alpha, const ColorRGBA & base)
  266. {
  267. return ColorRGBA(base.r, base.g, base.b, ui8(base.a * alpha / 256));
  268. }
  269. void CreatureAnimation::nextFrame(Canvas & canvas, const ColorRGBA & effectColor, uint8_t transparency, bool facingRight)
  270. {
  271. size_t frame = static_cast<size_t>(floor(currentFrame));
  272. std::shared_ptr<IImage> image;
  273. if(facingRight)
  274. image = forward->getImage(frame, size_t(type));
  275. else
  276. image = reverse->getImage(frame, size_t(type));
  277. if(image)
  278. {
  279. if (isIdle())
  280. image->setOverlayColor(genBorderColor(getBorderStrength(elapsedTime), border));
  281. else
  282. image->setOverlayColor(Colors::TRANSPARENCY);
  283. image->setEffectColor(effectColor);
  284. image->setAlpha(transparency);
  285. canvas.draw(image, pos.topLeft(), Rect(0, 0, pos.w, pos.h));
  286. }
  287. }
  288. void CreatureAnimation::playUntil(size_t frameIndex)
  289. {
  290. animationEnd = frameIndex;
  291. }
  292. int CreatureAnimation::framesInGroup(ECreatureAnimType group) const
  293. {
  294. return static_cast<int>(forward->size(size_t(group)));
  295. }
  296. bool CreatureAnimation::isDead() const
  297. {
  298. return getType() == ECreatureAnimType::DEAD
  299. || getType() == ECreatureAnimType::DEAD_RANGED;
  300. }
  301. bool CreatureAnimation::isDying() const
  302. {
  303. return getType() == ECreatureAnimType::DEATH
  304. || getType() == ECreatureAnimType::DEATH_RANGED;
  305. }
  306. bool CreatureAnimation::isDeadOrDying() const
  307. {
  308. return getType() == ECreatureAnimType::DEAD
  309. || getType() == ECreatureAnimType::DEATH
  310. || getType() == ECreatureAnimType::DEAD_RANGED
  311. || getType() == ECreatureAnimType::DEATH_RANGED;
  312. }
  313. bool CreatureAnimation::isIdle() const
  314. {
  315. return getType() == ECreatureAnimType::HOLDING
  316. || getType() == ECreatureAnimType::MOUSEON;
  317. }
  318. bool CreatureAnimation::isMoving() const
  319. {
  320. return getType() == ECreatureAnimType::MOVE_START
  321. || getType() == ECreatureAnimType::MOVING
  322. || getType() == ECreatureAnimType::MOVE_END
  323. || getType() == ECreatureAnimType::TURN_L
  324. || getType() == ECreatureAnimType::TURN_R;
  325. }
  326. bool CreatureAnimation::isShooting() const
  327. {
  328. return getType() == ECreatureAnimType::SHOOT_UP
  329. || getType() == ECreatureAnimType::SHOOT_FRONT
  330. || getType() == ECreatureAnimType::SHOOT_DOWN;
  331. }