CreatureAnimation.cpp 12 KB

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