CreatureAnimation.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/Canvas.h"
  15. #include "../gui/ColorFilter.h"
  16. static const SDL_Color creatureBlueBorder = { 0, 255, 255, 255 };
  17. static const SDL_Color creatureGoldBorder = { 255, 255, 0, 255 };
  18. static const SDL_Color creatureNoBorder = { 0, 0, 0, 0 };
  19. static SDL_Color genShadow(ui8 alpha)
  20. {
  21. return CSDL_Ext::makeColor(0, 0, 0, alpha);
  22. }
  23. SDL_Color AnimationControls::getBlueBorder()
  24. {
  25. return creatureBlueBorder;
  26. }
  27. SDL_Color AnimationControls::getGoldBorder()
  28. {
  29. return creatureGoldBorder;
  30. }
  31. SDL_Color 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::getCreatureAnimationSpeed(const CCreature * creature, const CreatureAnimation * anim, ECreatureAnimType type)
  41. {
  42. assert(creature->animation.walkAnimationTime != 0);
  43. assert(creature->animation.attackAnimationTime != 0);
  44. assert(anim->framesInGroup(type) != 0);
  45. // possible new fields for creature format:
  46. //split "Attack time" into "Shoot Time" and "Cast Time"
  47. // a lot of arbitrary multipliers, mostly to make animation speed closer to H3
  48. const float baseSpeed = 0.1f;
  49. const float speedMult = static_cast<float>(settings["battle"]["animationSpeed"].Float());
  50. const float speed = baseSpeed / speedMult;
  51. switch (type)
  52. {
  53. case ECreatureAnimType::MOVING:
  54. return static_cast<float>(speed * 2 * creature->animation.walkAnimationTime / anim->framesInGroup(type));
  55. case ECreatureAnimType::MOUSEON:
  56. return baseSpeed;
  57. case ECreatureAnimType::HOLDING:
  58. return static_cast<float>(baseSpeed * creature->animation.idleAnimationTime / anim->framesInGroup(type));
  59. case ECreatureAnimType::SHOOT_UP:
  60. case ECreatureAnimType::SHOOT_FRONT:
  61. case ECreatureAnimType::SHOOT_DOWN:
  62. case ECreatureAnimType::SPECIAL_UP:
  63. case ECreatureAnimType::SPECIAL_FRONT:
  64. case ECreatureAnimType::SPECIAL_DOWN:
  65. case ECreatureAnimType::CAST_DOWN:
  66. case ECreatureAnimType::CAST_FRONT:
  67. case ECreatureAnimType::CAST_UP:
  68. return static_cast<float>(speed * 4 * creature->animation.attackAnimationTime / anim->framesInGroup(type));
  69. // as strange as it looks like "attackAnimationTime" does not affects melee attacks
  70. // necessary because length of these animations must be same for all creatures for synchronization
  71. case ECreatureAnimType::ATTACK_UP:
  72. case ECreatureAnimType::ATTACK_FRONT:
  73. case ECreatureAnimType::ATTACK_DOWN:
  74. case ECreatureAnimType::HITTED:
  75. case ECreatureAnimType::DEFENCE:
  76. case ECreatureAnimType::DEATH:
  77. case ECreatureAnimType::DEATH_RANGED:
  78. case ECreatureAnimType::RESURRECTION:
  79. case ECreatureAnimType::GROUP_ATTACK_DOWN:
  80. case ECreatureAnimType::GROUP_ATTACK_FRONT:
  81. case ECreatureAnimType::GROUP_ATTACK_UP:
  82. return speed * 3 / anim->framesInGroup(type);
  83. case ECreatureAnimType::TURN_L:
  84. case ECreatureAnimType::TURN_R:
  85. return speed / 3;
  86. case ECreatureAnimType::MOVE_START:
  87. case ECreatureAnimType::MOVE_END:
  88. return speed / 3;
  89. case ECreatureAnimType::DEAD:
  90. case ECreatureAnimType::DEAD_RANGED:
  91. return speed;
  92. default:
  93. return speed;
  94. }
  95. }
  96. float AnimationControls::getProjectileSpeed()
  97. {
  98. return static_cast<float>(settings["battle"]["animationSpeed"].Float() * 4000);
  99. }
  100. float AnimationControls::getCatapultSpeed()
  101. {
  102. return static_cast<float>(settings["battle"]["animationSpeed"].Float() * 1000);
  103. }
  104. float AnimationControls::getSpellEffectSpeed()
  105. {
  106. return static_cast<float>(settings["battle"]["animationSpeed"].Float() * 30);
  107. }
  108. float AnimationControls::getMovementDuration(const CCreature * creature)
  109. {
  110. return static_cast<float>(settings["battle"]["animationSpeed"].Float() * 4 / creature->animation.walkAnimationTime);
  111. }
  112. float AnimationControls::getFlightDistance(const CCreature * creature)
  113. {
  114. return static_cast<float>(creature->animation.flightAnimationDistance * 200);
  115. }
  116. float AnimationControls::getFadeInDuration()
  117. {
  118. return 1.0f / settings["battle"]["animationSpeed"].Float();
  119. }
  120. float AnimationControls::getObstaclesSpeed()
  121. {
  122. return 10.0;// does not seems to be affected by animaiton speed settings
  123. }
  124. ECreatureAnimType CreatureAnimation::getType() const
  125. {
  126. return type;
  127. }
  128. void CreatureAnimation::setType(ECreatureAnimType type)
  129. {
  130. this->type = type;
  131. currentFrame = 0;
  132. once = false;
  133. play();
  134. }
  135. CreatureAnimation::CreatureAnimation(const std::string & name_, TSpeedController controller)
  136. : name(name_),
  137. speed(0.1f),
  138. shadowAlpha(128),
  139. currentFrame(0),
  140. elapsedTime(0),
  141. type(ECreatureAnimType::HOLDING),
  142. border(CSDL_Ext::makeColor(0, 0, 0, 0)),
  143. speedController(controller),
  144. once(false)
  145. {
  146. forward = std::make_shared<CAnimation>(name_);
  147. reverse = std::make_shared<CAnimation>(name_);
  148. //todo: optimize
  149. forward->preload();
  150. reverse->preload();
  151. // if necessary, add one frame into vcmi-only group DEAD
  152. if(forward->size(size_t(ECreatureAnimType::DEAD)) == 0)
  153. {
  154. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), forward->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  155. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), reverse->size(size_t(ECreatureAnimType::DEATH))-1, size_t(ECreatureAnimType::DEAD));
  156. }
  157. if(forward->size(size_t(ECreatureAnimType::DEAD_RANGED)) == 0 && forward->size(size_t(ECreatureAnimType::DEATH_RANGED)) != 0)
  158. {
  159. forward->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), forward->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  160. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH_RANGED), reverse->size(size_t(ECreatureAnimType::DEATH_RANGED))-1, size_t(ECreatureAnimType::DEAD_RANGED));
  161. }
  162. if(forward->size(size_t(ECreatureAnimType::FROZEN)) == 0)
  163. {
  164. forward->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  165. reverse->duplicateImage(size_t(ECreatureAnimType::HOLDING), 0, size_t(ECreatureAnimType::FROZEN));
  166. }
  167. if(forward->size(size_t(ECreatureAnimType::RESURRECTION)) == 0)
  168. {
  169. for (size_t i = 0; i < forward->size(size_t(ECreatureAnimType::DEATH)); ++i)
  170. {
  171. size_t current = forward->size(size_t(ECreatureAnimType::DEATH)) - 1 - i;
  172. forward->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  173. reverse->duplicateImage(size_t(ECreatureAnimType::DEATH), current, size_t(ECreatureAnimType::RESURRECTION));
  174. }
  175. }
  176. //TODO: get dimensions form CAnimation
  177. auto first = forward->getImage(0, size_t(type), true);
  178. if(!first)
  179. {
  180. fullWidth = 0;
  181. fullHeight = 0;
  182. return;
  183. }
  184. fullWidth = first->width();
  185. fullHeight = first->height();
  186. reverse->verticalFlip();
  187. play();
  188. }
  189. void CreatureAnimation::endAnimation()
  190. {
  191. once = false;
  192. auto copy = onAnimationReset;
  193. onAnimationReset.clear();
  194. copy();
  195. }
  196. bool CreatureAnimation::incrementFrame(float timePassed)
  197. {
  198. elapsedTime += timePassed;
  199. currentFrame += timePassed * speed;
  200. const auto framesNumber = framesInGroup(type);
  201. if(framesNumber <= 0)
  202. {
  203. endAnimation();
  204. }
  205. else if(currentFrame >= float(framesNumber))
  206. {
  207. // just in case of extremely low fps (or insanely high speed)
  208. while(currentFrame >= float(framesNumber))
  209. currentFrame -= framesNumber;
  210. if(once)
  211. setType(ECreatureAnimType::HOLDING);
  212. endAnimation();
  213. return true;
  214. }
  215. return false;
  216. }
  217. void CreatureAnimation::setBorderColor(SDL_Color palette)
  218. {
  219. border = palette;
  220. }
  221. int CreatureAnimation::getWidth() const
  222. {
  223. return fullWidth;
  224. }
  225. int CreatureAnimation::getHeight() const
  226. {
  227. return fullHeight;
  228. }
  229. float CreatureAnimation::getCurrentFrame() const
  230. {
  231. return currentFrame;
  232. }
  233. void CreatureAnimation::playOnce( ECreatureAnimType type )
  234. {
  235. setType(type);
  236. once = true;
  237. }
  238. inline int getBorderStrength(float time)
  239. {
  240. float borderStrength = fabs(vstd::round(time) - time) * 2; // generate value in range 0-1
  241. return static_cast<int>(borderStrength * 155 + 100); // scale to 0-255
  242. }
  243. static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base)
  244. {
  245. return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.a * alpha / 256));
  246. }
  247. static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
  248. {
  249. return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
  250. }
  251. static SDL_Color addColors(const SDL_Color & base, const SDL_Color & over)
  252. {
  253. return CSDL_Ext::makeColor(
  254. mixChannels(over.r, base.r, over.a, base.a),
  255. mixChannels(over.g, base.g, over.a, base.a),
  256. mixChannels(over.b, base.b, over.a, base.a),
  257. ui8(over.a + base.a * (255 - over.a) / 256)
  258. );
  259. }
  260. void CreatureAnimation::genSpecialPalette(IImage::SpecialPalette & target)
  261. {
  262. target[0] = genShadow(shadowAlpha / 2);
  263. target[1] = genShadow(shadowAlpha / 2);
  264. target[2] = genShadow(shadowAlpha);
  265. target[3] = genShadow(shadowAlpha);
  266. target[4] = genBorderColor(getBorderStrength(elapsedTime), border);
  267. target[5] = addColors(genShadow(shadowAlpha), genBorderColor(getBorderStrength(elapsedTime), border));
  268. target[6] = addColors(genShadow(shadowAlpha / 2), genBorderColor(getBorderStrength(elapsedTime), border));
  269. }
  270. void CreatureAnimation::nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight)
  271. {
  272. SDL_Color 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. IImage::SpecialPalette SpecialPalette;
  283. genSpecialPalette(SpecialPalette);
  284. image->setSpecialPallete(SpecialPalette);
  285. image->adjustPalette(shifter);
  286. canvas.draw(image, pos.topLeft(), Rect(0, 0, pos.w, pos.h));
  287. }
  288. }
  289. int CreatureAnimation::framesInGroup(ECreatureAnimType group) const
  290. {
  291. return static_cast<int>(forward->size(size_t(group)));
  292. }
  293. bool CreatureAnimation::isDead() const
  294. {
  295. return getType() == ECreatureAnimType::DEAD
  296. || getType() == ECreatureAnimType::DEAD_RANGED;
  297. }
  298. bool CreatureAnimation::isDying() const
  299. {
  300. return getType() == ECreatureAnimType::DEATH
  301. || getType() == ECreatureAnimType::DEATH_RANGED;
  302. }
  303. bool CreatureAnimation::isDeadOrDying() const
  304. {
  305. return getType() == ECreatureAnimType::DEAD
  306. || getType() == ECreatureAnimType::DEATH
  307. || getType() == ECreatureAnimType::DEAD_RANGED
  308. || getType() == ECreatureAnimType::DEATH_RANGED;
  309. }
  310. bool CreatureAnimation::isIdle() const
  311. {
  312. return getType() == ECreatureAnimType::HOLDING
  313. || getType() == ECreatureAnimType::MOUSEON;
  314. }
  315. bool CreatureAnimation::isMoving() const
  316. {
  317. return getType() == ECreatureAnimType::MOVE_START
  318. || getType() == ECreatureAnimType::MOVING
  319. || getType() == ECreatureAnimType::MOVE_END
  320. || getType() == ECreatureAnimType::TURN_L
  321. || getType() == ECreatureAnimType::TURN_R;
  322. }
  323. bool CreatureAnimation::isShooting() const
  324. {
  325. return getType() == ECreatureAnimType::SHOOT_UP
  326. || getType() == ECreatureAnimType::SHOOT_FRONT
  327. || getType() == ECreatureAnimType::SHOOT_DOWN;
  328. }
  329. void CreatureAnimation::pause()
  330. {
  331. speed = 0;
  332. }
  333. void CreatureAnimation::play()
  334. {
  335. //logAnim->trace("Play %s group %d at %d:%d", name, static_cast<int>(getType()), pos.x, pos.y);
  336. speed = 0;
  337. if(speedController(this, type) != 0)
  338. speed = 1 / speedController(this, type);
  339. }