BattleAnimationClasses.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. if(owner.moveSoundHander != -1)
  349. {
  350. CCS->soundh->stopSound(owner.moveSoundHander);
  351. owner.moveSoundHander = -1;
  352. }
  353. }
  354. MovementAnimation::MovementAnimation(BattleInterface & owner, const CStack *stack, std::vector<BattleHex> _destTiles, int _distance)
  355. : StackMoveAnimation(owner, stack, stack->getPosition(), _destTiles.front()),
  356. destTiles(_destTiles),
  357. curentMoveIndex(0),
  358. begX(0), begY(0),
  359. distanceX(0), distanceY(0),
  360. timeToMove(0.0),
  361. progress(0.0)
  362. {
  363. logAnim->debug("Created MovementAnimation for %s", stack->getName());
  364. }
  365. MovementEndAnimation::MovementEndAnimation(BattleInterface & owner, const CStack * _stack, BattleHex destTile)
  366. : StackMoveAnimation(owner, _stack, destTile, destTile)
  367. {
  368. logAnim->debug("Created MovementEndAnimation for %s", stack->getName());
  369. }
  370. bool MovementEndAnimation::init()
  371. {
  372. assert(stack);
  373. assert(!myAnim->isDeadOrDying());
  374. if(!stack || myAnim->isDeadOrDying())
  375. {
  376. delete this;
  377. return false;
  378. }
  379. logAnim->debug("CMovementEndAnimation::init: stack %s", stack->getName());
  380. myAnim->pos.moveTo(owner.stacksController->getStackPositionAtHex(nextHex, stack));
  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 std::string & colorFilterName, const CSpell * spell):
  509. BattleStackAnimation(owner, _stack),
  510. spell(spell),
  511. totalProgress(0.f)
  512. {
  513. auto effect = owner.effectsController->getMuxerEffect(colorFilterName);
  514. steps = effect.filters;
  515. timePoints = effect.timePoints;
  516. assert(!steps.empty() && steps.size() == timePoints.size());
  517. logAnim->debug("Created ColorTransformAnimation for %s", stack->getName());
  518. }
  519. RangedAttackAnimation::RangedAttackAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender)
  520. : AttackAnimation(owner_, attacker, dest_, defender),
  521. projectileEmitted(false)
  522. {
  523. setSound(battle_sound(getCreature(), shoot));
  524. }
  525. bool RangedAttackAnimation::init()
  526. {
  527. setAnimationGroup();
  528. initializeProjectile();
  529. return AttackAnimation::init();
  530. }
  531. void RangedAttackAnimation::setAnimationGroup()
  532. {
  533. Point shooterPos = stackAnimation(attackingStack)->pos.topLeft();
  534. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack);
  535. //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  536. static const double straightAngle = 0.2;
  537. double projectileAngle = -atan2(shotTarget.y - shooterPos.y, std::abs(shotTarget.x - shooterPos.x));
  538. // Calculate projectile start position. Offsets are read out of the CRANIM.TXT.
  539. if (projectileAngle > straightAngle)
  540. setGroup(getUpwardsGroup());
  541. else if (projectileAngle < -straightAngle)
  542. setGroup(getDownwardsGroup());
  543. else
  544. setGroup(getForwardGroup());
  545. }
  546. void RangedAttackAnimation::initializeProjectile()
  547. {
  548. const CCreature *shooterInfo = getCreature();
  549. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225);
  550. Point shotOrigin = stackAnimation(attackingStack)->pos.topLeft() + Point(222, 265);
  551. int multiplier = stackFacingRight(attackingStack) ? 1 : -1;
  552. if (getGroup() == getUpwardsGroup())
  553. {
  554. shotOrigin.x += ( -25 + shooterInfo->animation.upperRightMissleOffsetX ) * multiplier;
  555. shotOrigin.y += shooterInfo->animation.upperRightMissleOffsetY;
  556. }
  557. else if (getGroup() == getDownwardsGroup())
  558. {
  559. shotOrigin.x += ( -25 + shooterInfo->animation.lowerRightMissleOffsetX ) * multiplier;
  560. shotOrigin.y += shooterInfo->animation.lowerRightMissleOffsetY;
  561. }
  562. else if (getGroup() == getForwardGroup())
  563. {
  564. shotOrigin.x += ( -25 + shooterInfo->animation.rightMissleOffsetX ) * multiplier;
  565. shotOrigin.y += shooterInfo->animation.rightMissleOffsetY;
  566. }
  567. else
  568. {
  569. assert(0);
  570. }
  571. createProjectile(shotOrigin, shotTarget);
  572. }
  573. void RangedAttackAnimation::emitProjectile()
  574. {
  575. logAnim->debug("Ranged attack projectile emitted");
  576. owner.projectilesController->emitStackProjectile(attackingStack);
  577. projectileEmitted = true;
  578. }
  579. void RangedAttackAnimation::nextFrame()
  580. {
  581. // animation should be paused if there is an active projectile
  582. if (projectileEmitted)
  583. {
  584. if (!owner.projectilesController->hasActiveProjectile(attackingStack, false))
  585. {
  586. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  587. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  588. }
  589. }
  590. AttackAnimation::nextFrame();
  591. if (!projectileEmitted)
  592. {
  593. // emit projectile once animation playback reached "climax" frame
  594. if ( stackAnimation(attackingStack)->getCurrentFrame() >= getAttackClimaxFrame() )
  595. {
  596. emitProjectile();
  597. return;
  598. }
  599. }
  600. }
  601. RangedAttackAnimation::~RangedAttackAnimation()
  602. {
  603. assert(!owner.projectilesController->hasActiveProjectile(attackingStack, false));
  604. assert(projectileEmitted);
  605. // FIXME: is this possible? Animation is over but we're yet to fire projectile?
  606. if (!projectileEmitted)
  607. {
  608. logAnim->warn("Shooting animation has finished but projectile was not emitted! Emitting it now...");
  609. emitProjectile();
  610. }
  611. }
  612. ShootingAnimation::ShootingAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked)
  613. : RangedAttackAnimation(owner, attacker, _dest, _attacked)
  614. {
  615. logAnim->debug("Created ShootingAnimation for %s", stack->getName());
  616. }
  617. void ShootingAnimation::createProjectile(const Point & from, const Point & dest) const
  618. {
  619. owner.projectilesController->createProjectile(attackingStack, from, dest);
  620. }
  621. uint32_t ShootingAnimation::getAttackClimaxFrame() const
  622. {
  623. const CCreature *shooterInfo = getCreature();
  624. return shooterInfo->animation.attackClimaxFrame;
  625. }
  626. ECreatureAnimType::Type ShootingAnimation::getUpwardsGroup() const
  627. {
  628. return ECreatureAnimType::SHOOT_UP;
  629. }
  630. ECreatureAnimType::Type ShootingAnimation::getForwardGroup() const
  631. {
  632. return ECreatureAnimType::SHOOT_FRONT;
  633. }
  634. ECreatureAnimType::Type ShootingAnimation::getDownwardsGroup() const
  635. {
  636. return ECreatureAnimType::SHOOT_DOWN;
  637. }
  638. CatapultAnimation::CatapultAnimation(BattleInterface & owner, const CStack * attacker, BattleHex _dest, const CStack * _attacked, int _catapultDmg)
  639. : ShootingAnimation(owner, attacker, _dest, _attacked),
  640. catapultDamage(_catapultDmg),
  641. explosionEmitted(false)
  642. {
  643. logAnim->debug("Created shooting anim for %s", stack->getName());
  644. }
  645. void CatapultAnimation::nextFrame()
  646. {
  647. ShootingAnimation::nextFrame();
  648. if ( explosionEmitted)
  649. return;
  650. if ( !projectileEmitted)
  651. return;
  652. if (owner.projectilesController->hasActiveProjectile(attackingStack, false))
  653. return;
  654. explosionEmitted = true;
  655. Point shotTarget = owner.stacksController->getStackPositionAtHex(dest, defendingStack) + Point(225, 225) - Point(126, 105);
  656. if(catapultDamage > 0)
  657. owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLHIT", "SGEXPL.DEF", shotTarget));
  658. else
  659. owner.stacksController->addNewAnim( new PointEffectAnimation(owner, "WALLMISS", "CSGRCK.DEF", shotTarget));
  660. }
  661. void CatapultAnimation::createProjectile(const Point & from, const Point & dest) const
  662. {
  663. owner.projectilesController->createCatapultProjectile(attackingStack, from, dest);
  664. }
  665. CastAnimation::CastAnimation(BattleInterface & owner_, const CStack * attacker, BattleHex dest_, const CStack * defender, const CSpell * spell)
  666. : RangedAttackAnimation(owner_, attacker, dest_, defender),
  667. spell(spell)
  668. {
  669. assert(dest.isValid());// FIXME: when?
  670. if(!dest_.isValid() && defender)
  671. dest = defender->getPosition();
  672. }
  673. ECreatureAnimType::Type CastAnimation::getUpwardsGroup() const
  674. {
  675. return findValidGroup({
  676. ECreatureAnimType::CAST_UP,
  677. ECreatureAnimType::SPECIAL_UP,
  678. ECreatureAnimType::SHOOT_UP,
  679. ECreatureAnimType::ATTACK_UP
  680. });
  681. }
  682. ECreatureAnimType::Type CastAnimation::getForwardGroup() const
  683. {
  684. return findValidGroup({
  685. ECreatureAnimType::CAST_FRONT,
  686. ECreatureAnimType::SPECIAL_FRONT,
  687. ECreatureAnimType::SHOOT_FRONT,
  688. ECreatureAnimType::ATTACK_FRONT
  689. });
  690. }
  691. ECreatureAnimType::Type CastAnimation::getDownwardsGroup() const
  692. {
  693. return findValidGroup({
  694. ECreatureAnimType::CAST_DOWN,
  695. ECreatureAnimType::SPECIAL_DOWN,
  696. ECreatureAnimType::SHOOT_DOWN,
  697. ECreatureAnimType::ATTACK_DOWN
  698. });
  699. }
  700. void CastAnimation::createProjectile(const Point & from, const Point & dest) const
  701. {
  702. if (!spell->animationInfo.projectile.empty())
  703. owner.projectilesController->createSpellProjectile(attackingStack, from, dest, spell);
  704. }
  705. uint32_t CastAnimation::getAttackClimaxFrame() const
  706. {
  707. //TODO: allow defining this parameter in config file, separately from attackClimaxFrame of missile attacks
  708. uint32_t maxFrames = stackAnimation(attackingStack)->framesInGroup(getGroup());
  709. return maxFrames / 2;
  710. }
  711. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, int effects):
  712. BattleAnimation(owner),
  713. animation(std::make_shared<CAnimation>(animationName)),
  714. soundName(soundName),
  715. effectFlags(effects),
  716. soundPlayed(false),
  717. soundFinished(false),
  718. effectFinished(false)
  719. {
  720. logAnim->debug("CPointEffectAnimation::init: effect %s", animationName);
  721. }
  722. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<BattleHex> hex, int effects):
  723. PointEffectAnimation(owner, soundName, animationName, effects)
  724. {
  725. battlehexes = hex;
  726. }
  727. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, BattleHex hex, int effects):
  728. PointEffectAnimation(owner, soundName, animationName, effects)
  729. {
  730. assert(hex.isValid());
  731. battlehexes.push_back(hex);
  732. }
  733. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, std::vector<Point> pos, int effects):
  734. PointEffectAnimation(owner, soundName, animationName, effects)
  735. {
  736. positions = pos;
  737. }
  738. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, int effects):
  739. PointEffectAnimation(owner, soundName, animationName, effects)
  740. {
  741. positions.push_back(pos);
  742. }
  743. PointEffectAnimation::PointEffectAnimation(BattleInterface & owner, std::string soundName, std::string animationName, Point pos, BattleHex hex, int effects):
  744. PointEffectAnimation(owner, soundName, animationName, effects)
  745. {
  746. assert(hex.isValid());
  747. battlehexes.push_back(hex);
  748. positions.push_back(pos);
  749. }
  750. bool PointEffectAnimation::init()
  751. {
  752. animation->preload();
  753. auto first = animation->getImage(0, 0, true);
  754. if(!first)
  755. {
  756. delete this;
  757. return false;
  758. }
  759. if (screenFill())
  760. {
  761. for(int i=0; i * first->width() < owner.fieldController->pos.w ; ++i)
  762. for(int j=0; j * first->height() < owner.fieldController->pos.h ; ++j)
  763. positions.push_back(Point( owner.fieldController->pos.x + i * first->width(), owner.fieldController->pos.y + j * first->height()));
  764. }
  765. BattleEffect be;
  766. be.effectID = ID;
  767. be.animation = animation;
  768. be.currentFrame = 0;
  769. for (size_t i = 0; i < std::max(battlehexes.size(), positions.size()); ++i)
  770. {
  771. bool hasTile = i < battlehexes.size();
  772. bool hasPosition = i < positions.size();
  773. if (hasTile && !forceOnTop())
  774. be.position = battlehexes[i];
  775. else
  776. be.position = BattleHex::INVALID;
  777. if (hasPosition)
  778. {
  779. be.x = positions[i].x;
  780. be.y = positions[i].y;
  781. }
  782. else
  783. {
  784. const CStack * destStack = owner.getCurrentPlayerInterface()->cb->battleGetStackByPos(battlehexes[i], false);
  785. Rect tilePos = owner.fieldController->hexPositionAbsolute(battlehexes[i]);
  786. be.x = tilePos.x + tilePos.w/2 - first->width()/2;
  787. if(destStack && destStack->doubleWide()) // Correction for 2-hex creatures.
  788. be.x += (destStack->side == BattleSide::ATTACKER ? -1 : 1)*tilePos.w/2;
  789. if (alignToBottom())
  790. be.y = tilePos.y + tilePos.h - first->height();
  791. else
  792. be.y = tilePos.y - first->height()/2;
  793. }
  794. owner.effectsController->battleEffects.push_back(be);
  795. }
  796. return true;
  797. }
  798. void PointEffectAnimation::nextFrame()
  799. {
  800. playSound();
  801. playEffect();
  802. if (soundFinished && effectFinished)
  803. {
  804. //remove visual effect itself only if sound has finished as well - necessary for obstacles like force field
  805. clearEffect();
  806. delete this;
  807. }
  808. }
  809. bool PointEffectAnimation::alignToBottom() const
  810. {
  811. return effectFlags & ALIGN_TO_BOTTOM;
  812. }
  813. bool PointEffectAnimation::waitForSound() const
  814. {
  815. return effectFlags & WAIT_FOR_SOUND;
  816. }
  817. bool PointEffectAnimation::forceOnTop() const
  818. {
  819. return effectFlags & FORCE_ON_TOP;
  820. }
  821. bool PointEffectAnimation::screenFill() const
  822. {
  823. return effectFlags & SCREEN_FILL;
  824. }
  825. void PointEffectAnimation::onEffectFinished()
  826. {
  827. effectFinished = true;
  828. }
  829. void PointEffectAnimation::onSoundFinished()
  830. {
  831. soundFinished = true;
  832. }
  833. void PointEffectAnimation::playSound()
  834. {
  835. if (soundPlayed)
  836. return;
  837. soundPlayed = true;
  838. if (soundName.empty())
  839. {
  840. onSoundFinished();
  841. return;
  842. }
  843. int channel = CCS->soundh->playSound(soundName);
  844. if (!waitForSound() || channel == -1)
  845. onSoundFinished();
  846. else
  847. CCS->soundh->setCallback(channel, [&](){ onSoundFinished(); });
  848. }
  849. void PointEffectAnimation::playEffect()
  850. {
  851. if ( effectFinished )
  852. return;
  853. for(auto & elem : owner.effectsController->battleEffects)
  854. {
  855. if(elem.effectID == ID)
  856. {
  857. elem.currentFrame += AnimationControls::getSpellEffectSpeed() * GH.mainFPSmng->getElapsedMilliseconds() / 1000;
  858. if(elem.currentFrame >= elem.animation->size())
  859. {
  860. elem.currentFrame = elem.animation->size() - 1;
  861. onEffectFinished();
  862. break;
  863. }
  864. }
  865. }
  866. }
  867. void PointEffectAnimation::clearEffect()
  868. {
  869. auto & effects = owner.effectsController->battleEffects;
  870. vstd::erase_if(effects, [&](const BattleEffect & effect){
  871. return effect.effectID == ID;
  872. });
  873. }
  874. PointEffectAnimation::~PointEffectAnimation()
  875. {
  876. assert(effectFinished);
  877. assert(soundFinished);
  878. }
  879. HeroCastAnimation::HeroCastAnimation(BattleInterface & owner, std::shared_ptr<BattleHero> hero, BattleHex dest, const CStack * defender, const CSpell * spell):
  880. BattleAnimation(owner),
  881. projectileEmitted(false),
  882. hero(hero),
  883. target(defender),
  884. tile(dest),
  885. spell(spell)
  886. {
  887. }
  888. bool HeroCastAnimation::init()
  889. {
  890. hero->setPhase(EHeroAnimType::CAST_SPELL);
  891. hero->onPhaseFinished([&](){
  892. assert(owner.getAnimationCondition(EAnimationEvents::HIT) == true);
  893. delete this;
  894. });
  895. initializeProjectile();
  896. return true;
  897. }
  898. void HeroCastAnimation::initializeProjectile()
  899. {
  900. // spell has no projectile to play, ignore this step
  901. if (spell->animationInfo.projectile.empty())
  902. return;
  903. // targeted spells should have well, target
  904. assert(tile.isValid());
  905. Point srccoord = hero->pos.center();
  906. Point destcoord = owner.stacksController->getStackPositionAtHex(tile, target); //position attacked by projectile
  907. destcoord += Point(222, 265); // FIXME: what are these constants?
  908. owner.projectilesController->createSpellProjectile( nullptr, srccoord, destcoord, spell);
  909. }
  910. void HeroCastAnimation::emitProjectile()
  911. {
  912. if (projectileEmitted)
  913. return;
  914. //spell has no projectile to play, skip this step and immediately play hit animations
  915. if (spell->animationInfo.projectile.empty())
  916. emitAnimationEvent();
  917. else
  918. owner.projectilesController->emitStackProjectile( nullptr );
  919. projectileEmitted = true;
  920. }
  921. void HeroCastAnimation::emitAnimationEvent()
  922. {
  923. if(owner.getAnimationCondition(EAnimationEvents::HIT) == false)
  924. owner.setAnimationCondition(EAnimationEvents::HIT, true);
  925. }
  926. void HeroCastAnimation::nextFrame()
  927. {
  928. float frame = hero->getFrame();
  929. if (frame < 4.0f) // middle point of animation //TODO: un-hardcode
  930. return;
  931. if (!projectileEmitted)
  932. {
  933. emitProjectile();
  934. hero->pause();
  935. return;
  936. }
  937. if (!owner.projectilesController->hasActiveProjectile(nullptr, false))
  938. {
  939. emitAnimationEvent();
  940. //TODO: check H3 - it is possible that hero animation should be paused until hit effect is over, not just projectile
  941. hero->play();
  942. }
  943. }