CreatureAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. static const SDL_Color creatureBlueBorder = { 0, 255, 255, 255 };
  16. static const SDL_Color creatureGoldBorder = { 255, 255, 0, 255 };
  17. static const SDL_Color creatureNoBorder = { 0, 0, 0, 0 };
  18. static SDL_Color genShadow(ui8 alpha)
  19. {
  20. return CSDL_Ext::makeColor(0, 0, 0, alpha);
  21. }
  22. SDL_Color AnimationControls::getBlueBorder()
  23. {
  24. return creatureBlueBorder;
  25. }
  26. SDL_Color AnimationControls::getGoldBorder()
  27. {
  28. return creatureGoldBorder;
  29. }
  30. SDL_Color AnimationControls::getNoBorder()
  31. {
  32. return creatureNoBorder;
  33. }
  34. std::shared_ptr<CreatureAnimation> AnimationControls::getAnimation(const CCreature * creature)
  35. {
  36. auto func = std::bind(&AnimationControls::getCreatureAnimationSpeed, creature, _1, _2);
  37. return std::make_shared<CreatureAnimation>(creature->animDefName, func);
  38. }
  39. float AnimationControls::getCreatureAnimationSpeed(const CCreature * creature, const CreatureAnimation * anim, size_t group)
  40. {
  41. ECreatureAnimType::Type type = ECreatureAnimType::Type(group);
  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::CAST_UP:
  63. case ECreatureAnimType::CAST_FRONT:
  64. case ECreatureAnimType::CAST_DOWN:
  65. case ECreatureAnimType::VCMI_CAST_DOWN:
  66. case ECreatureAnimType::VCMI_CAST_FRONT:
  67. case ECreatureAnimType::VCMI_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::VCMI_2HEX_DOWN:
  80. case ECreatureAnimType::VCMI_2HEX_FRONT:
  81. case ECreatureAnimType::VCMI_2HEX_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() * 100);
  99. }
  100. float AnimationControls::getCatapultSpeed()
  101. {
  102. return static_cast<float>(settings["battle"]["animationSpeed"].Float() * 20);
  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. ECreatureAnimType::Type CreatureAnimation::getType() const
  121. {
  122. return type;
  123. }
  124. void CreatureAnimation::setType(ECreatureAnimType::Type type)
  125. {
  126. this->type = type;
  127. currentFrame = 0;
  128. once = false;
  129. play();
  130. }
  131. void CreatureAnimation::shiftColor(const ColorShifter* shifter)
  132. {
  133. SDL_Color shadowTest = shifter->shiftColor(genShadow(128));
  134. shadowAlpha = shadowTest.a;
  135. if(forward)
  136. forward->shiftColor(shifter);
  137. if(reverse)
  138. reverse->shiftColor(shifter);
  139. }
  140. CreatureAnimation::CreatureAnimation(const std::string & name_, TSpeedController controller)
  141. : name(name_),
  142. speed(0.1f),
  143. shadowAlpha(128),
  144. currentFrame(0),
  145. elapsedTime(0),
  146. type(ECreatureAnimType::HOLDING),
  147. border(CSDL_Ext::makeColor(0, 0, 0, 0)),
  148. speedController(controller),
  149. once(false)
  150. {
  151. forward = std::make_shared<CAnimation>(name_);
  152. reverse = std::make_shared<CAnimation>(name_);
  153. //todo: optimize
  154. forward->preload();
  155. reverse->preload();
  156. // if necessary, add one frame into vcmi-only group DEAD
  157. if(forward->size(ECreatureAnimType::DEAD) == 0)
  158. {
  159. forward->duplicateImage(ECreatureAnimType::DEATH, forward->size(ECreatureAnimType::DEATH)-1, ECreatureAnimType::DEAD);
  160. reverse->duplicateImage(ECreatureAnimType::DEATH, reverse->size(ECreatureAnimType::DEATH)-1, ECreatureAnimType::DEAD);
  161. }
  162. if(forward->size(ECreatureAnimType::DEAD_RANGED) == 0 && forward->size(ECreatureAnimType::DEATH_RANGED) != 0)
  163. {
  164. forward->duplicateImage(ECreatureAnimType::DEATH_RANGED, forward->size(ECreatureAnimType::DEATH_RANGED)-1, ECreatureAnimType::DEAD_RANGED);
  165. reverse->duplicateImage(ECreatureAnimType::DEATH_RANGED, reverse->size(ECreatureAnimType::DEATH_RANGED)-1, ECreatureAnimType::DEAD_RANGED);
  166. }
  167. if(forward->size(ECreatureAnimType::RESURRECTION) == 0)
  168. {
  169. for (size_t i = 0; i < forward->size(ECreatureAnimType::DEATH); ++i)
  170. {
  171. size_t current = forward->size(ECreatureAnimType::DEATH) - 1 - i;
  172. forward->duplicateImage(ECreatureAnimType::DEATH, current, ECreatureAnimType::RESURRECTION);
  173. reverse->duplicateImage(ECreatureAnimType::DEATH, current, ECreatureAnimType::RESURRECTION);
  174. }
  175. }
  176. //TODO: get dimensions form CAnimation
  177. auto first = forward->getImage(0, 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 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, bool facingRight)
  271. {
  272. size_t frame = static_cast<size_t>(floor(currentFrame));
  273. std::shared_ptr<IImage> image;
  274. if(facingRight)
  275. image = forward->getImage(frame, type);
  276. else
  277. image = reverse->getImage(frame, type);
  278. if(image)
  279. {
  280. IImage::SpecialPalette SpecialPalette;
  281. genSpecialPalette(SpecialPalette);
  282. image->setSpecialPallete(SpecialPalette);
  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. }