CCreatureAnimation.cpp 9.8 KB

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