CCreatureAnimation.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. 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. CCreatureAnimation * AnimationControls::getAnimation(const CCreature * creature)
  31. {
  32. auto func = std::bind(&AnimationControls::getCreatureAnimationSpeed, creature, _1, _2);
  33. return new CCreatureAnimation(creature->animDefName, func);
  34. }
  35. float AnimationControls::getCreatureAnimationSpeed(const CCreature * creature, const CCreatureAnimation * anim, size_t group)
  36. {
  37. CCreatureAnim::EAnimType type = CCreatureAnim::EAnimType(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.1;
  45. const float speedMult = settings["battle"]["animationSpeed"].Float();
  46. const float speed = baseSpeed / speedMult;
  47. switch (type)
  48. {
  49. case CCreatureAnim::MOVING:
  50. return speed * 2 * creature->animation.walkAnimationTime / anim->framesInGroup(type);
  51. case CCreatureAnim::MOUSEON:
  52. return baseSpeed;
  53. case CCreatureAnim::HOLDING:
  54. return baseSpeed * creature->animation.idleAnimationTime / anim->framesInGroup(type);
  55. case CCreatureAnim::SHOOT_UP:
  56. case CCreatureAnim::SHOOT_FRONT:
  57. case CCreatureAnim::SHOOT_DOWN:
  58. case CCreatureAnim::CAST_UP:
  59. case CCreatureAnim::CAST_FRONT:
  60. case CCreatureAnim::CAST_DOWN:
  61. case CCreatureAnim::VCMI_CAST_DOWN:
  62. case CCreatureAnim::VCMI_CAST_FRONT:
  63. case CCreatureAnim::VCMI_CAST_UP:
  64. return 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 CCreatureAnim::ATTACK_UP:
  68. case CCreatureAnim::ATTACK_FRONT:
  69. case CCreatureAnim::ATTACK_DOWN:
  70. case CCreatureAnim::HITTED:
  71. case CCreatureAnim::DEFENCE:
  72. case CCreatureAnim::DEATH:
  73. case CCreatureAnim::DEATH_RANGED:
  74. case CCreatureAnim::VCMI_2HEX_DOWN:
  75. case CCreatureAnim::VCMI_2HEX_FRONT:
  76. case CCreatureAnim::VCMI_2HEX_UP:
  77. return speed * 3 / anim->framesInGroup(type);
  78. case CCreatureAnim::TURN_L:
  79. case CCreatureAnim::TURN_R:
  80. return speed / 3;
  81. case CCreatureAnim::MOVE_START:
  82. case CCreatureAnim::MOVE_END:
  83. return speed / 3;
  84. case CCreatureAnim::DEAD:
  85. case CCreatureAnim::DEAD_RANGED:
  86. return speed;
  87. default:
  88. return speed;
  89. }
  90. }
  91. float AnimationControls::getProjectileSpeed()
  92. {
  93. return settings["battle"]["animationSpeed"].Float() * 100;
  94. }
  95. float AnimationControls::getSpellEffectSpeed()
  96. {
  97. return settings["battle"]["animationSpeed"].Float() * 60;
  98. }
  99. float AnimationControls::getMovementDuration(const CCreature * creature)
  100. {
  101. return settings["battle"]["animationSpeed"].Float() * 4 / creature->animation.walkAnimationTime;
  102. }
  103. float AnimationControls::getFlightDistance(const CCreature * creature)
  104. {
  105. return creature->animation.flightAnimationDistance * 200;
  106. }
  107. CCreatureAnim::EAnimType CCreatureAnimation::getType() const
  108. {
  109. return type;
  110. }
  111. void CCreatureAnimation::setType(CCreatureAnim::EAnimType type)
  112. {
  113. this->type = type;
  114. currentFrame = 0;
  115. once = false;
  116. play();
  117. }
  118. CCreatureAnimation::CCreatureAnimation(const std::string & name_, TSpeedController controller)
  119. : name(name_),
  120. speed(0.1),
  121. currentFrame(0),
  122. elapsedTime(0),
  123. type(CCreatureAnim::HOLDING),
  124. border(CSDL_Ext::makeColor(0, 0, 0, 0)),
  125. speedController(controller),
  126. once(false)
  127. {
  128. forward = std::make_shared<CAnimation>(name_);
  129. reverse = std::make_shared<CAnimation>(name_);
  130. //todo: optimize
  131. forward->preload();
  132. reverse->preload();
  133. // if necessary, add one frame into vcmi-only group DEAD
  134. if(forward->size(CCreatureAnim::DEAD) == 0)
  135. {
  136. forward->duplicateImage(CCreatureAnim::DEATH, forward->size(CCreatureAnim::DEATH)-1, CCreatureAnim::DEAD);
  137. reverse->duplicateImage(CCreatureAnim::DEATH, reverse->size(CCreatureAnim::DEATH)-1, CCreatureAnim::DEAD);
  138. }
  139. if(forward->size(CCreatureAnim::DEAD_RANGED) == 0 && forward->size(CCreatureAnim::DEATH_RANGED) != 0)
  140. {
  141. forward->duplicateImage(CCreatureAnim::DEATH_RANGED, forward->size(CCreatureAnim::DEATH_RANGED)-1, CCreatureAnim::DEAD_RANGED);
  142. reverse->duplicateImage(CCreatureAnim::DEATH_RANGED, reverse->size(CCreatureAnim::DEATH_RANGED)-1, CCreatureAnim::DEAD_RANGED);
  143. }
  144. //TODO: get dimensions form CAnimation
  145. auto first = forward->getImage(0, type, true);
  146. if(!first)
  147. {
  148. fullWidth = 0;
  149. fullHeight = 0;
  150. return;
  151. }
  152. fullWidth = first->width();
  153. fullHeight = first->height();
  154. reverse->verticalFlip();
  155. play();
  156. }
  157. void CCreatureAnimation::endAnimation()
  158. {
  159. once = false;
  160. auto copy = onAnimationReset;
  161. onAnimationReset.clear();
  162. copy();
  163. }
  164. bool CCreatureAnimation::incrementFrame(float timePassed)
  165. {
  166. elapsedTime += timePassed;
  167. currentFrame += timePassed * speed;
  168. if (currentFrame >= float(framesInGroup(type)))
  169. {
  170. // just in case of extremely low fps (or insanely high speed)
  171. while (currentFrame >= float(framesInGroup(type)))
  172. currentFrame -= framesInGroup(type);
  173. if (once)
  174. setType(CCreatureAnim::HOLDING);
  175. endAnimation();
  176. return true;
  177. }
  178. return false;
  179. }
  180. void CCreatureAnimation::setBorderColor(SDL_Color palette)
  181. {
  182. border = palette;
  183. }
  184. int CCreatureAnimation::getWidth() const
  185. {
  186. return fullWidth;
  187. }
  188. int CCreatureAnimation::getHeight() const
  189. {
  190. return fullHeight;
  191. }
  192. float CCreatureAnimation::getCurrentFrame() const
  193. {
  194. return currentFrame;
  195. }
  196. void CCreatureAnimation::playOnce( CCreatureAnim::EAnimType type )
  197. {
  198. setType(type);
  199. once = true;
  200. }
  201. inline int getBorderStrength(float time)
  202. {
  203. float borderStrength = fabs(vstd::round(time) - time) * 2; // generate value in range 0-1
  204. return borderStrength * 155 + 100; // scale to 0-255
  205. }
  206. static SDL_Color genShadow(ui8 alpha)
  207. {
  208. return CSDL_Ext::makeColor(0, 0, 0, alpha);
  209. }
  210. static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base)
  211. {
  212. return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.a * alpha / 256));
  213. }
  214. static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
  215. {
  216. return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
  217. }
  218. static SDL_Color addColors(const SDL_Color & base, const SDL_Color & over)
  219. {
  220. return CSDL_Ext::makeColor(
  221. mixChannels(over.r, base.r, over.a, base.a),
  222. mixChannels(over.g, base.g, over.a, base.a),
  223. mixChannels(over.b, base.b, over.a, base.a),
  224. ui8(over.a + base.a * (255 - over.a) / 256)
  225. );
  226. }
  227. void CCreatureAnimation::genBorderPalette(IImage::BorderPallete & target)
  228. {
  229. target[0] = genBorderColor(getBorderStrength(elapsedTime), border);
  230. target[1] = addColors(genShadow(128), genBorderColor(getBorderStrength(elapsedTime), border));
  231. target[2] = addColors(genShadow(64), genBorderColor(getBorderStrength(elapsedTime), border));
  232. }
  233. void CCreatureAnimation::nextFrame(SDL_Surface * dest, bool attacker)
  234. {
  235. size_t frame = floor(currentFrame);
  236. IImage * image = nullptr;
  237. if(attacker)
  238. image = forward->getImage(frame, type);
  239. else
  240. image = reverse->getImage(frame, type);
  241. if(image)
  242. {
  243. IImage::BorderPallete borderPallete;
  244. genBorderPalette(borderPallete);
  245. image->setBorderPallete(borderPallete);
  246. image->draw(dest, pos.x, pos.y);
  247. }
  248. }
  249. int CCreatureAnimation::framesInGroup(CCreatureAnim::EAnimType group) const
  250. {
  251. return forward->size(group);
  252. }
  253. bool CCreatureAnimation::isDead() const
  254. {
  255. return getType() == CCreatureAnim::DEAD
  256. || getType() == CCreatureAnim::DEATH
  257. || getType() == CCreatureAnim::DEAD_RANGED
  258. || getType() == CCreatureAnim::DEATH_RANGED;
  259. }
  260. bool CCreatureAnimation::isIdle() const
  261. {
  262. return getType() == CCreatureAnim::HOLDING
  263. || getType() == CCreatureAnim::MOUSEON;
  264. }
  265. bool CCreatureAnimation::isMoving() const
  266. {
  267. return getType() == CCreatureAnim::MOVE_START
  268. || getType() == CCreatureAnim::MOVING
  269. || getType() == CCreatureAnim::MOVE_END;
  270. }
  271. bool CCreatureAnimation::isShooting() const
  272. {
  273. return getType() == CCreatureAnim::SHOOT_UP
  274. || getType() == CCreatureAnim::SHOOT_FRONT
  275. || getType() == CCreatureAnim::SHOOT_DOWN;
  276. }
  277. void CCreatureAnimation::pause()
  278. {
  279. speed = 0;
  280. }
  281. void CCreatureAnimation::play()
  282. {
  283. //logAnim->trace("Play %s group %d at %d:%d", name, static_cast<int>(getType()), pos.x, pos.y);
  284. speed = 0;
  285. if(speedController(this, type) != 0)
  286. speed = 1 / speedController(this, type);
  287. }