CreatureAnimation.cpp 12 KB

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