CreatureAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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, size_t group)
  41. {
  42. ECreatureAnimType::Type type = ECreatureAnimType::Type(group);
  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() * 100);
  100. }
  101. float AnimationControls::getCatapultSpeed()
  102. {
  103. return static_cast<float>(settings["battle"]["animationSpeed"].Float() * 20);
  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. ECreatureAnimType::Type CreatureAnimation::getType() const
  122. {
  123. return type;
  124. }
  125. void CreatureAnimation::setType(ECreatureAnimType::Type type)
  126. {
  127. this->type = type;
  128. currentFrame = 0;
  129. once = false;
  130. play();
  131. }
  132. CreatureAnimation::CreatureAnimation(const std::string & name_, TSpeedController controller)
  133. : name(name_),
  134. speed(0.1f),
  135. shadowAlpha(128),
  136. currentFrame(0),
  137. elapsedTime(0),
  138. type(ECreatureAnimType::HOLDING),
  139. border(CSDL_Ext::makeColor(0, 0, 0, 0)),
  140. speedController(controller),
  141. once(false)
  142. {
  143. forward = std::make_shared<CAnimation>(name_);
  144. reverse = std::make_shared<CAnimation>(name_);
  145. //todo: optimize
  146. forward->preload();
  147. reverse->preload();
  148. // if necessary, add one frame into vcmi-only group DEAD
  149. if(forward->size(ECreatureAnimType::DEAD) == 0)
  150. {
  151. forward->duplicateImage(ECreatureAnimType::DEATH, forward->size(ECreatureAnimType::DEATH)-1, ECreatureAnimType::DEAD);
  152. reverse->duplicateImage(ECreatureAnimType::DEATH, reverse->size(ECreatureAnimType::DEATH)-1, ECreatureAnimType::DEAD);
  153. }
  154. if(forward->size(ECreatureAnimType::DEAD_RANGED) == 0 && forward->size(ECreatureAnimType::DEATH_RANGED) != 0)
  155. {
  156. forward->duplicateImage(ECreatureAnimType::DEATH_RANGED, forward->size(ECreatureAnimType::DEATH_RANGED)-1, ECreatureAnimType::DEAD_RANGED);
  157. reverse->duplicateImage(ECreatureAnimType::DEATH_RANGED, reverse->size(ECreatureAnimType::DEATH_RANGED)-1, ECreatureAnimType::DEAD_RANGED);
  158. }
  159. if(forward->size(ECreatureAnimType::FROZEN) == 0)
  160. {
  161. forward->duplicateImage(ECreatureAnimType::HOLDING, 0, ECreatureAnimType::FROZEN);
  162. reverse->duplicateImage(ECreatureAnimType::HOLDING, 0, ECreatureAnimType::FROZEN);
  163. }
  164. if(forward->size(ECreatureAnimType::RESURRECTION) == 0)
  165. {
  166. for (size_t i = 0; i < forward->size(ECreatureAnimType::DEATH); ++i)
  167. {
  168. size_t current = forward->size(ECreatureAnimType::DEATH) - 1 - i;
  169. forward->duplicateImage(ECreatureAnimType::DEATH, current, ECreatureAnimType::RESURRECTION);
  170. reverse->duplicateImage(ECreatureAnimType::DEATH, current, ECreatureAnimType::RESURRECTION);
  171. }
  172. }
  173. //TODO: get dimensions form CAnimation
  174. auto first = forward->getImage(0, type, true);
  175. if(!first)
  176. {
  177. fullWidth = 0;
  178. fullHeight = 0;
  179. return;
  180. }
  181. fullWidth = first->width();
  182. fullHeight = first->height();
  183. reverse->verticalFlip();
  184. play();
  185. }
  186. void CreatureAnimation::endAnimation()
  187. {
  188. once = false;
  189. auto copy = onAnimationReset;
  190. onAnimationReset.clear();
  191. copy();
  192. }
  193. bool CreatureAnimation::incrementFrame(float timePassed)
  194. {
  195. elapsedTime += timePassed;
  196. currentFrame += timePassed * speed;
  197. const auto framesNumber = framesInGroup(type);
  198. if(framesNumber <= 0)
  199. {
  200. endAnimation();
  201. }
  202. else if(currentFrame >= float(framesNumber))
  203. {
  204. // just in case of extremely low fps (or insanely high speed)
  205. while(currentFrame >= float(framesNumber))
  206. currentFrame -= framesNumber;
  207. if(once)
  208. setType(ECreatureAnimType::HOLDING);
  209. endAnimation();
  210. return true;
  211. }
  212. return false;
  213. }
  214. void CreatureAnimation::setBorderColor(SDL_Color palette)
  215. {
  216. border = palette;
  217. }
  218. int CreatureAnimation::getWidth() const
  219. {
  220. return fullWidth;
  221. }
  222. int CreatureAnimation::getHeight() const
  223. {
  224. return fullHeight;
  225. }
  226. float CreatureAnimation::getCurrentFrame() const
  227. {
  228. return currentFrame;
  229. }
  230. void CreatureAnimation::playOnce( ECreatureAnimType::Type type )
  231. {
  232. setType(type);
  233. once = true;
  234. }
  235. inline int getBorderStrength(float time)
  236. {
  237. float borderStrength = fabs(vstd::round(time) - time) * 2; // generate value in range 0-1
  238. return static_cast<int>(borderStrength * 155 + 100); // scale to 0-255
  239. }
  240. static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base)
  241. {
  242. return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.a * alpha / 256));
  243. }
  244. static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
  245. {
  246. return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
  247. }
  248. static SDL_Color addColors(const SDL_Color & base, const SDL_Color & over)
  249. {
  250. return CSDL_Ext::makeColor(
  251. mixChannels(over.r, base.r, over.a, base.a),
  252. mixChannels(over.g, base.g, over.a, base.a),
  253. mixChannels(over.b, base.b, over.a, base.a),
  254. ui8(over.a + base.a * (255 - over.a) / 256)
  255. );
  256. }
  257. void CreatureAnimation::genSpecialPalette(IImage::SpecialPalette & target)
  258. {
  259. target[0] = genShadow(shadowAlpha / 2);
  260. target[1] = genShadow(shadowAlpha / 2);
  261. target[2] = genShadow(shadowAlpha);
  262. target[3] = genShadow(shadowAlpha);
  263. target[4] = genBorderColor(getBorderStrength(elapsedTime), border);
  264. target[5] = addColors(genShadow(shadowAlpha), genBorderColor(getBorderStrength(elapsedTime), border));
  265. target[6] = addColors(genShadow(shadowAlpha / 2), genBorderColor(getBorderStrength(elapsedTime), border));
  266. }
  267. void CreatureAnimation::nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight)
  268. {
  269. SDL_Color shadowTest = shifter.shiftColor(genShadow(128));
  270. shadowAlpha = shadowTest.a;
  271. size_t frame = static_cast<size_t>(floor(currentFrame));
  272. std::shared_ptr<IImage> image;
  273. if(facingRight)
  274. image = forward->getImage(frame, type);
  275. else
  276. image = reverse->getImage(frame, type);
  277. if(image)
  278. {
  279. IImage::SpecialPalette SpecialPalette;
  280. genSpecialPalette(SpecialPalette);
  281. image->setSpecialPallete(SpecialPalette);
  282. image->adjustPalette(shifter);
  283. canvas.draw(image, pos.topLeft(), Rect(0, 0, pos.w, pos.h));
  284. }
  285. }
  286. int CreatureAnimation::framesInGroup(ECreatureAnimType::Type group) const
  287. {
  288. return static_cast<int>(forward->size(group));
  289. }
  290. bool CreatureAnimation::isDead() const
  291. {
  292. return getType() == ECreatureAnimType::DEAD
  293. || getType() == ECreatureAnimType::DEAD_RANGED;
  294. }
  295. bool CreatureAnimation::isDying() const
  296. {
  297. return getType() == ECreatureAnimType::DEATH
  298. || getType() == ECreatureAnimType::DEATH_RANGED;
  299. }
  300. bool CreatureAnimation::isDeadOrDying() const
  301. {
  302. return getType() == ECreatureAnimType::DEAD
  303. || getType() == ECreatureAnimType::DEATH
  304. || getType() == ECreatureAnimType::DEAD_RANGED
  305. || getType() == ECreatureAnimType::DEATH_RANGED;
  306. }
  307. bool CreatureAnimation::isIdle() const
  308. {
  309. return getType() == ECreatureAnimType::HOLDING
  310. || getType() == ECreatureAnimType::MOUSEON;
  311. }
  312. bool CreatureAnimation::isMoving() const
  313. {
  314. return getType() == ECreatureAnimType::MOVE_START
  315. || getType() == ECreatureAnimType::MOVING
  316. || getType() == ECreatureAnimType::MOVE_END
  317. || getType() == ECreatureAnimType::TURN_L
  318. || getType() == ECreatureAnimType::TURN_R;
  319. }
  320. bool CreatureAnimation::isShooting() const
  321. {
  322. return getType() == ECreatureAnimType::SHOOT_UP
  323. || getType() == ECreatureAnimType::SHOOT_FRONT
  324. || getType() == ECreatureAnimType::SHOOT_DOWN;
  325. }
  326. void CreatureAnimation::pause()
  327. {
  328. speed = 0;
  329. }
  330. void CreatureAnimation::play()
  331. {
  332. //logAnim->trace("Play %s group %d at %d:%d", name, static_cast<int>(getType()), pos.x, pos.y);
  333. speed = 0;
  334. if(speedController(this, type) != 0)
  335. speed = 1 / speedController(this, type);
  336. }