BattleAnimationClasses.cpp 34 KB

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