BattleAnimationClasses.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * BattleAnimationClasses.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 "BattleAnimationClasses.h"
  12. #include "BattleInterface.h"
  13. #include "BattleInterfaceClasses.h"
  14. #include "BattleProjectileController.h"
  15. #include "BattleSiegeController.h"
  16. #include "BattleFieldController.h"
  17. #include "BattleEffectsController.h"
  18. #include "BattleStacksController.h"
  19. #include "CreatureAnimation.h"
  20. #include "../CGameInfo.h"
  21. #include "../CMusicHandler.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../gui/CCursorHandler.h"
  24. #include "../gui/CGuiHandler.h"
  25. #include "../gui/SDL_Extensions.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CStack.h"
  28. BattleAnimation::BattleAnimation(BattleInterface & owner)
  29. : owner(owner),
  30. ID(owner.stacksController->animIDhelper++),
  31. initialized(false)
  32. {
  33. logAnim->trace("Animation #%d created", ID);
  34. }
  35. bool BattleAnimation::tryInitialize()
  36. {
  37. assert(!initialized);
  38. if ( init() )
  39. {
  40. initialized = true;
  41. return true;
  42. }
  43. return false;
  44. }
  45. bool BattleAnimation::isInitialized()
  46. {
  47. return initialized;
  48. }
  49. BattleAnimation::~BattleAnimation()
  50. {
  51. logAnim->trace("Animation #%d ended, type is %s", ID, typeid(this).name());
  52. for(auto & elem : pendingAnimations())
  53. {
  54. if(elem == this)
  55. elem = nullptr;
  56. }
  57. logAnim->trace("Animation #%d deleted", ID);
  58. }
  59. std::vector<BattleAnimation *> & BattleAnimation::pendingAnimations()
  60. {
  61. return owner.stacksController->currentAnimations;
  62. }
  63. std::shared_ptr<CreatureAnimation> BattleAnimation::stackAnimation(const CStack * stack) const
  64. {
  65. return owner.stacksController->stackAnimation[stack->ID];
  66. }
  67. bool BattleAnimation::stackFacingRight(const CStack * stack)
  68. {
  69. return owner.stacksController->stackFacingRight[stack->ID];
  70. }
  71. void BattleAnimation::setStackFacingRight(const CStack * stack, bool facingRight)
  72. {
  73. owner.stacksController->stackFacingRight[stack->ID] = facingRight;
  74. }
  75. BattleStackAnimation::BattleStackAnimation(BattleInterface & owner, const CStack * stack)
  76. : BattleAnimation(owner),
  77. myAnim(stackAnimation(stack)),
  78. stack(stack)
  79. {
  80. assert(myAnim);
  81. }
  82. StackActionAnimation::StackActionAnimation(BattleInterface & owner, const CStack * stack)
  83. : BattleStackAnimation(owner, stack)
  84. , nextGroup(ECreatureAnimType::HOLDING)
  85. , currGroup(ECreatureAnimType::HOLDING)
  86. {
  87. }
  88. ECreatureAnimType::Type StackActionAnimation::getGroup() const
  89. {
  90. return currGroup;
  91. }
  92. void StackActionAnimation::setNextGroup( ECreatureAnimType::Type group )
  93. {
  94. nextGroup = group;
  95. }
  96. void StackActionAnimation::setGroup( ECreatureAnimType::Type group )
  97. {
  98. currGroup = group;
  99. }
  100. void StackActionAnimation::setSound( std::string sound )
  101. {
  102. this->sound = sound;
  103. }
  104. bool StackActionAnimation::init()
  105. {
  106. if (!sound.empty())
  107. CCS->soundh->playSound(sound);
  108. if (myAnim->framesInGroup(currGroup) > 0)
  109. {
  110. myAnim->playOnce(currGroup);
  111. myAnim->onAnimationReset += [&](){ delete this; };
  112. }
  113. else
  114. delete this;
  115. return true;
  116. }
  117. StackActionAnimation::~StackActionAnimation()
  118. {
  119. if (stack->isFrozen())
  120. myAnim->setType(ECreatureAnimType::HOLDING);
  121. else
  122. myAnim->setType(nextGroup);
  123. }
  124. ECreatureAnimType::Type AttackAnimation::findValidGroup( const std::vector<ECreatureAnimType::Type> candidates ) const
  125. {
  126. for ( auto group : candidates)
  127. {
  128. if(myAnim->framesInGroup(group) > 0)
  129. return group;
  130. }
  131. assert(0);
  132. return ECreatureAnimType::HOLDING;
  133. }
  134. const CCreature * AttackAnimation::getCreature() const
  135. {
  136. if (attackingStack->getCreature()->idNumber == CreatureID::ARROW_TOWERS)
  137. return owner.siegeController->getTurretCreature();
  138. else
  139. return attackingStack->getCreature();
  140. }
  141. AttackAnimation::AttackAnimation(BattleInterface & owner, const CStack *attacker, BattleHex _dest, const CStack *defender)
  142. : StackActionAnimation(owner, attacker),
  143. dest(_dest),
  144. defendingStack(defender),
  145. attackingStack(attacker)
  146. {
  147. assert(attackingStack && "attackingStack is nullptr in CBattleAttack::CBattleAttack !\n");
  148. attackingStackPosBeforeReturn = attackingStack->getPosition();
  149. }
  150. HittedAnimation::HittedAnimation(BattleInterface & owner, const CStack * stack)
  151. : StackActionAnimation(owner, stack)
  152. {
  153. setGroup(ECreatureAnimType::HITTED);
  154. setSound(battle_sound(stack->getCreature(), wince));
  155. logAnim->debug("Created HittedAnimation for %s", stack->getName());
  156. }
  157. DefenceAnimation::DefenceAnimation(BattleInterface & owner, const CStack * stack)
  158. : StackActionAnimation(owner, stack)
  159. {
  160. setGroup(ECreatureAnimType::DEFENCE);
  161. setSound(battle_sound(stack->getCreature(), defend));
  162. logAnim->debug("Created DefenceAnimation for %s", stack->getName());
  163. }
  164. DeathAnimation::DeathAnimation(BattleInterface & owner, const CStack * stack, bool ranged):
  165. StackActionAnimation(owner, stack)
  166. {
  167. setSound(battle_sound(stack->getCreature(), killed));
  168. if(ranged && myAnim->framesInGroup(ECreatureAnimType::DEATH_RANGED) > 0)
  169. setGroup(ECreatureAnimType::DEATH_RANGED);
  170. else
  171. setGroup(ECreatureAnimType::DEATH);
  172. if(ranged && myAnim->framesInGroup(ECreatureAnimType::DEAD_RANGED) > 0)
  173. setNextGroup(ECreatureAnimType::DEAD_RANGED);
  174. else
  175. setNextGroup(ECreatureAnimType::DEAD);
  176. logAnim->debug("Created DeathAnimation for %s", stack->getName());
  177. }
  178. DummyAnimation::DummyAnimation(BattleInterface & owner, int howManyFrames)
  179. : BattleAnimation(owner),
  180. counter(0),
  181. howMany(howManyFrames)
  182. {
  183. logAnim->debug("Created dummy animation for %d frames", howManyFrames);
  184. }
  185. bool DummyAnimation::init()
  186. {
  187. return true;
  188. }
  189. void DummyAnimation::nextFrame()
  190. {
  191. counter++;
  192. if(counter > howMany)
  193. delete this;
  194. }
  195. ECreatureAnimType::Type MeleeAttackAnimation::getUpwardsGroup(bool multiAttack) const
  196. {
  197. if (!multiAttack)
  198. return ECreatureAnimType::ATTACK_UP;
  199. return findValidGroup({
  200. ECreatureAnimType::GROUP_ATTACK_UP,
  201. ECreatureAnimType::SPECIAL_UP,
  202. ECreatureAnimType::ATTACK_UP
  203. });
  204. }
  205. ECreatureAnimType::Type MeleeAttackAnimation::getForwardGroup(bool multiAttack) const
  206. {
  207. if (!multiAttack)
  208. return ECreatureAnimType::ATTACK_FRONT;
  209. return findValidGroup({
  210. ECreatureAnimType::GROUP_ATTACK_FRONT,
  211. ECreatureAnimType::SPECIAL_FRONT,
  212. ECreatureAnimType::ATTACK_FRONT
  213. });
  214. }
  215. ECreatureAnimType::Type MeleeAttackAnimation::getDownwardsGroup(bool multiAttack) const
  216. {
  217. if (!multiAttack)
  218. return ECreatureAnimType::ATTACK_DOWN;
  219. return findValidGroup({
  220. ECreatureAnimType::GROUP_ATTACK_DOWN,
  221. ECreatureAnimType::SPECIAL_DOWN,
  222. ECreatureAnimType::ATTACK_DOWN
  223. });
  224. }
  225. ECreatureAnimType::Type MeleeAttackAnimation::selectGroup(bool multiAttack)
  226. {
  227. const ECreatureAnimType::Type mutPosToGroup[] =
  228. {
  229. getUpwardsGroup (multiAttack),
  230. getUpwardsGroup (multiAttack),
  231. getForwardGroup (multiAttack),
  232. getDownwardsGroup(multiAttack),
  233. getDownwardsGroup(multiAttack),
  234. getForwardGroup (multiAttack)
  235. };
  236. int revShiftattacker = (attackingStack->side == BattleSide::ATTACKER ? -1 : 1);
  237. int mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, dest);
  238. if(mutPos == -1 && attackingStack->doubleWide())
  239. {
  240. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, defendingStack->getPosition());
  241. }
  242. if (mutPos == -1 && defendingStack->doubleWide())
  243. {
  244. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn, defendingStack->occupiedHex());
  245. }
  246. if (mutPos == -1 && defendingStack->doubleWide() && attackingStack->doubleWide())
  247. {
  248. mutPos = BattleHex::mutualPosition(attackingStackPosBeforeReturn + revShiftattacker, defendingStack->occupiedHex());
  249. }
  250. assert(mutPos >= 0 && mutPos <=5);
  251. return mutPosToGroup[mutPos];
  252. }
  253. void MeleeAttackAnimation::nextFrame()
  254. {
  255. size_t currentFrame = stackAnimation(attackingStack)->getCurrentFrame();
  256. size_t totalFrames = stackAnimation(attackingStack)->framesInGroup(getGroup());
  257. if ( currentFrame * 2 >= totalFrames )
  258. {
  259. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  260. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  261. }
  262. AttackAnimation::nextFrame();
  263. }
  264. MeleeAttackAnimation::MeleeAttackAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, bool multiAttack)
  265. : AttackAnimation(owner, attacker, _dest, _attacked)
  266. {
  267. logAnim->debug("Created MeleeAttackAnimation for %s", attacker->getName());
  268. setSound(battle_sound(getCreature(), attack));
  269. setGroup(selectGroup(multiAttack));
  270. }
  271. StackMoveAnimation::StackMoveAnimation(BattleInterface & owner, const CStack * _stack, BattleHex prevHex, BattleHex nextHex):
  272. BattleStackAnimation(owner, _stack),
  273. prevHex(prevHex),
  274. nextHex(nextHex)
  275. {
  276. }
  277. bool MovementAnimation::init()
  278. {
  279. assert(stack);
  280. assert(!myAnim->isDeadOrDying());
  281. assert(stackAnimation(stack)->framesInGroup(ECreatureAnimType::MOVING) > 0);
  282. if(stackAnimation(stack)->framesInGroup(ECreatureAnimType::MOVING) == 0)
  283. {
  284. //no movement, end immediately
  285. delete this;
  286. return false;
  287. }
  288. logAnim->debug("CMovementAnimation::init: stack %s moves %d -> %d", stack->getName(), prevHex, nextHex);
  289. //reverse unit if necessary
  290. if(owner.stacksController->shouldRotate(stack, prevHex, nextHex))
  291. {
  292. // it seems that H3 does NOT plays full rotation animation during movement
  293. // Logical since it takes quite a lot of time
  294. rotateStack(prevHex);
  295. }
  296. if(myAnim->getType() != ECreatureAnimType::MOVING)
  297. {
  298. myAnim->setType(ECreatureAnimType::MOVING);
  299. }
  300. if (owner.moveSoundHander == -1)
  301. {
  302. owner.moveSoundHander = CCS->soundh->playSound(battle_sound(stack->getCreature(), move), -1);
  303. }
  304. Point begPosition = owner.stacksController->getStackPositionAtHex(prevHex, stack);
  305. Point endPosition = owner.stacksController->getStackPositionAtHex(nextHex, stack);
  306. timeToMove = AnimationControls::getMovementDuration(stack->getCreature());
  307. begX = begPosition.x;
  308. begY = begPosition.y;
  309. progress = 0;
  310. distanceX = endPosition.x - begPosition.x;
  311. distanceY = endPosition.y - begPosition.y;
  312. if (stack->hasBonus(Selector::type()(Bonus::FLYING)))
  313. {
  314. float distance = static_cast<float>(sqrt(distanceX * distanceX + distanceY * distanceY));
  315. timeToMove *= AnimationControls::getFlightDistance(stack->getCreature()) / distance;
  316. }
  317. return true;
  318. }
  319. void MovementAnimation::nextFrame()
  320. {
  321. progress += float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000 * timeToMove;
  322. //moving instructions
  323. myAnim->pos.x = static_cast<Sint16>(begX + distanceX * progress );
  324. myAnim->pos.y = static_cast<Sint16>(begY + distanceY * progress );
  325. BattleAnimation::nextFrame();
  326. if(progress >= 1.0)
  327. {
  328. // Sets the position of the creature animation sprites
  329. Point coords = owner.stacksController->getStackPositionAtHex(nextHex, stack);
  330. myAnim->pos.moveTo(coords);
  331. // true if creature haven't reached the final destination hex
  332. if ((curentMoveIndex + 1) < destTiles.size())
  333. {
  334. // update the next hex field which has to be reached by the stack
  335. curentMoveIndex++;
  336. prevHex = nextHex;
  337. nextHex = destTiles[curentMoveIndex];
  338. // request re-initialization
  339. initialized = false;
  340. }
  341. else
  342. delete this;
  343. }
  344. }
  345. MovementAnimation::~MovementAnimation()
  346. {
  347. assert(stack);
  348. myAnim->pos.moveTo(owner.stacksController->getStackPositionAtHex(nextHex, stack));
  349. if(owner.moveSoundHander != -1)
  350. {
  351. CCS->soundh->stopSound(owner.moveSoundHander);
  352. owner.moveSoundHander = -1;
  353. }
  354. }
  355. MovementAnimation::MovementAnimation(BattleInterface & owner, const CStack *stack, std::vector<BattleHex> _destTiles, int _distance)
  356. : StackMoveAnimation(owner, stack, stack->getPosition(), _destTiles.front()),
  357. destTiles(_destTiles),
  358. curentMoveIndex(0),
  359. begX(0), begY(0),
  360. distanceX(0), distanceY(0),
  361. timeToMove(0.0),
  362. progress(0.0)
  363. {
  364. logAnim->debug("Created MovementAnimation for %s", stack->getName());
  365. }
  366. MovementEndAnimation::MovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile)
  367. : StackMoveAnimation(owner, _stack, destTile, destTile)
  368. {
  369. logAnim->debug("Created MovementEndAnimation for %s", stack->getName());
  370. }
  371. bool MovementEndAnimation::init()
  372. {
  373. assert(stack);
  374. assert(!myAnim->isDeadOrDying());
  375. if(!stack || myAnim->isDeadOrDying())
  376. {
  377. delete this;
  378. return false;
  379. }
  380. logAnim->debug("CMovementEndAnimation::init: stack %s", stack->getName());
  381. CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
  382. if(!myAnim->framesInGroup(ECreatureAnimType::MOVE_END))
  383. {
  384. delete this;
  385. return false;
  386. }
  387. myAnim->setType(ECreatureAnimType::MOVE_END);
  388. myAnim->onAnimationReset += [&](){ delete this; };
  389. return true;
  390. }
  391. MovementEndAnimation::~MovementEndAnimation()
  392. {
  393. if(myAnim->getType() != ECreatureAnimType::DEAD)
  394. myAnim->setType(ECreatureAnimType::HOLDING); //resetting to default
  395. CCS->curh->show();
  396. }
  397. MovementStartAnimation::MovementStartAnimation(BattleInterface & owner, const CStack * _stack)
  398. : StackMoveAnimation(owner, _stack, _stack->getPosition(), _stack->getPosition())
  399. {
  400. logAnim->debug("Created MovementStartAnimation for %s", stack->getName());
  401. }
  402. bool MovementStartAnimation::init()
  403. {
  404. assert(stack);
  405. assert(!myAnim->isDeadOrDying());
  406. if(!stack || myAnim->isDeadOrDying())
  407. {
  408. delete this;
  409. return false;
  410. }
  411. logAnim->debug("CMovementStartAnimation::init: stack %s", stack->getName());
  412. CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
  413. if(!myAnim->framesInGroup(ECreatureAnimType::MOVE_START))
  414. {
  415. delete this;
  416. return false;
  417. }
  418. myAnim->setType(ECreatureAnimType::MOVE_START);
  419. myAnim->onAnimationReset += [&](){ delete this; };
  420. return true;
  421. }
  422. ReverseAnimation::ReverseAnimation(BattleInterface & owner, const CStack * stack, BattleHex dest)
  423. : StackMoveAnimation(owner, stack, dest, dest)
  424. {
  425. logAnim->debug("Created ReverseAnimation for %s", stack->getName());
  426. }
  427. bool ReverseAnimation::init()
  428. {
  429. assert(myAnim);
  430. assert(!myAnim->isDeadOrDying());
  431. if(myAnim == nullptr || myAnim->isDeadOrDying())
  432. {
  433. delete this;
  434. return false; //there is no such creature
  435. }
  436. logAnim->debug("CReverseAnimation::init: stack %s", stack->getName());
  437. if(myAnim->framesInGroup(ECreatureAnimType::TURN_L))
  438. {
  439. myAnim->playOnce(ECreatureAnimType::TURN_L);
  440. myAnim->onAnimationReset += std::bind(&ReverseAnimation::setupSecondPart, this);
  441. }
  442. else
  443. {
  444. setupSecondPart();
  445. }
  446. return true;
  447. }
  448. void BattleStackAnimation::rotateStack(BattleHex hex)
  449. {
  450. setStackFacingRight(stack, !stackFacingRight(stack));
  451. stackAnimation(stack)->pos.moveTo(owner.stacksController->getStackPositionAtHex(hex, stack));
  452. }
  453. void ReverseAnimation::setupSecondPart()
  454. {
  455. assert(stack);
  456. if(!stack)
  457. {
  458. delete this;
  459. return;
  460. }
  461. rotateStack(nextHex);
  462. if(myAnim->framesInGroup(ECreatureAnimType::TURN_R))
  463. {
  464. myAnim->playOnce(ECreatureAnimType::TURN_R);
  465. myAnim->onAnimationReset += [&](){ delete this; };
  466. }
  467. else
  468. delete this;
  469. }
  470. ResurrectionAnimation::ResurrectionAnimation(BattleInterface & owner, const CStack * _stack):
  471. StackActionAnimation(owner, _stack)
  472. {
  473. setGroup(ECreatureAnimType::RESURRECTION);
  474. logAnim->debug("Created ResurrectionAnimation for %s", stack->getName());
  475. }
  476. bool ColorTransformAnimation::init()
  477. {
  478. return true;
  479. }
  480. void ColorTransformAnimation::nextFrame()
  481. {
  482. float elapsed = GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
  483. float fullTime = AnimationControls::getFadeInDuration();
  484. float delta = elapsed / fullTime;
  485. totalProgress += delta;
  486. size_t index = 0;
  487. while (index < timePoints.size() && timePoints[index] < totalProgress )
  488. ++index;
  489. if (index == timePoints.size())
  490. {
  491. //end of animation. Apply ColorShifter using final values and die
  492. const auto & shifter = steps[index - 1];
  493. owner.stacksController->setStackColorFilter(shifter, stack, spell, false);
  494. delete this;
  495. return;
  496. }
  497. assert(index != 0);
  498. const auto & prevShifter = steps[index - 1];
  499. const auto & nextShifter = steps[index];
  500. float prevPoint = timePoints[index-1];
  501. float nextPoint = timePoints[index];
  502. float localProgress = totalProgress - prevPoint;
  503. float stepDuration = (nextPoint - prevPoint);
  504. float factor = localProgress / stepDuration;
  505. auto shifter = ColorFilter::genInterpolated(prevShifter, nextShifter, factor);
  506. owner.stacksController->setStackColorFilter(shifter, stack, spell, true);
  507. }
  508. ColorTransformAnimation::ColorTransformAnimation(BattleInterface & owner, const CStack * _stack, const CSpell * spell):
  509. BattleStackAnimation(owner, _stack),
  510. spell(spell),
  511. totalProgress(0.f)
  512. {
  513. logAnim->debug("Created ColorTransformAnimation for %s", stack->getName());
  514. }
  515. ColorTransformAnimation * ColorTransformAnimation::bloodlustAnimation(BattleInterface & owner, const CStack * stack, const CSpell * spell)
  516. {
  517. auto result = new ColorTransformAnimation(owner, stack, spell);
  518. result->steps.push_back(ColorFilter::genEmptyShifter());
  519. result->steps.push_back(ColorFilter::genMuxerShifter( { 0.3, 0.0, 0.3, 0.4}, { 0, 1, 0, 0}, { 0, 0, 1, 0}, 1.f ));
  520. result->steps.push_back(ColorFilter::genMuxerShifter( { 0.3, 0.3, 0.3, 0.1}, { 0, 0.5, 0, 0}, { 0, 0.5, 0, 0}, 1.f ));
  521. result->steps.push_back(ColorFilter::genMuxerShifter( { 0.3, 0.0, 0.3, 0.4}, { 0, 1, 0, 0}, { 0, 0, 1, 0}, 1.f ));
  522. result->steps.push_back(ColorFilter::genEmptyShifter());
  523. result->timePoints.push_back(0.0f);
  524. result->timePoints.push_back(0.2f);
  525. result->timePoints.push_back(0.4f);
  526. result->timePoints.push_back(0.6f);
  527. result->timePoints.push_back(0.8f);
  528. return result;
  529. }
  530. ColorTransformAnimation * ColorTransformAnimation::cloneAnimation(BattleInterface & owner, const CStack * stack, const CSpell * spell)
  531. {
  532. auto result = new ColorTransformAnimation(owner, stack, spell);
  533. result->steps.push_back(ColorFilter::genRangeShifter( 0, 0, 0.5, 0.5, 0.5, 1.0));
  534. result->timePoints.push_back(0.f);
  535. return result;
  536. }
  537. ColorTransformAnimation * ColorTransformAnimation::petrifyAnimation(BattleInterface & owner, const CStack * stack, const CSpell * spell)
  538. {
  539. auto result = new ColorTransformAnimation(owner, stack, spell);
  540. result->steps.push_back(ColorFilter::genEmptyShifter());
  541. result->steps.push_back(ColorFilter::genGrayscaleShifter());
  542. result->timePoints.push_back(0.f);
  543. result->timePoints.push_back(1.f);
  544. return result;
  545. }
  546. ColorTransformAnimation * ColorTransformAnimation::summonAnimation(BattleInterface & owner, const CStack * stack)
  547. {
  548. auto result = teleportInAnimation(owner, stack);
  549. result->timePoints.back() = 1.0f;
  550. return result;
  551. }
  552. ColorTransformAnimation * ColorTransformAnimation::teleportInAnimation(BattleInterface & owner, const CStack * stack)
  553. {
  554. auto result = new ColorTransformAnimation(owner, stack, nullptr);
  555. result->steps.push_back(ColorFilter::genAlphaShifter(0.f));
  556. result->steps.push_back(ColorFilter::genEmptyShifter());
  557. result->timePoints.push_back(0.0f);
  558. result->timePoints.push_back(0.2f);
  559. return result;
  560. }
  561. ColorTransformAnimation * ColorTransformAnimation::teleportOutAnimation(BattleInterface & owner, const CStack * stack)
  562. {
  563. auto result = teleportInAnimation(owner, stack);
  564. std::swap(result->steps[0], result->steps[1]);
  565. return result;
  566. }
  567. ColorTransformAnimation * ColorTransformAnimation::fadeOutAnimation(BattleInterface & owner, const CStack * stack)
  568. {
  569. auto result = teleportOutAnimation(owner, stack);
  570. result->timePoints.back() = 1.0f;
  571. return result;
  572. }
  573. RangedAttackAnimation::RangedAttackAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  574. : AttackAnimation(owner_, attacker, dest_, defender),
  575. projectileEmitted(false)
  576. {
  577. setSound(battle_sound(getCreature(), shoot));
  578. }
  579. bool RangedAttackAnimation::init()
  580. {
  581. setAnimationGroup();
  582. initializeProjectile();
  583. return AttackAnimation::init();
  584. }
  585. void RangedAttackAnimation::setAnimationGroup()
  586. {
  587. Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
  588. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack);
  589. //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  590. static const double straightAngle = 0.2;
  591. double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
  592. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  593. if (projectileAngle > straightAngle)
  594. setGroup(getUpwardsGroup());
  595. else if (projectileAngle < -straightAngle)
  596. setGroup(getDownwardsGroup());
  597. else
  598. setGroup(getForwardGroup());
  599. }
  600. void RangedAttackAnimation::initializeProjectile()
  601. {
  602. const CCreature *shooterInfo = getCreature();
  603. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225);
  604. Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
  605. int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
  606. if (getGroup() == getUpwardsGroup())
  607. {
  608. shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
  609. shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
  610. }
  611. else if (getGroup() == getDownwardsGroup())
  612. {
  613. shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
  614. shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
  615. }
  616. else if (getGroup() == getForwardGroup())
  617. {
  618. shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
  619. shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
  620. }
  621. else
  622. {
  623. assert(0);
  624. }
  625. createProjectile(shotOrigin, shotTarget);
  626. }
  627. void RangedAttackAnimation::emitProjectile()
  628. {
  629. logAnim->debug("Ranged attack projectile emitted");
  630. owner.projectilesController->emitStackProjectile(attackingStack);
  631. projectileEmitted = true;
  632. }
  633. void RangedAttackAnimation::nextFrame()
  634. {
  635. // animation should be paused if there is an active projectile
  636. if (projectileEmitted)
  637. {
  638. if (!owner.projectilesController->hasActiveProjectile(attackingStack, false))
  639. {
  640. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  641. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  642. }
  643. }
  644. AttackAnimation::nextFrame();
  645. if (!projectileEmitted)
  646. {
  647. // emit projectile once animation playback reached "climax" frame
  648. if ( stackAnimation(attackingStack)->getCurrentFrame() >= getAttackClimaxFrame() )
  649. {
  650. emitProjectile();
  651. return;
  652. }
  653. }
  654. }
  655. RangedAttackAnimation::~RangedAttackAnimation()
  656. {
  657. assert(!owner.projectilesController->hasActiveProjectile(attackingStack, false));
  658. assert(projectileEmitted);
  659. // FIXME: is this possible? Animation is over but we're yet to fire projectile?
  660. if (!projectileEmitted)
  661. {
  662. logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
  663. emitProjectile();
  664. }
  665. }
  666. ShootingAnimation::ShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
  667. : RangedAttackAnimation(owner, attacker, _dest, _attacked)
  668. {
  669. logAnim->debug("Created ShootingAnimation for %s", stack->getName());
  670. }
  671. void ShootingAnimation::createProjectile(const Point & from, const Point & dest) const
  672. {
  673. owner.projectilesController->createProjectile(attackingStack, from, dest);
  674. }
  675. uint32_t ShootingAnimation::getAttackClimaxFrame() const
  676. {
  677. const CCreature *shooterInfo = getCreature();
  678. return shooterInfo->animation.attackClimaxFrame;
  679. }
  680. ECreatureAnimType::Type ShootingAnimation::getUpwardsGroup() const
  681. {
  682. return ECreatureAnimType::SHOOT_UP;
  683. }
  684. ECreatureAnimType::Type ShootingAnimation::getForwardGroup() const
  685. {
  686. return ECreatureAnimType::SHOOT_FRONT;
  687. }
  688. ECreatureAnimType::Type ShootingAnimation::getDownwardsGroup() const
  689. {
  690. return ECreatureAnimType::SHOOT_DOWN;
  691. }
  692. CatapultAnimation::CatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, int _catapultDmg)
  693. : ShootingAnimation(owner, attacker, _dest, _attacked),
  694. catapultDamage(_catapultDmg),
  695. explosionEmitted(false)
  696. {
  697. logAnim->debug("Created shooting anim for %s", stack->getName());
  698. }
  699. void CatapultAnimation::nextFrame()
  700. {
  701. ShootingAnimation::nextFrame();
  702. if ( explosionEmitted)
  703. return;
  704. if ( !projectileEmitted)
  705. return;
  706. if (owner.projectilesController->hasActiveProjectile(attackingStack, false))
  707. return;
  708. explosionEmitted = true;
  709. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225) - Point(126, 105);
  710. if(catapultDamage > 0)
  711. owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLHIT", "SGEXPL.DEF", shotTarget));
  712. else
  713. owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLMISS", "CSGRCK.DEF", shotTarget));
  714. }
  715. void CatapultAnimation::createProjectile(const Point & from, const Point & dest) const
  716. {
  717. owner.projectilesController->createCatapultProjectile(attackingStack, from, dest);
  718. }
  719. CastAnimation::CastAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell)
  720. : RangedAttackAnimation(owner_, attacker, dest_, defender),
  721. spell(spell)
  722. {
  723. assert(dest.isValid());// FIXME: when?
  724. if(!dest_.isValid() && defender)
  725. dest = defender->getPosition();
  726. }
  727. ECreatureAnimType::Type CastAnimation::getUpwardsGroup() const
  728. {
  729. return findValidGroup({
  730. ECreatureAnimType::CAST_UP,
  731. ECreatureAnimType::SPECIAL_UP,
  732. ECreatureAnimType::SHOOT_UP,
  733. ECreatureAnimType::ATTACK_UP
  734. });
  735. }
  736. ECreatureAnimType::Type CastAnimation::getForwardGroup() const
  737. {
  738. return findValidGroup({
  739. ECreatureAnimType::CAST_FRONT,
  740. ECreatureAnimType::SPECIAL_FRONT,
  741. ECreatureAnimType::SHOOT_FRONT,
  742. ECreatureAnimType::ATTACK_FRONT
  743. });
  744. }
  745. ECreatureAnimType::Type CastAnimation::getDownwardsGroup() const
  746. {
  747. return findValidGroup({
  748. ECreatureAnimType::CAST_DOWN,
  749. ECreatureAnimType::SPECIAL_DOWN,
  750. ECreatureAnimType::SHOOT_DOWN,
  751. ECreatureAnimType::ATTACK_DOWN
  752. });
  753. }
  754. void CastAnimation::createProjectile(const Point & from, const Point & dest) const
  755. {
  756. if (!spell->animationInfo.projectile.empty())
  757. owner.projectilesController->createSpellProjectile(attackingStack, from, dest, spell);
  758. }
  759. uint32_t CastAnimation::getAttackClimaxFrame() const
  760. {
  761. //TODO: allow defining this parameter in config file, separately from attackClimaxFrame of missile attacks
  762. uint32_t maxFrames = stackAnimation(attackingStack)->framesInGroup(getGroup());
  763. return maxFrames / 2;
  764. }
  765. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, int effects):
  766. BattleAnimation(owner),
  767. animation(std::make_shared<CAnimation>(animationName)),
  768. soundName(soundName),
  769. effectFlags(effects),
  770. soundPlayed(false),
  771. soundFinished(false),
  772. effectFinished(false)
  773. {
  774. logAnim->debug("CPointEffectAnimation::init: effect %s", animationName);
  775. }
  776. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<BattleHex> hex, int effects):
  777. PointEffectAnimation(owner, soundName, animationName, effects)
  778. {
  779. battlehexes = hex;
  780. }
  781. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, BattleHex hex, int effects):
  782. PointEffectAnimation(owner, soundName, animationName, effects)
  783. {
  784. assert(hex.isValid());
  785. battlehexes.push_back(hex);
  786. }
  787. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<Point> pos, int effects):
  788. PointEffectAnimation(owner, soundName, animationName, effects)
  789. {
  790. positions = pos;
  791. }
  792. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, int effects):
  793. PointEffectAnimation(owner, soundName, animationName, effects)
  794. {
  795. positions.push_back(pos);
  796. }
  797. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, BattleHex hex, int effects):
  798. PointEffectAnimation(owner, soundName, animationName, effects)
  799. {
  800. assert(hex.isValid());
  801. battlehexes.push_back(hex);
  802. positions.push_back(pos);
  803. }
  804. bool PointEffectAnimation::init()
  805. {
  806. animation->preload();
  807. auto first = animation->getImage(0, 0, true);
  808. if(!first)
  809. {
  810. delete this;
  811. return false;
  812. }
  813. if (screenFill())
  814. {
  815. for(int i=0; i * first->width() < owner.pos.w ; ++i)
  816. for(int j=0; j * first->height() < owner.pos.h ; ++j)
  817. positions.push_back(Point( owner.pos.x + i * first->width(), owner.pos.y + j * first->height()));
  818. }
  819. BattleEffect be;
  820. be.effectID = ID;
  821. be.animation = animation;
  822. be.currentFrame = 0;
  823. for (size_t i = 0; i < std::max(battlehexes.size(), positions.size()); ++i)
  824. {
  825. bool hasTile = i < battlehexes.size();
  826. bool hasPosition = i < positions.size();
  827. if (hasTile && !forceOnTop())
  828. be.position = battlehexes[i];
  829. else
  830. be.position = BattleHex::INVALID;
  831. if (hasPosition)
  832. {
  833. be.x = positions[i].x;
  834. be.y = positions[i].y;
  835. }
  836. else
  837. {
  838. const CStack * destStack = owner.getCurrentPlayerInterface()->cb->battleGetStackByPos(battlehexes[i], false);
  839. Rect tilePos = owner.fieldController->hexPositionAbsolute(battlehexes[i]);
  840. be.x = tilePos.x + tilePos.w/2 - first->width()/2;
  841. if(destStack && destStack->doubleWide()) // Correction for 2-hex creatures.
  842. be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
  843. if (alignToBottom())
  844. be.y = tilePos.y + tilePos.h - first->height();
  845. else
  846. be.y = tilePos.y - first->height()/2;
  847. }
  848. owner.effectsController->battleEffects.push_back(be);
  849. }
  850. return true;
  851. }
  852. void PointEffectAnimation::nextFrame()
  853. {
  854. playSound();
  855. playEffect();
  856. if (soundFinished && effectFinished)
  857. {
  858. //remove visual effect itself only if sound has finished as well - necessary for obstacles like force field
  859. clearEffect();
  860. delete this;
  861. }
  862. }
  863. bool PointEffectAnimation::alignToBottom() const
  864. {
  865. return effectFlags & ALIGN_TO_BOTTOM;
  866. }
  867. bool PointEffectAnimation::waitForSound() const
  868. {
  869. return effectFlags & WAIT_FOR_SOUND;
  870. }
  871. bool PointEffectAnimation::forceOnTop() const
  872. {
  873. return effectFlags & FORCE_ON_TOP;
  874. }
  875. bool PointEffectAnimation::screenFill() const
  876. {
  877. return effectFlags & SCREEN_FILL;
  878. }
  879. void PointEffectAnimation::onEffectFinished()
  880. {
  881. effectFinished = true;
  882. }
  883. void PointEffectAnimation::onSoundFinished()
  884. {
  885. soundFinished = true;
  886. }
  887. void PointEffectAnimation::playSound()
  888. {
  889. if (soundPlayed)
  890. return;
  891. soundPlayed = true;
  892. if (soundName.empty())
  893. {
  894. onSoundFinished();
  895. return;
  896. }
  897. int channel = CCS->soundh->playSound(soundName);
  898. if (!waitForSound() || channel == -1)
  899. onSoundFinished();
  900. else
  901. CCS->soundh->setCallback(channel, [&](){ onSoundFinished(); });
  902. }
  903. void PointEffectAnimation::playEffect()
  904. {
  905. if ( effectFinished )
  906. return;
  907. for(auto & elem : owner.effectsController->battleEffects)
  908. {
  909. if(elem.effectID == ID)
  910. {
  911. elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  912. if(elem.currentFrame >= elem.animation->size())
  913. {
  914. elem.currentFrame = elem.animation->size() - 1;
  915. onEffectFinished();
  916. break;
  917. }
  918. }
  919. }
  920. }
  921. void PointEffectAnimation::clearEffect()
  922. {
  923. auto & effects = owner.effectsController->battleEffects;
  924. vstd::erase_if(effects, [&](const BattleEffect & effect){
  925. return effect.effectID == ID;
  926. });
  927. }
  928. PointEffectAnimation::~PointEffectAnimation()
  929. {
  930. assert(effectFinished);
  931. assert(soundFinished);
  932. }
  933. HeroCastAnimation::HeroCastAnimation(BattleInterface & owner, std::shared_ptr<BattleHero> hero, BattleHex dest, const CStack * defender, const CSpell * spell):
  934. BattleAnimation(owner),
  935. projectileEmitted(false),
  936. hero(hero),
  937. target(defender),
  938. tile(dest),
  939. spell(spell)
  940. {
  941. }
  942. bool HeroCastAnimation::init()
  943. {
  944. hero->setPhase(EHeroAnimType::CAST_SPELL);
  945. hero->onPhaseFinished([&](){
  946. assert(owner.getAnimationCondition(EAnimationEvents::HIT) == true);
  947. delete this;
  948. });
  949. initializeProjectile();
  950. return true;
  951. }
  952. void HeroCastAnimation::initializeProjectile()
  953. {
  954. // spell has no projectile to play, ignore this step
  955. if (spell->animationInfo.projectile.empty())
  956. return;
  957. // targeted spells should have well, target
  958. assert(tile.isValid());
  959. Point srccoord = hero->pos.center();
  960. Point destcoord = owner.stacksController->getStackPositionAtHex(tile, target); //position attacked by projectile
  961. destcoord += Point(222, 265); // FIXME: what are these constants?
  962. owner.projectilesController->createSpellProjectile( nullptr, srccoord, destcoord, spell);
  963. }
  964. void HeroCastAnimation::emitProjectile()
  965. {
  966. if (projectileEmitted)
  967. return;
  968. //spell has no projectile to play, skip this step and immediately play hit animations
  969. if (spell->animationInfo.projectile.empty())
  970. emitAnimationEvent();
  971. else
  972. owner.projectilesController->emitStackProjectile( nullptr );
  973. projectileEmitted = true;
  974. }
  975. void HeroCastAnimation::emitAnimationEvent()
  976. {
  977. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  978. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  979. }
  980. void HeroCastAnimation::nextFrame()
  981. {
  982. float frame = hero->getFrame();
  983. if (frame < 4.0f) // middle point of animation //TODO: un-hardcode
  984. return;
  985. if (!projectileEmitted)
  986. {
  987. emitProjectile();
  988. hero->pause();
  989. return;
  990. }
  991. if (!owner.projectilesController->hasActiveProjectile(nullptr, false))
  992. {
  993. emitAnimationEvent();
  994. //TODO: check H3 - it is possible that hero animation should be paused until hit effect is over, not just projectile
  995. hero->play();
  996. }
  997. }