CreatureAnimation.cpp 11 KB

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